
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2020 05:52 PM
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
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2020 05:55 PM
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);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2020 05:55 PM
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);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2020 06:27 PM
Thanks Mike!
Was it the if statement or the gr.name giving me trouble? Both? Would it have worked with gr.name?
Daniel

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2020 06:35 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2024 09:00 AM
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!