1

i have the following functions i'm using to not only collect all DEs in an account but using the JSON payload from that, also extract all the Business Unit IDs.

Within the pullAllBusinessUnits() function below, i have a simple if statement, basically if the Business Unit ID doesn't exist in the allBusinessUnits[] then add it.

 function pullAllDataExtensions() {
    var prox                            = new Script.Util.WSProxy();
    var moreData                        = true;
    var reqID                           = null;
    var cols                            = ["Name","CustomerKey","CategoryID","IsSendable", "Client.ID"];
    var filterAllDataExtensions         = {
                                            LeftOperand: {
                                                Property        : "IsSendable",
                                                SimpleOperator  : "equals",
                                                Value           : true
                                            },
                                            LogicalOperator: "OR",
                                            RightOperand: {
                                                Property        : "IsSendable",
                                                SimpleOperator  : "equals",
                                                Value           : false
                                            }
                                        }
    var options                         = {IncludeObjects: true};
    var props                           = {   
        QueryAllAccounts: true
    };
    var dataExtensionResults            = [];
    while(moreData) {
        moreData = false;
        var data = reqID == null ?
            prox.retrieve("DataExtension", cols, filterAllDataExtensions, options, props) : 
            prox.getNextBatch("DataExtension", reqID);
        if(data != null) {
            moreData    = data.HasMoreRows;
            reqID       = data.RequestID;
            if (data && data.Results) {
                for (i=0; i < data.Results.length; i++) {
                    dataExtensionResults.push(data.Results[i])
                }
            }
        }
        
        if (debug == true) {
            Write("Results: " + data.Results.length)
            Write(Stringify(data))
        }
    };
    return dataExtensionResults
}

// Write(Stringify(pullAllDataExtensions()))

function pullAllBusinessUnits() {
    var allDataExtensions = pullAllDataExtensions();
    var allBusinessUnits = [];

    for (i=0; i < allDataExtensions.length; i++) {
        var buID = allDataExtensions[i].Client.ID

        if (!allBusinessUnits.includes(buID)){
            allBusinessUnits.push(allDataExtensions[i].Client.ID)
        } 
    };

    return allBusinessUnits
}

Write(Stringify(pullAllBusinessUnits())) // using for debug 

My issue? For some reason, the response i get when i use the array.includes() method is that it is expecting an object - see below. Anyone know why this is and how i can possibly fix the code?

{"message":"Object expected: includes","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(UnaryExpression expression)\r\n at Jint.Expressions.UnaryExpression.Accept(IJintVisitor visitor)\r\n at Jint.ExecutionVisitor.Visit(IfStatement statement)\r\n at Jint.Expressions.IfStatement.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(ForStatement statement)\r\n at Jint.Expressions.ForStatement.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.CallFunction(JsFunction function, JsDictionaryObject that, JsInstance[] parameters)\r\n at Jint.Native.JsFunction.Execute(ExecutionVisitor visitor, JsDictionaryObject that, JsInstance[] parameters)\r\n at Jint.ExecutionVisitor.ExecuteFunction(JsFunction function, JsDictionaryObject that, JsInstance[] parameters)\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(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(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: includes\r\nException of type 'Jint.Native.JsException' was thrown. - from Jint\r\n\r\n"}

2 Answers 2

6

As @RafalWolsztyniak stated, SFMC SSJS currently uses ECMAscript 3, so a good portion of these types of functions or methods you have been able to use for years in JS are not available in SSJS. BUT, this does not mean we lose the abilities of these functions, just that they are not premade or 'baked-in' to SSJS.

For instance, if you create the backfill like below:

  Array.includes = function(val,arr) {
    for(i=0;i<arr.length;i++){
       if (!ret || ret == false){ret = val == arr[i] ? true: false;}
    }
    return ret;
  }

You can then run:

var arr = ["Emil", "Tobias", "Linus"];
var checker = Array.includes("Emil",arr)

And it will output true.

Just because the function does not exist OOTB, does not mean you cannot then just make the same function manually and use it.

2
  • 1
    Amazing! I’m new to Js (just getting into it after using ssjs) so this info is very helpful. Thanks again @Gortonington!!!
    – balwill
    Commented Jul 23, 2020 at 15:40
  • 1
    @Gortonington This is an awesome idea, thanks for sharing it :) Commented Jul 23, 2020 at 18:08
1

SSJS in Marketing Cloud uses an outdated JavaScript version that reaches 20 years back:

The Core library currently stands at version 1.1.1 and supports ECMAscript 3.0. This version represents the current, original version of the server-side JavaScript library and contains no revisions.

(Source)

As such newer functions functions than ca. 2000 are not supported and you are forced to recreate their functionality yourself.

1
  • Thanks! I was looking for this type of info.
    – balwill
    Commented Jul 23, 2020 at 14:26

You must log in to answer this question.

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