Skip to content

Instantly share code, notes, and snippets.

@wvpv
Last active January 18, 2023 14:10
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save wvpv/b6ccea822efc424fbad2739a53308fbc to your computer and use it in GitHub Desktop.
Save wvpv/b6ccea822efc424fbad2739a53308fbc to your computer and use it in GitHub Desktop.
CNX18-RASA-contact-delete
<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>
@amrutha01
Copy link

I get error when running the above code in ssjs script automation activity.
Can any one help me

@wvpv
Copy link
Author

wvpv commented Nov 19, 2019

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.

@chandanv624
Copy link

chandanv624 commented Apr 29, 2020

@wvpv
Copy link
Author

wvpv commented May 4, 2020

Sure, just update your tenant URLs and the pattern for retrieving the token from the response.

@hruskapetr
Copy link

Hi @wvpv,
thank you for the great code. Just a note, shouldn't the line 16 look like this?
statusCode = accessTokenResult["StatusCode"];

@wvpv
Copy link
Author

wvpv commented Dec 17, 2020

@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