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

Can you give me example of the source email field? Is it comma separated?

Hello,

Yes it is comma separated,  But the below code doesn't work for me. Could you please help me where I went wrong.

answer = (function transformEntry(source) {

    // Add your code here
    var userNames = source.u_contact.split(',');
    var userSysIds = [];

    for (var i = 0; i < userNames.length; i++) {
        var gr = new GlideRecord("sys_user");
        gr.addQuery("email", userNames[i]);
        gr.query();

        if (gr.next()) {
            userSysIds.push(gr.sys_id);
        }
    }
    return userSysIds;
})(source);

Community Alums
Not applicable

Have you got solution for this? If yes then please share with me

Community Alums
Not applicable

Can you reply on Ruthika0319