0

I am trying to add an existing Query Activity to an existing Automation using a SSJS script. This is my code. I have referred the doc

<script runat="server">
    Platform.Load("core", "1");
    var api = new Script.Util.WSProxy();
    var restInstanceUrl = "https://SUBDOMAIN.rest.marketingcloudapis.com";
    var accessToken = "--------";
    try 
    {
        var myActivity = {
            "name": "myquery",
            "objectTypeId": 300,
            "displayOrder": 1,
            "activityObjectId": "d3771ef8-1be9-4bd7-9539-c0e4b445f019"
        };

        var automationObjectId = "c3e1111e-6777-408b-b4f9-556d76539281";
        var endpoint = restInstanceUrl + "automation/v1/automations/" + automationObjectId;

        var request = new Script.Util.HttpRequest(endpoint);
        request.emptyContentHandling = 0;
        request.retries = 2;
        request.continueOnError = true;
        request.setHeader("Authorization", "Bearer " + accessToken);
        request.method = "GET";
        request.contentType = "application/json";

        var results = request.send();
        var result = Platform.Function.ParseJSON(String(results.content));
        var activities = result.steps[result.steps.length - 1].activities;
        activities.push(myActivity);

        var payload = {
            steps: result.steps
        };

        var request = new Script.Util.HttpRequest(endpoint);
            request.emptyContentHandling = 0;
            request.retries = 2;
            request.continueOnError = true;
            request.setHeader("Authorization", "Bearer " + accessToken);
            request.method = "PATCH";
            request.contentType = "application/json";
            request.encoding = "UTF-8";
            request.postData = Stringify(payload);

        var results = request.send();

        var result = Platform.Function.ParseJSON(String(results.content));

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

But I am getting error :

{"message":"Object expected: push","jintException":"Jint.Native.JsException: Exception of type 'Jint.Native.JsException' was thrown.\r\n at Jint.ExecutionVisitor.Visit(MethodCall methodCall)\r\n at Jint.Expressions.MethodCall.Accept(IJintVisitor visitor)\r\n at Jint.ExecutionVisitor.Visit(MemberExpression expression)\r\n at Jint.Expressions.MemberExpression.Accept(IJintVisitor visitor)\r\n at Jint.ExecutionVisitor.Visit(ExpressionStatement statement)\r\n at Jint.Expressions.ExpressionStatement.Accept(IJintVisitor visitor)\r\n at Jint.ExecutionVisitor.Visit(BlockStatement statement)\r\n at Jint.Expressions.BlockStatement.Accept(IJintVisitor visitor)\r\n at Jint.ExecutionVisitor.Visit(TryStatement statement)","description":"Jint.Native.JsException: Object expected: push\r\nException of type 'Jint.Native.JsException' was thrown. - from Jint\r\n\r\n"}

Could please someone help me, point out what is wrong in the code?

1 Answer 1

2

It looks like the trouble in your code is how you're tinkering with the activities array. The error message is giving you a heads up about an "Object expected: push" issue, indicating that the object you're trying to push isn't being recognized as an array.

To sort this out, let's make sure that the activities property is treated as an array before attempting to add a new activity. I've tweaked your code a bit to fix this hiccup

The issue in your code is related to the way you are trying to manipulate the activities array

    <script runat="server">
    
        Platform.Load("core", "1");
    
        var api = new Script.Util.WSProxy();
    
        var restInstanceUrl = "https://SUBDOMAIN.rest.marketingcloudapis.com";
        var accessToken = "--------";
    
        try {
    
            var myActivity = {
                "name": "myquery",
                "objectTypeId": 300,
                "displayOrder": 1,
                "activityObjectId": "d3771ef8-1be9-4bd7-9539-c0e4b445f019"
            };
    
            var automationObjectId = "c3e1111e-6777-408b-b4f9-556d76539281";
    
            var endpoint = restInstanceUrl + "automation/v1/automations/" + automationObjectId;
    
            var request = new Script.Util.HttpRequest(endpoint);
            request.emptyContentHandling = 0;
            request.retries = 2;
            request.continueOnError = true;
            request.setHeader("Authorization", "Bearer " + accessToken);
            request.method = "GET";
            request.contentType = "application/json";
    
            var results = request.send();
    
            var result = Platform.Function.ParseJSON(String(results.content));
    
            // Ensure that 'activities' is an array
            if (!Array.isArray(result.steps[result.steps.length - 1].activities)) {
                result.steps[result.steps.length - 1].activities = [];
            }
    
            // Push the new activity
            result.steps[result.steps.length - 1].activities.push(myActivity);
    
            var payload = {
                steps: result.steps
            };
    
            var request = new Script.Util.HttpRequest(endpoint);
            request.emptyContentHandling = 0;
            request.retries = 2;
            request.continueOnError = true;
            request.setHeader("Authorization", "Bearer " + accessToken);
            request.method = "PATCH";
            request.contentType = "application/json";
            request.encoding = "UTF-8";
            request.postData = Stringify(payload);
    
            var results = request.send();
    
            var result = Platform.Function.ParseJSON(String(results.content));
    
            Write(Stringify(result));
    
        } 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 .