diff --git a/README.md b/README.md index e14b277..bdf5f96 100644 --- a/README.md +++ b/README.md @@ -88,13 +88,30 @@ Used to update a record in an SF CRM object. ### Params -- `type`: _string_ - SF CRM object API name, i.e. "Contact", "ema_CustomObject__c" +- `type`: _string_ - SF CRM object API name, i.e. "Contact", "ns_CustomObject__c" - `props`: _object_ - An object containing the new record's fields and values ### Returns _Object | `undefined`_ +## [deleteContactsByKey](./src/deleteContactsByKey.js) + +Triggers SFMC contact deletion via API. + +Docs: [REST Reference](https://developer.salesforce.com/docs/marketing/marketing-cloud/guide/DeleteByContactKeys.html) + +Requires valid token data obtained from a [getToken()](./src/getToken.js) call. + +### Params + +- `tokenData`: _object_ - SFMC REST API token +- `contactKeys`: _string[]_ - An array of contact keys deletion should be triggered for + +### Returns + +_Object_ | `undefined` + ## [deleteDataExtRow](./src/deleteDataExtRow.js) This function deletes a row in an SFMC data extension. @@ -295,7 +312,7 @@ Used to update a record in an SF CRM object. ### Params -- `type`: _string_ - SF CRM object API name, i.e. "Contact", "ema_CustomObject__c" +- `type`: _string_ - SF CRM object API name, i.e. "Contact", "ns_CustomObject__c" - `sfObjId`: _string_ - SF CRM record id, i.e. 003... for a ContactKey - `props`: _object_ - An object containing the fields to update and their new values diff --git a/include.txt b/include.txt index fb4bf57..491a5d2 100644 --- a/include.txt +++ b/include.txt @@ -1,5 +1,6 @@ ./src/createLogRow.js ./src/createSalesforceObject.js +./src/deleteContactsByKey.js ./src/deleteDataExtRow.js ./src/getAllRows.js ./src/getRowData.js diff --git a/src/deleteContactsByKey.js b/src/deleteContactsByKey.js new file mode 100644 index 0000000..a2c9ff2 --- /dev/null +++ b/src/deleteContactsByKey.js @@ -0,0 +1,28 @@ +/** + * Triggers SFMC contact deletion via API + * @param {object} tokenData SFMC REST API token + * @param {string[]} contactKeys An array of contact keys deletion should be triggered for + * @returns {object | undefined} + */ +function deleteContactsByKey(tokenData, contactKeys) { + if (!tokenData || !tokenData.token || !tokenData.restInstanceURL) { + return undefined + } + + if (!contactKeys || contactKeys.length <= 0) { + return undefined + } + + var headerNames = ['Authorization'] + var headerValues = ['Bearer ' + tokenData.token] + var requestData = { + values: contactKeys, + DeleteOperationType: 'ContactAndAttributes' + } + var requestUrl = tokenData.restInstanceURL + '/contacts/v1/contacts/actions/delete?type=keys' + var triggerDelete = HTTP.Post(requestUrl, 'application/json; charset=utf-8', Stringify(requestData), headerNames, headerValues) + + return triggerDelete.Response && triggerDelete.Response[0] + ? Platform.Function.ParseJSON(triggerDelete.Response[0]) + : undefined +} \ No newline at end of file diff --git a/utils_full.js b/utils_full.js index bd052a0..082f828 100644 --- a/utils_full.js +++ b/utils_full.js @@ -94,6 +94,35 @@ function sfmcUtils() { : { error: 'Error creating SF record' } } + /** + * Triggers SFMC contact deletion via API + * @param {object} tokenData SFMC REST API token + * @param {string[]} contactKeys An array of contact keys deletion should be triggered for + * @returns {object | undefined} + */ + function deleteContactsByKey(tokenData, contactKeys) { + if (!tokenData || !tokenData.token || !tokenData.restInstanceURL) { + return undefined + } + + if (!contactKeys || contactKeys.length <= 0) { + return undefined + } + + var headerNames = ['Authorization'] + var headerValues = ['Bearer ' + tokenData.token] + var requestData = { + values: contactKeys, + DeleteOperationType: 'ContactAndAttributes' + } + var requestUrl = tokenData.restInstanceURL + '/contacts/v1/contacts/actions/delete?type=keys' + var triggerDelete = HTTP.Post(requestUrl, 'application/json; charset=utf-8', Stringify(requestData), headerNames, headerValues) + + return triggerDelete.Response && triggerDelete.Response[0] + ? Platform.Function.ParseJSON(triggerDelete.Response[0]) + : undefined + } + /** * Delete a row in an SFMC data extension * @param {string} ext Data extensions external key @@ -599,6 +628,7 @@ function sfmcUtils() { return { createLogRow: createLogRow , createSalesforceObject: createSalesforceObject + , deleteContactsByKey: deleteContactsByKey , deleteDataExtRow: deleteDataExtRow , getAllRows: getAllRows , getRowData: getRowData