Retrieving and Starting an Automation with SSJS

Server-side Script (SSJS) Activities are a hidden gem of the Salesforce Marketing Cloud. They’re useful for a wide range of applications. I ran into a scenario where I wanted to run an Automation programmatically — if it’s not already running.

It was way harder than it needed to be. Hope this saves you some frustration:

The retrieve is for getting the ObjectID and Status of the Automation, which you then use to check to see if it’s running and then invoke a Perform if it isn’t.

The frustrating part of this was that the ObjectID is not a retrievable property of the Automation Object (unlike Status). It’s only returned in the result-set of a successful Retrieve.

I’d suggest testing this script in a Landing Page until you get it working and then move it to the Script Activity.

SFMC Support can enable Script Activities if there’s not a Script entry in the Interactions drop-down menu.

For further reading:

Leave a Reply

10 Comments on "Retrieving and Starting an Automation with SSJS"

Notify of
avatar
Sort by:   newest | oldest | most voted
Victor Corey
Guest

How would you start an Automation in a Child BU? I tried adding

var cid = Platform.Function.CreateObject(“ClientID”);
Platform.Function.SetObjectProperty(cid, “ID”, “xxxxxxx”);
Platform.Function.SetObjectProperty(cid, “IDSpecified”, true);
Platform.Function.AddObjectArrayItem(rr, “ClientIDs”, cid);

before the Simple Filter Part but still getting automation not found?

Daniel
Guest

I’m looking to do the same as Victor here. Are we differently sure we can’t set the Account ID as part of this? Otherwise great article!

Victor
Guest
I was able to get this working using the SOAP API. Not as clean as the solution above, but it starts an Automation in the Child BU. I did have to create an API User that had the Default Business Unit to the Child Business Unit. Platform.Load(‘core’, ‘1’); var username = ‘api_user_name (set default BU to child BU)’; var password = ‘api_user_password’; var payload = ”; var endpoint = “https://webservice.s7.exacttarget.com/Service.asmx”; //Automation Customer Key var customerKey = “17148d1e-872e-6d85-b073-149b9e13a2b1”; var result; var logDE = DataExtension.Init(“Triggered_Automation_Log”); startAutomation(); function startAutomation() { payload += ”; payload += ”; payload += ‘ ‘; payload += ‘… Read more »
Victor
Guest

Doh, the XML got stripped out. Try this:
http://pasted.co/5dc31949

Ricardo den Haan
Guest

I always try to avoid using the “Platform.Function” syntax at is less readeable than using directly the corresponding API:

Platform.Load('core', '1');
var myAutomation = "Automation_Dummy";

// Search for an automation
var filter = {Property:"CustomerKey",SimpleOperator:"equals",Value:myAutomation};
var automationObject = Program.Retrieve(filter);

// Start automation if "ready"
if (2 == automationObject[0].Status) {
var automationPerformed = Program.Init(myAutomation);
automationPerformed.Perform();
}

sfmc100
Guest

I am trying this code to retrieve data through automation:

Platform.Load(“Core”,”1.1.1″); var rr = Platform.Function.CreateObject(“RetrieveRequest”); Platform.Function.SetObjectProperty(rr, “ObjectType”, “Automation”); Platform.Function.AddObjectArrayItem(rr, “Properties”, “CustomerKey”); Platform.Function.AddObjectArrayItem(rr, “Properties”, “ObjectID”); Platform.Function.AddObjectArrayItem(rr, “Properties”, “Status”); Write(Stringify(Status));
always status value are coming as null, how to retrieve all the values using automation object?

wpDiscuz