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.

Transform Map Script changing email address to user name

dwilborn
Tera Contributor

Hi all,

I'm very new to coding in ServiceNow so I'm sure this is a rudimentary ask but I have the following scenario.

I have an import table u_sample_import with a field which holds primary user emails u_primary_user_email.  I'm attempting to use this value to set 'assigned_to' in a transform map.  In order to do this I need to query the sys_user table and get matching records based on the 'sys_user.email' field. 

Once those matching records are found I need to get the data from sys_user.name and set it as the value of the field in the target table.

I'm running this from a transform map field script on create.

 

answer = (function transformEntry(source) {
	
	var gr = new GlidRecord('sys_user');
	var userName = '';
	
	gr.addQuery('email', source.u_primary_user_email);
	gr.query();
	
	userName = gr.name;
	
	return userName; // return the value to be put into the target field
	
})(source);

 

Am I even in the ballpark?

Thanks,


Daniel

1 ACCEPTED SOLUTION

Mike Patel
Tera Sage

try below

answer = (function transformEntry(source) {
	var userName = '';
	var gr = new GlideRecord('sys_user');
	gr.addQuery('email', source.u_primary_user_email);
	gr.query();
	if(gr.next()){
		userName = gr.sys_id;
	}
	return userName; // return the value to be put into the target field
})(source);

View solution in original post

8 REPLIES 8

Mike Patel
Tera Sage

try below

answer = (function transformEntry(source) {
	var userName = '';
	var gr = new GlideRecord('sys_user');
	gr.addQuery('email', source.u_primary_user_email);
	gr.query();
	if(gr.next()){
		userName = gr.sys_id;
	}
	return userName; // return the value to be put into the target field
})(source);

Thanks Mike!

Was it the if statement or the gr.name giving me trouble?  Both?  Would it have worked with gr.name?

Daniel

few things you

  • missed e in GlideRecord 
  • assigned to is reference field so it needs sys_id of record not name so gr.name will not work.
  • using if condition is best practice.

Ruthika0319
Tera Contributor

Hello All,

The above information is very helpful to me,

I just want to know if I more than one email id how can I able to map that to target field using the script.

 

Thank you!