transform map script is not working

chandan2212
Tera Contributor

HI all , 

 

 

For the single user it is goving correct answer but for the multiple users it is giving undefined answer

 

Can anyone correct this script :

 

source.u_owner_name:- multiple user id is coming ,

 

  return: undefined output is coming 

 

// Transform Map Script (Single reference field: return the first matched user sys_id)
answer = (function transformEntry(source) {
   var owners = source.u_owner_name;
   var userIds = [];
       var arr = owners.split(',');
    gs.log('TM Owners tokens=' + arr.length);
 

    for (var i = 0; i < arr.length; i++) {
        var userVal = (arr[i] || '').trim();
        if (!userVal) continue;
 

        var userGR = new GlideRecord('sys_user');
        userGR.addQuery('user_name', userVal);
        userGR.query();
 

        while (userGR.next()) {
         userIds.push(userGR.name);
         
          gs.log('test_123'+userIds);
            return userIds.join(',')  ;
        }
    }

    })(source);


1 ACCEPTED SOLUTION

Source: u_owner_name
Target: List Collector (sys_user)
Input format: user1,user2,user3


answer = (function transformEntry(source) {

if (!source.u_owner_name)
return '';

var owners = source.u_owner_name;
var userIds = [];
var arr = owners.split(',');

for (var i = 0; i < arr.length; i++) {
var userVal = (arr[i] || '').trim();
if (!userVal)
continue;

var userGR = new GlideRecord('sys_user');
userGR.addQuery('user_name', userVal); // change if using email or name
userGR.query();

if (userGR.next()) {
userIds.push(userGR.sys_id.toString());
}
}

// List Collector expects comma-separated sys_ids
return userIds.join(',');

})(source);

View solution in original post

8 REPLIES 8

Matthew Green2
Kilo Guru

Updated Script

This version:

  • Handles single or multiple users

  • Returns comma-separated sys_ids

  • Returns after all users are processed

answer = (function transformEntry(source) {

if (!source.u_owner_name)
return '';

var owners = source.u_owner_name;
var userIds = [];
var arr = owners.split(',');

gs.log('TM Owners tokens=' + arr.length);

for (var i = 0; i < arr.length; i++) {
var userVal = (arr[i] || '').trim();
if (!userVal)
continue;

var userGR = new GlideRecord('sys_user');
userGR.addQuery('user_name', userVal);
userGR.query();

if (userGR.next()) {
userIds.push(userGR.sys_id.toString());
gs.log('Matched user: ' + userVal);
}
}

return userIds.join(',');

})(source);

Remember If your target field is a single reference field, ServiceNow will only accept one sys_id.
If it’s a List field, comma-separated sys_ids are correct.

If you tell me:

  • Target field type (Reference vs List)

  • Source value format (username, email, name)

I can tailor this exactly to your use case.

@Matthew Green2  : list collector field type

Source: u_owner_name
Target: List Collector (sys_user)
Input format: user1,user2,user3


answer = (function transformEntry(source) {

if (!source.u_owner_name)
return '';

var owners = source.u_owner_name;
var userIds = [];
var arr = owners.split(',');

for (var i = 0; i < arr.length; i++) {
var userVal = (arr[i] || '').trim();
if (!userVal)
continue;

var userGR = new GlideRecord('sys_user');
userGR.addQuery('user_name', userVal); // change if using email or name
userGR.query();

if (userGR.next()) {
userIds.push(userGR.sys_id.toString());
}
}

// List Collector expects comma-separated sys_ids
return userIds.join(',');

})(source);