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.
-
1Why use the REST API, when there are AMPscript functions for updating SF objects from SFMC?– Adam SpriggsJul 8, 2018 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 BhowmikJul 8, 2018 at 16:02
-
1I'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 SpriggsJul 8, 2018 at 19:40
1 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>
-
1To 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.– tkhuynhMar 21, 2019 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. Dec 19, 2019 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. Dec 19, 2019 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. Dec 19, 2019 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– tkhuynhDec 20, 2019 at 21:08