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

GlideFather
Tera Patron

Hi @Community Alums,

 

can you possibly explain what's wrong with your code? it's not executed or the result is not staisfactory? It needs some context to give you meaningful response, pasting a random code is not sufficient.... :((

 

 

EDIT:

And what is supposed to be the || '' part in

var owners = source.u_owner_name || '';  

 

What do the logs give you? Shouldn't it be just "var owners = source.u_owner_name;"???

_____
Answers generated by GlideFather. Check for accuracy.

Not applicable

@GlideFather  In this line :var owners = source.u_owner_name ; multple userid will come and script is giving undefined output

MadhanMaddy
Mega Guru

Hi @Community Alums ,

Can you please let me know what you're getting once you're loading or running this data (I mean which log/info message the script is populating or it's not running at all). 

Also, If you could share a sample data/obj of how the owners array/object is going to look like it'll be easy for me to test more & provide you with a fix soon. 

where are you writing this script? Is it in the transform map run script or transform script(before, after..) or field mapping source script?

 

Not applicable

@MadhanMaddy  :this script is giving undefined output till this line     gs.info('TM Owners tokens=' + arr.length); it is working but it is not going to GlideRecord

 

 

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