Need help with a script to copy values in a field (glide_list)

Sergio26
Giga Guru

Hello,

I'm trying to create a script so I can copy certain values from a table X into a field of type glide_list in another table. Here are the details.

From the table 'resource_plan' I want to retrieve each 'User resource' value and copy it in the field 'Additional assigne' of the corresponding 'Task'.

For example:

find_real_file.png

find_real_file.png

 

Ideally I don't want to push more than once the same User resource in a corresponding Task.

Thank you

 

 

1 ACCEPTED SOLUTION

Sergio26
Giga Guru

Hello,

I didn't have time to reply before but I was able to create a script from a couple of posts including some of the ones mentionned here.

Still I was missing the part where the elements pushed in the array had to be copied in a glide list of another table. So here is my final script.

var rpQuery1 = 'u_tache_principale.active=true';
//var rpQuery2 = 'number=prjtask0107247';
var taskList = [];

var taskGR = new GlideRecord(table);
taskGR.addEncodedQuery(rpQuery1);
//taskGR.addEncodedQuery(rpQuery2);
taskGR.query();
gs.print('Task Count: ' + taskGR.getRowCount());
while(taskGR.next()) {
	
	gs.print('Task ID: ' + taskGR.sys_id);
	taskList.push(taskGR.sys_id.toString());
	
}

gs.print('Task List: ' + taskList);

for(var i=0; i<taskList.length; i++ ) {
	
	gs.print('Task ID: ' + taskList[i]);
	
	var userList = [];
	var arrayUtil = new ArrayUtil();
	
	var resPlan = new GlideRecord('resource_plan');
	resPlan.addQuery('task', taskList[i]);
	resPlan.addEncodedQuery('user_resourceNSAMEAStask.assigned_to');
	resPlan.query();
	gs.print('Count: ' + resPlan.getRowCount());
	while(resPlan.next()) {
		
		if(arrayUtil.contains(userList,resPlan.user_resource.sys_id.toString())) {
			continue;
		}
		
		userList.push(resPlan.user_resource.sys_id);
		
	}
	
	gs.print('User list: ' + userList);
	
	var getTask = new GlideRecord('task');
	getTask.get('sys_id', taskList[i]);
	gs.print('Task ID: ' + getTask.sys_id);
	getTask.additional_assignee_list = userList.join(',');
	getTask.update();
	
}

 

Thank you all for your time and inputs.

View solution in original post

5 REPLIES 5

jaloftas
Giga Contributor

The value of a glide list contain is string of comma separated sys_id's. Therefore you could write a script that loops through all your resource plans and builds up the string and uses the JavaScript indexOf to check that the sys_id isn't already present before adding it.

Ajaykumar1
Tera Guru

Hi Sergio,

Following lin may be helpful : How to assign multiple values to a list field

Regards,
Ajay

SatheeshKumar
Kilo Sage

you can achieve that by adding few additional lines of code in your script!!

 

Use array functions like unique,join,indexof !!

by using above three functions you can easily get desired behaviour , for more details on array function supported in servicenow refer the below link.

 

https://docs.servicenow.com/bundle/jakarta-application-development/page/app-store/dev_portal/API_ref...

 

 

check the below community thread -for an example:

 

https://community.servicenow.com/community?id=community_question&sys_id=ae0b0faddb5cdbc01dcaf3231f96...

 

Thanks,

satheesh

Chalan B L
Giga Guru

Hello Sergio,

 

Below code will work for you and i have tried in my personal instance.

//var parentGR = 'd33a0569dbc9e3007301f36f299619c8';
var tested = [];
var incidentGR = new GlideRecord('incident');
//incidentGR.addQuery('rfc',parentGR);
incidentGR.addQuery('parent',current.getUniqueValue());
incidentGR.query();
while(incidentGR.next()){

var caller = incidentGR.caller_id.getDisplayValue();

tested.push(caller);

}
//gs.print('<<inside>>'+tested);
current.work_notes_list = tested;