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.

  • 1
    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
  • 1
    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
10

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>
  • 1
    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 '19 at 16:38
  • In addition, why do you wrote the whole payload as a string since SSJS allows JSON handling? I think it's better to have a JSON payload and then in the Post function use the function Stringify to send a string version of that payload. – Darleison Rodrigues Dec 19 '19 at 16:09
  • @DarleisonRodrigues It's b/c I'm a JS noob. Feel free to re-write it as your own answer. You'll certainly get an upvote from me if you did. – Adam Spriggs Dec 19 '19 at 16:22
  • @tkhuynh Under normal circumstances, automations can run at a minimum of every hour. New REST tokens are only good for around 20 minutes. – Adam Spriggs Dec 19 '19 at 16:24
  • @AdamSpriggs you are correct about automation run at a minimum of 1 hour. However, there is wait time activity. What you would do is to have the automation run every hour with 3 activities: 1st is script activity, 2nd wait activity for 45 min, 3rd is the same script activity as the 1st one. With V2 REST token which expires in ~18 min. So I would set up 1 hour run automation with: scritpt, wait 15min, script, wait 15min, script, wait 15, script.Hope this answers your concern – tkhuynh Dec 20 '19 at 21:08

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.