Transform map to set multiple values in a list field

Steven Watts2
Tera Guru

Hi,

I'm trying to set multiple values in a list field via a transform map. The field is referencing the sys_user table.

function setApproverV2() {
	var input_array = source.u_approver.split(',');

	for (var i = 0; i < input_array; i++) {
		var getUser = new GlideRecord('sys_user');
		getUser.addActiveQuery();
		getUser.addQuery('user_name', input_array[i]);
		getUser.query();
		while (getUser.next()) {
			target.u_approver = getUser.sys_id;
		}
	}
}

The outcome I'm trying to achieve with the script is to loop through the list from the source file, separate the values in the Approver column, find the user records in the sys_user table and then set target value with sys_id of the users.

 

Anyone see where i'm going wrong?

4 REPLIES 4

Peter Bodelier
Giga Sage

Hi @Steven Watts2,

 

You are always setting it to the one value in the while loop.

Try this:

function setApproverV2() {
	var input_array = source.u_approver.split(',');
var approvers = [];
	for (var i = 0; i < input_array; i++) {
		var getUser = new GlideRecord('sys_user');
		getUser.addActiveQuery();
		getUser.addQuery('user_name', input_array[i]);
		getUser.query();
		if (getUser.next()) {
			approvers.push(getUser.getUniqueValue());
		}
	}
target.u_approver = approvers.toString();
}

Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Hi Peter,

 

Thanks for the assistance. I've just tried this and unfortunately when the records are created no value is being populated in the Approver list field. I'll add some logging statements to check the values being stored in 'approvers'.

Peter Bodelier
Giga Sage

Hi @Steven Watts2,

Let me know if you need anymore help. If so, please add a excert from your import data, in order to help you better.


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Ankur Bawiskar
Tera Patron
Tera Patron

@Steven Watts2 

try script shared by Peter once and share the update

 

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