Is it possible to invoke automation from SSJS? We want to create Script Activity and based on some conditions wants to execute different automation? Can we do invoke automation using Script Activity?
2 Answers
It is relevant to add to the good answer by Brad Sapkota, that this will work perfectly within the same Business Unit. However it is also possible to start automations across Business Units using the SOAP API called from SSJS. There is a good example here, how to wrap SOAP XML in SSJS: Making SOAP Calls via SSJS
-
2To add on to an add on, you can also make this even more efficient by using WSProxy (developer.salesforce.com/docs/…) to perform your SOAP call Feb 19, 2019 at 16:06
-
Hi there, I checked in RetrieveRequest Object (developer.salesforce.com/docs/…) there is an Property named QueryAllAccounts saying in the description that can be used to query in all Accounts. Is this also a possibility to start or check automation in other BU? Have you ever tried?– LetoFeb 5, 2020 at 15:36
Yes, this is possible. Here is the sample code that invokes the automation. Please add the desired condition statements and replace the external key of the target automation.
<script runat="server">
Platform.Load("Core", "1.1.1");
// Automation A
var automationACustomerKey = "external_key_of_the_target_automation_here";
var rrA = Platform.Function.CreateObject("RetrieveRequest");
Platform.Function.SetObjectProperty(rrA, "ObjectType", "Automation");
Platform.Function.AddObjectArrayItem(rrA, "Properties", "ProgramID");
Platform.Function.AddObjectArrayItem(rrA, "Properties", "CustomerKey");
Platform.Function.AddObjectArrayItem(rrA, "Properties", "Status");
var sfpA = Platform.Function.CreateObject("SimpleFilterPart");
Platform.Function.SetObjectProperty(sfpA, "Property", "CustomerKey");
Platform.Function.SetObjectProperty(sfpA, "SimpleOperator", "equals");
Platform.Function.AddObjectArrayItem(sfpA, "Value", automationACustomerKey);
Platform.Function.SetObjectProperty(rrA, "Filter", sfpA);
var retrieveStatusA = [0, 0, 0];
var automationResultSetA = Platform.Function.InvokeRetrieve(rrA, retrieveStatusA);
var ObjectIDA = automationResultSetA[0]["ObjectID"];
var StatusA = automationResultSetA[0]["Status"];
if (ObjectIDA != "null") {
if (StatusA == 6) {
var obj = Platform.Function.CreateObject("Automation");
Platform.Function.SetObjectProperty(obj, "ObjectID", ObjectIDA);
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>
Reference: