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

GlideFather
Tera Patron

Hi @chandan2212,

 

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;"???

_____
100 % GlideFather experience and 0 % generative AI

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

MadhanMaddy
Tera Contributor

Hi @chandan2212 ,

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?

 

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