4

We are trying to update a service cloud custom object from marketing cloud. Can we make a REST api call within a script in Automation Studio.It will be great if someone in this forum can confirm they have already done it.

  • Why use the REST API, when there are AMPscript functions for updating SF objects from SFMC? – Adam Spriggs Jul 8 '18 at 11:18
  • Hi Adam,Thanks. We do not want to use cloud page ,We are trying to do this as a scheduled activity through Automation .Can we achieve that through AMPscript functions ? – Monomit Bhowmik Jul 8 '18 at 16:02
  • I'm not referring to CloudPages. You include content areas or blocks that contain AMPscript with using SSJS . See the SSJS Content Functions for details. – Adam Spriggs Jul 8 '18 at 19:40
9

I've done a REST API call from a Script Activity. Here's an example:

<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 = '{"clientId":"CLIENT_ID_GOES_HERE",';
payload += '"clientSecret":"CLIENT_SECRET_GOES_HERE"}';

var accessTokenResult = HTTP.Post(url, contentType, payload);
var statusCode = result["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 += ' "deleteOperationType": "ContactAndAttributes",';
payload += ' "targetList": {';
payload += '   "listType": {';
payload += '     "listTypeID": 3';
payload += '   },';
payload += '   "listKey": "' + DE  + '"';
payload += ' },';
payload += ' "deleteListWhenCompleted": false,';
payload += ' "deleteListContentsWhenCompleted": true';
payload += '}';

try {

  var 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>
  • To speed up the REST call in a script, you can set up another automation to get the access token and write it to a DE every 45 min (if legacy, 45min just to be safe). Then before you make api call just retrieve the token from that DE, I believe retrieve a row in a DE that only contains 1 row should be faster than an HTTP call to get access token. – tkhuynh Mar 21 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.