verified
Add an Activity to an AutomationLearn how to add an Activity to an Automation in Salesforce Marketing Cloud (SFMC) with SSJS (server-side JavaScript). Code snippets include the REST API method.
This method works for Script Activities, Query Activities and all the other types of Activities allowed in an Automation.
REST API
var autoObjectId = "S0M3-GU1D-K3Y-G03SR1G4T-H3R3";
var payload = {
"steps": [
{
"annotation": "",
"stepNumber": 0,
"activities": [
{
"name": "My Script Activity",
"objectTypeId": 423,
"displayOrder": 2,
"activityObjectId": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3"
}
]
}
]
};
var endpoint = restInstanceUrl + "automation/v1/automations/" + autoObjectId;
var request = new Script.Util.HttpRequest(endpoint);
request.emptyContentHandling = 0;
request.retries = 2;
request.continueOnError = true;
request.setHeader("Authorization", "Bearer " + accessToken);
request.method = "PATCH";
request.contentType = "application/json";
request.encoding = "UTF-8";
request.postData = Stringify(payload);
var results = request.send();
var result = Platform.Function.ParseJSON(String(results.content));
<script runat="server">
Platform.Load("core", "1");
var api = new Script.Util.WSProxy();
var restInstanceUrl = "https://YOUR_SUBDOMAIN.rest.marketingcloudapis.com/",
accessToken = "YOUR_REST_API_TOKEN";
try {
var myActivity = {
"name": "My Script Activity",
"objectTypeId": 423,
"displayOrder": 2,
"activityObjectId": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3"
};
var automationObjectId = "S0M3-GU1D-K3Y-G03SR1G4T-H3R3";
var endpoint = restInstanceUrl + "automation/v1/automations/" + automationObjectId;
var request = new Script.Util.HttpRequest(endpoint);
request.emptyContentHandling = 0;
request.retries = 2;
request.continueOnError = true;
request.setHeader("Authorization", "Bearer " + accessToken);
request.method = "GET";
request.contentType = "application/json";
var results = request.send();
var result = Platform.Function.ParseJSON(String(results.content));
var activities = result.steps[result.steps.length - 1].activities;
activities.push(myActivity);
var payload = {
steps: result.steps
};
var request = new Script.Util.HttpRequest(endpoint);
request.emptyContentHandling = 0;
request.retries = 2;
request.continueOnError = true;
request.setHeader("Authorization", "Bearer " + accessToken);
request.method = "PATCH";
request.contentType = "application/json";
request.encoding = "UTF-8";
request.postData = Stringify(payload);
var results = request.send();
var result = Platform.Function.ParseJSON(String(results.content));
Write(Stringify(result));
} catch(error) {
Write(Stringify(error));
}
</script>
{
"id": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"legacyId": "AZERTYUIOPQSDFGHJKLM",
"name": "MyAutomation",
"description": "",
"key": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"categoryId": 3267,
"statusId": 2,
"lastSavedDate": "2022-12-13T11:16:15.377",
"lastSavedByName": "Some app user",
"createdDate": "2022-12-13T10:16:59.39",
"createdByName": "",
"updateInProgress": false,
"steps": [
{
"annotation": "",
"stepNumber": 0,
"activities": [
{
"id": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"name": "My Script Activity",
"description": "",
"activityObjectId": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"objectTypeId": 423,
"displayOrder": 2,
"serializedObject": ""
}
]
}
],
"startSource": {
"typeId": 1,
"schedule": {
"scheduleTypeId": 3,
"startDate": "0001-01-01T00:00:00",
"endDate": "0001-01-01T00:00:00",
"rangeTypeId": 0,
"occurrences": 0,
"icalRecur": "FREQ=DAILY;INTERVAL=1",
"timezoneId": 79,
"statusId": 0
}
}
}
WARNING
In order to add a new activity, please first retrieve the exisiting ones and add them to the payload. Otherwise, the existing activities will be overwritten by the PATCH call.
ObjectTypeId property reference
Id | Automation Activity |
---|---|
42 | User-initiated Email |
43 | Import Definition |
45 | Group Definition |
53 | File Transfer Activity |
73 | Data Extract Activity |
84 | Report Activity |
300 | Query Activity |
303 | Filter Activity |
423 | Server Side Javascript Activity |
425 | ELT Activity |
427 | Build Audience Activity |
467 | Program Wait |
724 | Mobile Automation List Refresh Instance |
725 | MobileConnect Message Instance |
726 | Mobile File Import Instance |
733 | InteractionStudio |
736 | MobilePush Message Object Instance |
749 | Interaction Studio Event |
756 | Interaction Studio Date Event |
771 | Salesforce Send Activiy |
783 | GroupConnect |
1010 | Thunderhead Transfer Activity |
1101 | Interaction Studio Decision Activity |
1701 | PredictiveIntelligenceRecommendationActivity |
Reference
Ressources and references related to the current methods.
SOAP object