[SalesForce] Making a REST API call from a Script Activity in Automation Studio

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.

Best Answer

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);

/* for legacy API component package */
var url = 'https://auth.exacttargetapis.com/v1/requestToken';

/* for newest API component packages */
/* var url = 'https://auth.exacttargetapis.com/v2/token'; */

var contentType = 'application/json';

var payload = {
  "grant_type":"client_credentials",
  "client_id":"YOUR_CLIENT_ID",
  "client_secret":"YOUR_CLIENT_SECRET",
  "account_id":"YOUR_MID"
};

var authRequest = HTTP.Post(url, contentType, Stringify(payload));
var statusCode = authRequest["StatusCode"];
var response = authRequest["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];

var payload = {
     "deleteOperationType": "ContactAndAttributes",
     "targetList": {
       "listType": {
         "listTypeID": 3
       },
       "listKey": DE 
     },
   "deleteListWhenCompleted": false,
   "deleteListContentsWhenCompleted": true
};

try {

  var result = HTTP.Post(url, contentType, Stringify(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>
Related Topic