We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

transform script is not working

chandan2212
Tera Contributor

Hi All, 

 

Could you please help me with the transform map script so that it works properly

 

 

 
answer = (function transformEntry(source) {
    var owners = source.u_owner_name || '';  
    if (!owners) {
        gs.info('TM Owners empty');
        return '';
    }

    var arr = owners.split(',');
    gs.info('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.setLimit(1);
        userGR.query();

        if (userGR.next()) {
            gs.info('TM Matched user_name=' + userVal + ' -> ' + userGR.getDisplayValue());
           
            return userGR.name;
        } else {
            gs.info('TM No match for user_name=' + userVal);
        }
    }

   

    })(source);
1 ACCEPTED SOLUTION

@chandan2212 Could you please try the following.

 

answer = (function transformEntry(source) {
    var owners = source.u_owner_name+'' || ' ';  
    if (!owners) {
        gs.info('TM Owners empty');
        return '';
    }
 

    var arr = owners.split(',');
    gs.info('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.setLimit(1);
        userGR.query();
 

        if (userGR.next()) {
            gs.info('TM Matched user_name=' + userVal + ' -> ' + userGR.getDisplayValue());
           
            return userGR.name.toString();
        } else {
            gs.info('TM No match for user_name=' + userVal);
        }
    }
 

   
 

    })(source);

 

Here I have replaced the line return userGR.name; with return userGR.name.toString(); ideally the string conversion should force the value to be retained.

 

Hope this helps.

View solution in original post

7 REPLIES 7

Sandeep Rajput
Tera Patron

@chandan2212 Tried running your script in background scripts and it runs just fine. Any specific issues you are facing with this script?

@Sandeep Rajput      this script not working when i am putting in the source transform map script, it is giving undefined return

 

 

answer = (function transformEntry(source) {
    var owners = source.u_owner_name || ' ';  
    if (!owners) {
        gs.info('TM Owners empty');
        return '';
    }

 

    var arr = owners.split(',');
    gs.info('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.setLimit(1);
        userGR.query();

 

        if (userGR.next()) {
            gs.info('TM Matched user_name=' + userVal + ' -> ' + userGR.getDisplayValue());
           
            return userGR.name;
        } else {
            gs.info('TM No match for user_name=' + userVal);
        }
    }

 

   

 

    })(source);

@chandan2212 Could you please try the following.

 

answer = (function transformEntry(source) {
    var owners = source.u_owner_name+'' || ' ';  
    if (!owners) {
        gs.info('TM Owners empty');
        return '';
    }
 

    var arr = owners.split(',');
    gs.info('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.setLimit(1);
        userGR.query();
 

        if (userGR.next()) {
            gs.info('TM Matched user_name=' + userVal + ' -> ' + userGR.getDisplayValue());
           
            return userGR.name.toString();
        } else {
            gs.info('TM No match for user_name=' + userVal);
        }
    }
 

   
 

    })(source);

 

Here I have replaced the line return userGR.name; with return userGR.name.toString(); ideally the string conversion should force the value to be retained.

 

Hope this helps.