verified
Create an AutomationLearn how to create an Automation in Salesforce Marketing Cloud (SFMC) with SSJS (server-side JavaScript). Code snippets include the WSProxy and REST API methods.
WSProxy
var api = new Script.Util.WSProxy();
api.setClientId({
"ID": Platform.Function.AuthenticatedMemberID(),
"UserID": Platform.Function.AuthenticatedEmployeeID()
});
var config = {
Name: "MyAutomation",
CustomerKey: GUID(),
Description: "",
AutomationType: "Scheduled",
CategoryID: null
};
var result = api.createItem('Automation', config);
<script runat="server">
Platform.Load("core", "1");
var api = new Script.Util.WSProxy();
api.setClientId({
"ID": Platform.Function.AuthenticatedMemberID(),
"UserID": Platform.Function.AuthenticatedEmployeeID()
});
try {
var config = {
Name: "MyAutomation",
CustomerKey: GUID(),
Description: "",
AutomationType: "Scheduled",
CategoryID: null
};
var result = api.createItem('Automation', config);
Write(Stringify(result));
} catch(error) {
Write(Stringify(error));
}
</script>
{
"Status": "OK",
"RequestID": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"Results": [
{
"NewID": 0,
"NewObjectID": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"PartnerKey": null,
"Object": {
"Schedule": null,
"AutomationTasks": null,
"IsActive": true,
"AutomationSource": null,
"Status": 0,
"Notifications": null,
"ScheduledTime": "0001-01-01T00:00:00.000",
"AutomationType": "Scheduled",
"UpdateModified": false,
"LastRunInstanceID": null,
"CreatedBy": 0,
"CategoryID": null,
"LastRunTime": "0001-01-01T00:00:00.000",
"LastSaveDate": "0001-01-01T00:00:00.000",
"ModifiedBy": 0,
"RecurrenceID": null,
"LastSavedBy": 0,
"InteractionObjectID": null,
"Name": "MyAutomation",
"Description": "",
"Keyword": null,
"Client": {
"ID": 100000000,
"ClientID1": 0,
"PartnerClientKey": null,
"UserID": 100000000,
"PartnerUserKey": null,
"CreatedBy": 0,
"ModifiedBy": 0,
"EnterpriseID": 0,
"CustomerKey": null,
"CustomerID": null
},
"PartnerKey": null,
"PartnerProperties": [
{
"Name": "AutomationType",
"Value": "Scheduled"
}
],
"CreatedDate": "0001-01-01T00:00:00.000",
"ModifiedDate": null,
"ID": 0,
"ObjectID": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"CustomerKey": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"Owner": null,
"CorrelationID": null,
"ObjectState": null,
"IsPlatformObject": false
},
"CreateResults": null,
"ParentPropertyName": null,
"StatusCode": "OK",
"StatusMessage": null,
"OrdinalID": 0,
"ErrorCode": 0,
"RequestID": null,
"ConversationID": null,
"OverallStatusCode": null,
"RequestType": "Synchronous",
"ResultType": null,
"ResultDetailXML": null
}
]
}
WARNING
setClientId is required to avoid the "Create Access is denied!" error.
REST API
var payload = {
"name": "Scheduled Automation",
"description": "Example for a scheduled automation",
"key": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"steps": [
{
"annotation": "",
"stepNumber": 0,
"activities": [
{
"name": "1 Minute Wait",
"objectTypeId": 467,
"displayOrder": 0,
"activityObjectId": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"serializedObject": "{\"duration\":1,\"durationUnits\":\"Minutes\"}"
}
]
}
],
"startSource": {
"typeId": 1,
"schedule": {
"icalRecur": "FREQ=DAILY;INTERVAL=1;UNTIL=20241012",
"startDate": "2024-12-01T06:00:00-04:00",
"timezoneId": 1,
}
}
};
var endpoint = restInstanceUrl + "automation/v1/automations";
var request = new Script.Util.HttpRequest(endpoint);
request.emptyContentHandling = 0;
request.retries = 2;
request.continueOnError = true;
request.setHeader("Authorization", "Bearer " + accessToken);
request.method = "POST";
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 restInstanceUrl = "https://YOUR_SUBDOMAIN.rest.marketingcloudapis.com/",
accessToken = "YOUR_REST_API_TOKEN";
try {
var payload = {
"name": "Scheduled Automation",
"description": "Example for a scheduled automation",
"key": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"steps": [
{
"annotation": "",
"stepNumber": 0,
"activities": [
{
"name": "1 Minute Wait",
"objectTypeId": 467,
"displayOrder": 0,
"activityObjectId": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"serializedObject": "{\"duration\":1,\"durationUnits\":\"Minutes\"}"
}
]
}
],
"startSource": {
"typeId": 1,
"schedule": {
"icalRecur": "FREQ=DAILY;INTERVAL=1;UNTIL=20241012",
"startDate": "2024-12-01T06:00:00-04:00",
"timezoneId": 1,
}
}
};
var endpoint = restInstanceUrl + "automation/v1/automations";
var request = new Script.Util.HttpRequest(endpoint);
request.emptyContentHandling = 0;
request.retries = 2;
request.continueOnError = true;
request.setHeader("Authorization", "Bearer " + accessToken);
request.method = "POST";
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": "0M3-1D-G03SR1G4T-H3R3",
"name": "Scheduled Automation",
"description": "Example for a scheduled automation",
"key": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"categoryId": 1234,
"statusId": 4,
"lastSavedByName": "johndoe",
"createdDate": "2024-11-01T12:00:00.000",
"createdByName": "johndoe",
"updateInProgress": false,
"steps": [
{
"annotation": "",
"stepNumber": 0,
"activities": [
{
"id": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"name": "1 Minute Wait",
"description": "",
"activityObjectId": "S0M3-GU1D-K3Y-G03SR1G4T-H3R3",
"objectTypeId": 467,
"displayOrder": 0,
"serializedObject": "{\"duration\":1,\"durationUnits\":\"Minutes\"}"
}
]
}
],
"startSource": {
"typeId": 1,
"schedule": {
"scheduleTypeId": 3,
"startDate": "2024-12-01T06:00:00-04:00",
"endDate": "2024-12-01T06:00:00-04:00",
"rangeTypeId": 1,
"occurrences": 61,
"icalRecur": "FREQ=DAILY;INTERVAL=1;UNTIL=20241012",
"timezoneId": 1,
"statusId": 0
}
}
}
Reference
Ressources and references related to the current methods.
REST documentation
SOAP documentation
SOAP object