Script Issue

Pankaj kr
Kilo Sage

Hi,

 

I am trying to get the unique gliderecords based on client field using below script but it is not working. Can anyone help me here?

 

 

var gr = new GlideRecord('u_client_role_new');
        gr.addEncodedQuery('u_module=Tooling Electronics^u_department=PUR (PURCHASING)^u_bu=Electronics^u_client!=undefined');
        gr.orderBy('u_client_role');
        gr.query();

        gr.next();
        var sys_ids = gr.sys_id;
        gs.print(sys_ids);
        var client = gr.u_client_role;
var client1=client;
        gs.print(client);
        while(gr.next()) {
            //gs.print(gr.u_client_role);
            gs.print(client1+' vs '+gr.u_client_role);
    
        }

        

 

When i ran this script i am getting the result like below-

Pankakkumar_0-1703247536007.png

Here you can see that i am not re-assigning value of client but it is getting automatically re-assigned. I tried to do similar thing in browser console but autoreassigment in not happening there but in servicenow this re-assignment is happening automatically.

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

This is the old memory location issue where your the use of gr.field_name for a variable assignment is assigning the value as the memory location of the record instead of the text string you are expecting.  To avoid this use getValue instead:

var gr = new GlideRecord('u_client_role_new');
gr.addEncodedQuery('u_module=Tooling Electronics^u_department=PUR (PURCHASING)^u_bu=Electronics^u_client!=undefined');
gr.orderBy('u_client_role');
gr.query();
gr.next();
var sys_ids = gr.getValue('sys_id');
gs.print(sys_ids);
var client = gr.getValue('u_client_role');
var client1=client;
gs.print(client);
while (gr.next()) {
    gs.print(client1+' vs '+gr.getValue('u_client_role'));
}

This post does a good job of explaining the issue:

https://www.servicenow.com/community/developer-blog/tnt-the-importance-of-using-quot-getvalue-quot-w... 

View solution in original post

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

This is the old memory location issue where your the use of gr.field_name for a variable assignment is assigning the value as the memory location of the record instead of the text string you are expecting.  To avoid this use getValue instead:

var gr = new GlideRecord('u_client_role_new');
gr.addEncodedQuery('u_module=Tooling Electronics^u_department=PUR (PURCHASING)^u_bu=Electronics^u_client!=undefined');
gr.orderBy('u_client_role');
gr.query();
gr.next();
var sys_ids = gr.getValue('sys_id');
gs.print(sys_ids);
var client = gr.getValue('u_client_role');
var client1=client;
gs.print(client);
while (gr.next()) {
    gs.print(client1+' vs '+gr.getValue('u_client_role'));
}

This post does a good job of explaining the issue:

https://www.servicenow.com/community/developer-blog/tnt-the-importance-of-using-quot-getvalue-quot-w... 

Thanks for the solution.

You are welcome!