0

I am trying to query the contact and another data extension and then upsert a third data extension called customerETL.

Everything works if I hardcode values into the function but when I pass values through a for loop, I get the following error:

{"message":"Length cannot be less than zero.\r\nParameter name: length","description":"System.ArgumentOutOfRangeException: Length cannot be less than zero.\r\nParameter name: length - from Jint\r\n\r\n"}

Here is the code:

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

    function getPreferences(subkey, email, deName, fieldname){
        var dataExt = DataExtension.Init(deName);
        var dataFilter = {Property:"Subscriberkey",SimpleOperator:"equals",Value:subkey};
        var data = dataExt.Rows.Retrieve(dataFilter);
    
        data = data[0]
    
        var dataConcatList = "";
    
        var dataTrueFields = [];
    
        for (var key in data){
            if(data[key] === "True"){
                dataTrueFields.push(key);
            }
        }
    
        for (var i = 0; i < dataTrueFields.length; i++) {
            dataConcatList += dataTrueFields[i] + "|";
        }
    
        var result = dataConcatList.slice(0, -1);
        var attribute = Stringify(result);
        attribute = attribute.replace(/"/g, '');

    
        var deName = "customerETL"; 
        whereColumnArr = ["attribute:sfmcContactKey","attribute:emailAddress"];
        whereColumValueArr = [subkey, email]; 
        upsertColumnArr = [fieldname]; 
        upsertColumnValueArr = [attribute];

        var upsertedRowCount = Platform.Function.UpsertDE(
            deName,
            whereColumnArr,
            whereColumValueArr,
            upsertColumnArr,
            upsertColumnValueArr
        );
    }
    
try {

    var contactDE = DataExtension.Init("Contact");
    var contactData = contactDE.Rows.Retrieve();
    
    var listOfsubkeys = [];
    var listOfemails = [];
    
    for (var n = 0; n < contactData.length; n++){
        listOfsubkeys.push(contactData[n]["Subscriberkey"]);
        listOfemails.push(contactData[n]["EmailAddress"]);
    }
    
    for (var i = 0; i < contactData.length; i++){
        getPreferences(listOfsubkeys[i],listOfemails[i],"travel_reasons","attribute:reasons")
    }
}
catch (error){
    var err = Stringify(error);
    Write(err);
} 
    

    

</script>

Thanks in advance.

1 Answer 1

2

Seems like the version of JavaScript running in MC, does not support negative parameters in the slice() method:

var result = dataConcatList.slice(0, -1);

Replace it with something like this:

if(dataConcatList.length > 1){
    var result = dataConcatList.slice(0, dataConcatList.length-1);
    }

Keep in mind that SSJS is an ancient version of JavaScript. As the documentation states:

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.

2
  • 1
    Also as a note, it is not an exact 1:1 match with ECMAscript 3 as its not only Server-side (so removes window and DOM capabilities) but also its built on a .NET JS processor (an older version of JINT - github.com/sebastienros/jint), So there are certain aspects that do not translate correctly and provide some unexpected behaviors or restrictions from 'regular' vanilla JS. May 13 at 13:07
  • In the end I created a very long series of SQL queries to do the same thing as SSJS is such a pain and is so limited.
    – neo2049
    Jun 26 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.