We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

List Collector variable value to split into multiple records.

Balaram7
Kilo Sage

Hi all,

 

In a list collector field if the records with values containing pipelines are selected , then the requirement is to split them and populate as individual records. 

eg: if a record selected in the branch variable of the catalog item is selected as (7898 | 2456 | 7654 | 8976 | 0989), then while submitting the request that record selected needs to split as commas seperated as 7898, 2456, 7654 and 8976. And that individual records with different sys_ids with the same u_id values are available in that source table. 

Anyone please help me out on how to acheive this. Tried many ways, but no luck. Below are the script i have used so far.

 

 

Script Includes: 

var SplitBranchRecords = Class.create();
SplitBranchRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getBranchSysIds: function(){
        var ids = this.getParameter('sysparm_value');
        if(!ids)
        return'';
        var idsArray = ids.split(',');
        var result = [];
        var gr = new GlideRecord('u_custom_tablename');
        gr.addQuery('u_id', 'IN',idsArray);
        gr.query();
        while(gr.next()){
            result.push(gr.getUniqueValue());
           
        }
    return result.join(',');
   
},

    type: 'SplitBranchRecords'
});
 
 
Onsubmit client script : 
 
function onSubmit() {
    //Type appropriate comment here, and begin script below
    var branchValues = g_form.getValue('branch');
    if (!branchValues){
        alert("No branch selected");
        return true;
    }
    var sysIdArray = branchValues.split(',');
    var idsToFetch = [];
    var finalSysIds = [];
    for (var i = 0; i < sysIdArray.length; i++) {
        var displayValue = g_form.getDisplayBox('branch').value;
        if (displayValue.indexOf('|') !== -1) {
            var splitValues = displayValue.split('|');
            for (var j = 0; j < splitValues.length; j++) {
                var clean = splitValues[j].trim();
                if (clean)
                    idsToFetch.push(clean);
            }
        } else {
            finalSysIds.push(sysIdArray[i]);
        }
    }


    if (idsToFetch.length === 0)
        return true;

var ga = new GlideAjax('SplitBranchRecords');
ga.addParam('sysparm_name', 'getExpandedValues');
ga.addParam('sysparm_ids', idsToFetch.join(','));
ga.getXMLwait();
var response = ga.getAnswer();
if (response) {
    var newSysIds = response.split(',');
    finalSysIds = finalSysIds.concat(newSysIds);
}
finalSysIds = Array.from(newSet(finalSysIds));
alert('breaking the list values')
g_form.setValue('branch', finalSysIds.join(','));
return true;
}
 
 
Please help me out with the corrections in the script or do i need to change the entire script or let me know if there is any other way to get this requirement done.
 
Thank you in advance.
 
1 REPLY 1

Ankur Bawiskar
Tera Patron

@Balaram7 

list collector points to reference table and holds sysIds

what's your exact business requirement?

share screenshots of catalog form

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader