Salesforce Asked by balwill on December 12, 2021
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.rn at Jint.ExecutionVisitor.Visit(MethodCall methodCall)rn at Jint.Expressions.MethodCall.Accept(IJintVisitor visitor)rn at Jint.ExecutionVisitor.Visit(MemberExpression expression)rn at Jint.Expressions.MemberExpression.Accept(IJintVisitor visitor)rn at Jint.ExecutionVisitor.Visit(UnaryExpression expression)rn at Jint.Expressions.UnaryExpression.Accept(IJintVisitor visitor)rn at Jint.ExecutionVisitor.Visit(IfStatement statement)rn at Jint.Expressions.IfStatement.Accept(IJintVisitor visitor)rn at Jint.ExecutionVisitor.Visit(BlockStatement statement)rn at Jint.Expressions.BlockStatement.Accept(IJintVisitor visitor)rn at Jint.ExecutionVisitor.Visit(ForStatement statement)rn at Jint.Expressions.ForStatement.Accept(IJintVisitor visitor)rn at Jint.ExecutionVisitor.Visit(BlockStatement statement)rn at Jint.Expressions.BlockStatement.Accept(IJintVisitor visitor)rn at Jint.ExecutionVisitor.CallFunction(JsFunction function, JsDictionaryObject that, JsInstance[] parameters)rn at Jint.Native.JsFunction.Execute(ExecutionVisitor visitor, JsDictionaryObject that, JsInstance[] parameters)rn at Jint.ExecutionVisitor.ExecuteFunction(JsFunction function, JsDictionaryObject that, JsInstance[] parameters)rn at Jint.ExecutionVisitor.Visit(MethodCall methodCall)rn at Jint.Expressions.MethodCall.Accept(IJintVisitor visitor)rn at Jint.ExecutionVisitor.Visit(MemberExpression expression)rn at Jint.Expressions.MemberExpression.Accept(IJintVisitor visitor)rn at Jint.ExecutionVisitor.Visit(MethodCall methodCall)rn at Jint.Expressions.MethodCall.Accept(IJintVisitor visitor)rn at Jint.ExecutionVisitor.Visit(MemberExpression expression)rn at Jint.Expressions.MemberExpression.Accept(IJintVisitor visitor)rn at Jint.ExecutionVisitor.Visit(MethodCall methodCall)rn at Jint.Expressions.MethodCall.Accept(IJintVisitor visitor)rn at Jint.ExecutionVisitor.Visit(MemberExpression expression)rn at Jint.Expressions.MemberExpression.Accept(IJintVisitor visitor)rn at Jint.ExecutionVisitor.Visit(ExpressionStatement statement)rn at Jint.Expressions.ExpressionStatement.Accept(IJintVisitor visitor)rn at Jint.ExecutionVisitor.Visit(BlockStatement statement)rn at Jint.Expressions.BlockStatement.Accept(IJintVisitor visitor)rn at Jint.ExecutionVisitor.Visit(TryStatement statement)","description":"Jint.Native.JsException: Object expected: includesrnException of type 'Jint.Native.JsException' was thrown. - from Jintrnrn"}
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.
Answered by Gortonington on December 12, 2021
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.
Answered by Rafał Wolsztyniak on December 12, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP