1

I have to create an automation in Salesforce Marketing Cloud to run every day from 8am to 10pm but unfortunately the schedule configuration does not have this option.

I heard that it is possible to create a SSJS to stop the automation at 10 pm and another to start it at 8 am. I am new to SSJS and don't know if this is real or how to do it.

I would like to know if anyone has used a SSJS in a similar situation or can help me to solve this problem.

Thank you in advance.

1
  • Does it need to run hourly in that period, or? Commented Nov 3, 2022 at 13:39

2 Answers 2

3

There are many ways to tackle this programmatically [and have its own Pros and Cons]
If your requirement is to Run your automation from 8am to 10pm every hour; daily.
Create a secondary automation as below.

  • Scheduled to run daily at 8am.
  • Add 14 Script activity with below code with 13 wait activities [of 1 hours] (change this frequency) as per your requirement.

enter image description here

SSJS code to run automation

<script runat="server">
  Platform.Load('core', '1');
  var myAutomation1 = "YourAutomationKey"; //External key of the automation
  var prog1 = Program.Init(myAutomation1);
  prog1.Perform();
</script>

WSproxy code to run automation [updated as per suggestion by Greg]

/*  You will be able to access automations in child BU from the parent BU and the reverse, from child to parent, 
but don't think you won't be able to go from child BU to child BU.  */

<script  runat="server">
    Platform.Load("core","1.1.1");

    var prox = new Script.Util.WSProxy();

    //Switch context to different BU. Change the MID to suit.
    prox.setClientId({ "ID": xxxxxxxx });

    // Need to retrieve the ObjectID for the Automation, which isn't available via the SFMC interface.
    var cols = ["Name", "ProgramID", "IsActive"];
    var filter = {
        Property: "Name",
        SimpleOperator: "equals",
        Value: "xxxxxxxxxx Name of your Automation xxxxxxxxxxxx"
    };
    var desc = prox.retrieve("Automation", cols, filter);

    // Output ObjectID if you want to see it. 
    Write('<br>ObjectID: ' + desc.Results[0].ObjectID);

    var props = {
        ObjectID: desc.Results[0].ObjectID
    };
    var action = "start";
    var opts = {};
    var res = prox.performItem("Automation", props, action, opts);
    var status = res.Status;
    var statusCode = res.Results[0].StatusCode;

    Write("status code: " + statusCode);

    //reset the context if required
    prox.resetClientIds();
</script>
2
  • @Jerry I would be careful using the Program object in SFMC SSJS as it is no longer a supported object and 'automations' technically are not 'programs' as these were the precursor to automations when it was still Exact Target. This means there is potential for some unexpected behavior. This level of risk can make utilization in a production environment a potential issue. I would instead recommend using WSProxy to accomplish this. (can do similar process like this answer, or can ustilize Schedule Method to pause/activate or can even use some undocumented REST APIs to do it) Commented Nov 3, 2022 at 17:18
  • 1
    @Gortonington thank you so much... for your inputs.... I also was unaware of this information... I have updated my answer... feel free to correct/add more details.. Commented Nov 4, 2022 at 6:18
0

Run automation in Parent Business unit from Child Business unit.

Recently I noticed using WS proxy we can only run Child automation from Parent. But running an automation in Parent from is NOT possible.

Here is the alternative I found.

<script runat="server">
Platform.Load("Core","1.1.1");

/* Change Subdomain, ClientID, ClientSecrete based on ur instance */

var subdomain = 'xxx';
var authEndpoint = 'https://'+subdomain+'.auth.marketingcloudapis.com';
var payload = {
        client_id: "xxx",
        client_secret: "xxx",
        grant_type: "client_credentials",
        account_id: xxxx //Parent BU MID
    };
var url = authEndpoint + '/v2/token';  
var contentType = 'application/json';

try {
    /* Generating access token :: START */
   var accessTokenRequest = HTTP.Post(url, contentType, Stringify(payload));
   if(accessTokenRequest.StatusCode == 200) 
   {
        var tokenResponse = Platform.Function.ParseJSON(accessTokenRequest.Response[0]);
        var accessToken = tokenResponse.access_token;
        Write('Access token = '+accessToken)
   }
   /* Generating access token :: END */

   /* SOAP API :: automation start :: START */ 
    var payload = '';
    var endpoint = "https://"+subdomain+".soap.marketingcloudapis.com/Service.asmx";
    var result;

    payload += '<?xml version="1.0" encoding="UTF-8"?>';
    payload += '<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">';
    payload += '    <s:Header>';
    payload += '        <a:Action s:mustUnderstand="1">Perform</a:Action>';
    payload += '        <a:To s:mustUnderstand="1">https://'+subdomain+'.soap.marketingcloudapis.com/Service.asmx</a:To>';
    payload += '        <fueloauth xmlns="http://exacttarget.com">'+accessToken+'</fueloauth>';
    payload += '    </s:Header>';
    payload += '    <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">';
    payload += '        <PerformRequestMsg xmlns="http://exacttarget.com/wsdl/partnerAPI">';
    payload += '         <Action>start</Action>';
    payload += '         <Definitions>';
    payload += '            <Definition xsi:type="Automation">';
    payload += '               <Client>';
    payload += '                  <ID>xxxx</ID>'; //Parent BU MID
    payload += '               </Client>';
    payload += '               <ObjectID>xxxxx</ObjectID>'; //ObjectID of your automation [note this is not customerkey]
    payload += '            </Definition>';
    payload += '         </Definitions>';
    payload += '      </PerformRequestMsg>';
    payload += '    </s:Body>';
    payload += '</s:Envelope>';

    try {
           
            /* Write(' Paylod = '+payload); */
            
            result = HTTP.Post(endpoint,"text/xml",payload,["SOAPAction"],["Perform"]);
           /* Write("Result = "+result); */
        } 
    catch(e) 
        {
            result = {StatusCode:500,Response:Stringify(e)};    
        }
   /* SOAP API :: automation start :: END */

}//try 

catch (error) {
    Write(Stringify(error));
}
</script>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .