Created
March 17, 2016 16:18
-
-
Save wvpv/a268a989daf0867bc028 to your computer and use it in GitHub Desktop.
SFMC SSJS Retrieve and Start and Automation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script runat="server"> | |
Platform.Load("Core","1.1.1"); | |
var automationCustomerKey = "CUSTOMERKEY-OF-AUTOMATION" | |
var rr = Platform.Function.CreateObject("RetrieveRequest"); | |
Platform.Function.SetObjectProperty(rr, "ObjectType", "Automation"); | |
Platform.Function.AddObjectArrayItem(rr, "Properties", "ProgramID"); | |
Platform.Function.AddObjectArrayItem(rr, "Properties", "CustomerKey"); | |
Platform.Function.AddObjectArrayItem(rr, "Properties", "Status"); | |
var sfp = Platform.Function.CreateObject("SimpleFilterPart"); | |
Platform.Function.SetObjectProperty(sfp, "Property", "CustomerKey"); | |
Platform.Function.SetObjectProperty(sfp, "SimpleOperator", "equals"); | |
Platform.Function.AddObjectArrayItem(sfp, "Value", automationCustomerKey); | |
Platform.Function.SetObjectProperty(rr, "Filter", sfp); | |
var retrieveStatus = [0,0,0]; | |
var automationResultSet = Platform.Function.InvokeRetrieve(rr, retrieveStatus); | |
var ObjectID = automationResultSet[0]["ObjectID"]; | |
var Status = automationResultSet[0]["Status"]; | |
if (ObjectID != "null") { | |
/* | |
Code Status | |
-1 Error | |
0 BuildingError | |
1 Building | |
2 Ready | |
3 Running | |
4 Paused | |
5 Stopped | |
6 Scheduled | |
7 Awaiting Trigger | |
8 InactiveTrigger | |
*/ | |
if (Status == 2) { | |
var obj = Platform.Function.CreateObject("Automation"); | |
Platform.Function.SetObjectProperty(obj, "ObjectID", ObjectID); | |
var po = Platform.Function.CreateObject("PerformOptions"); | |
var performResult = [0,0,0]; | |
var performStatus = Platform.Function.InvokePerform(obj, "start", performResult, po); | |
} else { | |
// already running | |
} | |
} else { | |
// automation not found | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It will be work if you replace "Automation" with "Program" and "ProgramID" with "ObjectID".
Also, if you work in different business units you need add this:
`var mid = 1234567;
var clientId = Platform.Function.CreateObject("clientId");
Platform.Function.SetObjectProperty(clientId, "ID", mid);
Platform.Function.SetObjectProperty(clientId, "IDSpecified", "true");
var rr = Platform.Function.CreateObject("RetrieveRequest");
Platform.Function.AddObjectArrayItem(rr, "ClientIDs", clientId);`