Created
October 12, 2020 05:07
-
-
Save eliotharper/d1f8c7b4e5b4643e3b9b9da483fa04de to your computer and use it in GitHub Desktop.
SSJS Script to delete Contacts from Salesforce Marketing Cloud
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 language="javascript" runat="server"> | |
Platform.Load("core","1.1.5"); | |
// Author: Eliot Harper <[email protected]> | |
// Revision date: October 24, 2019 | |
// DISCLAIMER | |
// While due care has been taken in the preparation of this | |
// supplied code example, no liability is assumed for incidental | |
// or consequential damages in connection with or arising out its | |
// use. Example code is provided on an "as is" basis and no | |
// expressed or implied warranty of any kind is made for the | |
// suitability of this code for your purpose. Salesforce Marketing | |
// Cloud operational procedures and programming methods may change | |
// between releases and you are solely responsible for determining | |
// whether this code is applicable for your intended use. | |
var eid = Platform.Function.AuthenticatedEnterpriseID(); | |
var deKey = 'INSERT_DE_KEY'; | |
var clientId = 'INSERT_CLIENT_ID'; | |
var clientSecret = 'INSERT_CLIENT_SECRET'; | |
var authBaseUrl = 'INSERT_AUTH_BASE_URL'; | |
var authUrl = authBaseUrl + 'v2/token/' | |
var contentType = 'application/json'; | |
payload = '{'; | |
payload += ' "grant_type": "client_credentials",'; | |
payload += ' "client_id":"' + clientId + '",'; | |
payload += ' "client_secret":"' + clientSecret + '",'; | |
payload += ' "scope": "list_and_subscribers_write",'; | |
payload += ' "account_id": "' + eid + '" '; | |
payload += '}'; | |
var result = HTTP.Post(authUrl, contentType, payload); | |
var response = result["Response"][0]; | |
var accessToken = Platform.Function.ParseJSON(response).access_token; | |
var restUrl = Platform.Function.ParseJSON(response).rest_instance_url; | |
Platform.Response.Write(" response: " + response); | |
Platform.Response.Write("\n\n token: " + accessToken); | |
Platform.Response.Write("\n\n rest URL: " + restUrl); | |
var headerNames = ["Authorization"]; | |
var headerValues = ["Bearer " + accessToken]; | |
payload = '{'; | |
payload += ' "deleteOperationType": "ContactAndAttributes",'; | |
payload += ' "targetList": { '; | |
payload += ' "listType": { '; | |
payload += ' "listTypeID":3'; | |
payload += ' },'; | |
payload += ' "listKey": "' + deKey + '"'; | |
payload += ' },'; | |
payload += ' "deleteListWhenCompleted":false,'; | |
payload += ' "deleteListContentsWhenCompleted":true'; | |
payload += '}'; | |
var deleteEndpoint = restUrl + 'contacts/v1/contacts/actions/delete?type=listReference'; | |
var result = HTTP.Post(deleteEndpoint, contentType, payload, headerNames, headerValues) | |
result = Stringify(result).replace(/[\n\r]/g, ''); | |
Platform.Response.Write("\n\n result: " + result); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've just created a sync between Sales cloud and MC with a filter based on a check box to Include records into MC Sync. I understand that even if that box is unchecked and it no longer syncs a record, because of the initial sync that sub is still in the All Contacts record count. Wondering if there's a way to tweak this script to do just that?