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

what type of field is it from.. what type of field is it going to...


They're both 'List' type fields.


Please use below in your business rule on idea table



(function executeRule(current, previous /*null when async*/) {



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();


}



}



// Add your code here



})(current, previous);



Regards,


Sachin


So here's what I have, but right now I'm only getting the last entry in the array coming over (original has 'North America, Europe', the new Project record only has 'Europe').




var reg = current.u_region.getDisplayValue();


var regarray = reg.split(",");


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


var gotreg = regarray[i];


}



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


var proj = new GlideRecord(projTable);


proj.setValue("u_regions", gotreg);



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


//current.stage = 'demand';


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);


I think we were overthinking this, simply doing this works   :





var reg = current.u_region.getDisplayValue();



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


var proj = new GlideRecord(projTable);


proj.setValue("u_regions", reg);



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


//current.stage = 'demand';


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);