Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Update List collector from Excel data load

Michael_Nash
Tera Contributor

Hi All, 

Please Help.

I have built a catalog item that when submitted the a business rule is triggered to run an import of the attachment from the RITM, everything is working expect when I try update the list collector field it removes the current data and inserts the value form the attachment, I need it to maintain current data and add the new value from the attachment.

 

Here is my onBefore Transform Script.

 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
    // Get the value of the source field (u_kit)
    var newKitValue = source.u_kit;

    // Get the existing list from the target field (u_kits)
    var existingKits = target.u_kits || '';

    // Check if the new value already exists in the list
    var valueExists = existingKits.indexOf(newKitValue) !== -1;

    // If the new value doesn't exist, add it to the existing list
    if (!valueExists) {
        // If existing list is not empty, append a comma before adding the new value
        if (existingKits) {
            existingKits += ',';
        }
        existingKits += newKitValue;
    }

    // Set the updated list to the target field
    target.u_kits = existingKits;

})(source, map, log, target);
1 REPLY 1

Sonam_Tiwari
Kilo Sage

Hi @Michael_Nash ,

 

(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
    
    // Retrieve the new value from the source field 'u_kit'
    var newKitValue = source.u_kit;
    
    // Check if the target field 'u_kits' already has existing values else initialize as empty array
    var existingKits = target.u_kits ? target.u_kits.split(',') : [];

    // Add the new value to the array of existing values
    existingKits.push(newKitValue);

    // Join the array of values back into a comma-separated string
 
    target.u_kits = existingKits.join(',');

})(source, map, log, target);

 

Can  you try this.
I think in your snippet, it kind of replacing with the new array.

Consider indicating the response as helpful and marking it as correct if it meets your needs.