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.

How do I copy one array (i.e. List) to another in a business rule?

Shane J
Tera Guru

I need to copy an array (List) from the Idea table to a new Project record that has another field setup exactly the same way.   A straight-up 'setValue' doesn't work.

23 REPLIES 23

Glad it works for you.



Regards,


Sachin


I spoke too soon, all that was doing was dumping invalid String entries into the Project field...  


ok a glide list is just a comma separated list of sys id's... so the first thing you want to do is go to the source record.. where you have the regions listed.. right click the header and make VERY sure it is a comma separated list of sys id's



now for the loop to load it.. the other poster was very close with...



var proj = new GlideRecord('pm_project');


proj.addQuery('u_idea',current.sys_id);


proj.query();


while(proj.next()){



var list = current.u_users.getDisplayValue();


var array = list.split(",");


for (var i=0; i < array.length; i++) {


//gs.print("Reference value is: " + array[i]);


proj.watch_list = proj.watch_list + array[i];


proj.update();


}



}




the only real issue with this is that there is no comma seperation...



so modify just a tad.



var proj = new GlideRecord('pm_project');


proj.addQuery('u_idea',current.sys_id);


proj.query();


while(proj.next()){



var tag = true;


var list = current.u_users.getDisplayValue();


var array = list.split(",");


for (var i=0; i < array.length; i++) {


if(tag){


proj.watch_list = proj.watch_list + array[i];


}


else{


//gs.print("Reference value is: " + array[i]);


proj.watch_list = ',' + proj.watch_list + array[i];


}


}


proj.update();


gs.addInfoMessage('final list is ' + proj.watch_list);



}


Had to modify that for my use, and not getting the expected results:



This from the current record:


find_real_file.png



Copies to the new Project like this:


find_real_file.png




Here's my BR code (your addition on bold😞



var projTable = "pm_project";
if(GlidePluginManager.isActive('com.snc.project_management_v3')){
projTable = SNC.PPMConfig.getProjectTable(current.getTableName());
}


var proj = new GlideRecord(projTable);



var tag = true;
var list = current.u_region.getDisplayValue();
var array = list.split(",");
for (var i=0; i < array.length; i++) {
if(tag){
proj.u_regions = proj.u_regions + array[i];
}
else{
//gs.print("Reference value is: " + array[i]);
proj.u_regions = ',' + proj.u_regions + array[i];
}
}


//proj.setValue("u_regions", reg);



var dmnId = proj.insert();
proj.get(dmnId);
current.proj = dmnId;


var link = ' <a href ="/' + projTable + '.do?sysparm_query=number%3D' + proj.getValue('number') + '">'+ proj.getValue('number') +'</a>';
var message = gs.getMessage("Project {0} has been created");
message = message.replace("{0}", link);


gs.addInfoMessage(message);


Shane J
Tera Guru

Still looking for a solution if anyone has one to offer up!