Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

Transfrorm Map - Field Map script or Transform Map script

OluseyiS3924078
Tera Contributor

I have been tasked with modifying a Transform Map. specifically one of the fields
Source field - emp-id - Managed By (alm_hardware)   .

 

i am assuming it should do some kind of  lookup on the sys_user table, where the emp-id and the employee id matches, and then populate the Managed by field with the corresponding name

i have tried using the following script.
answer = (function transformEntry(source) {
var userSysId = '';
var employeeId = source.u_emp_id; // Replace u_emp_id with your actual source column name

if (employeeId) {
var userGR = new GlideRecord('sys_user');
if (userGR.get('employee_number', employeeId)) { // Assuming employee ID is in employee_number field
userSysId = userGR.getUniqueValue();
}
}
return userSysId;
})(source);

But the managed_by field is populated with 'No details'.  Any hints on what i need to tweak in the code

5 REPLIES 5

Vaibhav Chouhan
Tera Guru

I took a look at your script, and the main issue is likely that it’s not actually finding a matching user record, so the answer ends up empty, which is why you’re seeing “No details”.

First, double-check that your source field name is correct. Make sure source.u_emp_id is exactly the column name in your import set table.

Next, check the actual employee number values that you are importing. Sometimes there are formatting differences like leading zeros or extra spaces. It’s a good idea to trim the value before querying, for example employeeId = employeeId.trim(), and temporarily add a gs.log to print out the employeeId so you can confirm what the script is really receiving during transform.

That said, you probably don’t need a script at all for this. A cleaner approach is to use the Reference value field name option on the field map. Set the source field to your emp-id, the target to managed_by, and in Reference value field name put employee_number. ServiceNow will automatically look up the correct user and populate the reference for you.

You can follow the official documentation on creating a field map and using the reference value field name here: Create a field map • Zurich Data and Automation • Docs | ServiceNow 

svirkar420
Tera Guru

Hi @OluseyiS3924078 , there are syntax and logical errors in the code, here is a the code, can you try using this,

(function transformEntry(source) {

var employeeId = (source.u_emp_id || '').toString().trim();

if (!employeeId) {
answer = '';
return;
}

var userGR = new GlideRecord('sys_user');
userGR.addQuery('employee_number', employeeId);
userGR.setLimit(1);
userGR.query();

if (userGR.next()) {
answer = userGR.getUniqueValue();
} else {
answer = '';
// Optional logging
gs.warn('Transform: No user found for employee ID: ' + employeeId);
}

})(source);

 

Just make sure that the field names are correct, let me know if this works.

 

If this response helped resolve your issue, please consider marking it as Accepted Solution and giving it a 👍.
This helps others in the community find useful answers more easily.

Regards,
Saurabh V.

Hi @svirkar420 , for this line
var employeeId = (source.u_emp_id || '').toString().trim();  i don't see any opening double quotes.  

OluseyiS3924078
Tera Contributor

I tried that approach and set the reference field to employee_number, and it did not work. By 'not work' i mean it not pulling the data. i queried the sys_user table with one of the employee numbers from the source column and the record does exist.