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

Ankur Bawiskar
Tera Patron
Tera Patron

@chandan2212 

the target field is reference or list type?

you are returning name of users

share some screenshots

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

target field is list collector :list  @Ankur Bawiskar 

@chandan2212 

then you should pass sysId and push array

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@chandan2212 

Would you mind closing your earlier questions by marking appropriate response as correct?

Members have invested their time and efforts in helping you.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader