Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

transform script is not working

Not applicable

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

@Community Alums 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

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

Not applicable

@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);

@Community Alums 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.