Last active
January 18, 2023 14:10
-
-
Save wvpv/b6ccea822efc424fbad2739a53308fbc to your computer and use it in GitHub Desktop.
CNX18-RASA-contact-delete
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="javascript" runat="server"> | |
Platform.Load("core","1.1.5"); | |
var DE = "Contacts_To_Delete"; | |
var logDE = "Contacts_To_Delete_Log"; | |
var log = DataExtension.Init(logDE); | |
var url = 'https://auth.exacttargetapis.com/v1/requestToken'; | |
var contentType = 'application/json'; | |
var payload = ""; | |
payload += '{"clientId":"CLIENT_ID_GOES_HERE",'; | |
payload += '"clientSecret":"CLIENT_SECRET_GOES_HERE"}'; | |
var accessTokenResult = HTTP.Post(url, contentType, payload); | |
var statusCode = accessTokenResult["StatusCode"]; | |
var response = accessTokenResult["Response"][0]; | |
var accessToken = Platform.Function.ParseJSON(response).accessToken; | |
url = "https://www.exacttargetapis.com/contacts"; | |
url += "/v1/contacts/actions/delete?type=listReference"; | |
var headerNames = ["Authorization"]; | |
var headerValues = ["Bearer " + accessToken]; | |
payload = ""; | |
payload += '{'; | |
payload += ' "deleteOperationType": "ContactAndAttributes",'; | |
payload += ' "targetList": {'; | |
payload += ' "listType": {'; | |
payload += ' "listTypeID": 3'; | |
payload += ' },'; | |
payload += ' "listKey": "' + DE + '"'; | |
payload += ' },'; | |
payload += ' "deleteListWhenCompleted": false,'; | |
payload += ' "deleteListContentsWhenCompleted": true'; | |
payload += '}'; | |
try { | |
result = HTTP.Post(url, contentType, payload, headerNames, headerValues); | |
result = Stringify(result).replace(/[\n\r]/g, ''); | |
log.Rows.Add({"Message": "result: " + result}); | |
} catch (e) { | |
e = Stringify(e).replace(/[\n\r]/g, '') | |
log.Rows.Add({"Message": "error: " + e}); | |
} | |
</script> |
If you have questions like this, I'd recommend asking them on https://salesforce.stackexchange.com. Please include all you've tried and the exact errors you are receiving.
Will this work with the OAuth 2.0?
https://developer.salesforce.com/docs/atlas.en-us.mc-app-development.meta/mc-app-development/integration-considerations.htm
Sure, just update your tenant URLs and the pattern for retrieving the token from the response.
Hi @wvpv,
thank you for the great code. Just a note, shouldn't the line 16 look like this?
statusCode = accessTokenResult["StatusCode"];
@hruskapetr yes, you're correct. Updated and thank you!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I get error when running the above code in ssjs script automation activity.
Can any one help me