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:

29
Leave a Reply

avatar
10 Comment threads
19 Thread replies
5 Followers
 
Most reacted comment
Hottest comment thread
12 Comment authors
CJ BuskeyThomasChrisNicolasAndra Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Victor Corey
Guest
Victor Corey

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
Daniel

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
Victor

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
Victor

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

Ricardo den Haan
Guest
Ricardo den Haan

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
sfmc100

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?

Daniel
Guest

Hi good article!

Is it possible to create a new automation with ssjs?

I’ve trying but I can not be able to …

How can I create new automations with activities?

Is it possible?

Thank you

Mohammed
Guest
Mohammed

Can we use this script to re-start the automation if the status is “skipped”?

Andra
Guest
Andra

Hi Adam,

Thanks for sharing this.
I need to perform exactly the action from your post when clicking on a SUBMIT button in a landing page. However, I do not know SSJS.
Shall I just replace the key with External Key below?
var automationCustomerKey = “CUSTOMERKEY-OF-AUTOMATION”
and use your code as it is?
Could you also help me to understand how this can be performed when clicking on SUBMIT button? Thanks!

Nicolas
Guest
Nicolas

Hi Adam,

Now that it is possible to create an Automation with SSJS, do you know if we can schedule it as well? I can see ScheduleDefinition and Schedule objects but haven’t been able to make them work yet.

Any feedback would be greatly appreciated.
Thanks

Thomas
Guest
Thomas

Dear Adam,

I’m trying to use this script on a CloudPage (otherwise empty, only some text in it). I want to open this page and the script should then run the automation. I pasted it inbetween tags of the CloudPage, inserted the key of the automation, however, I only do get a 500.

Is there something specific I need to be aware of to get this to work? I’m an absolute SSJS newbie btw. 😉

Help is highly appreciated!

Thx,
thomas

CJ Buskey
Guest
CJ Buskey

Have you ever tried using this script in an automation within Marketing Cloud?

Basically I want an automation to run more than once/hour and I’m hoping to use a SSJS activity to trigger the automation to run.