The CreatorCon Call for Content is officially open! Get started here.

Trying to return user name using the background script , till is returning the null value

rahamathullahmr
Giga Expert

Hello Experts,

I have a requirement where I need to populate a   string field called "user" with the user's name   as given in caller_id of the form if the user is   VIP .

To achieve it , I tried to go ahead with the onchange script   (as it a type of onload with some conditions) to invoke the glide ajax methods.

But before I go ahead , I wanted to verify if I can check using the background script that for the particular sys_id, whether I am able to get the user_name attribute on checking if the user's VIP value is true.

so this is the background script I worte :

var gr = new GlideRecord('sys_user');

  gr.addQuery('sys_id','62826bf03710200044e0bfc8bcbe5df1');

                gr.query();

  if (gr.next()){

                                  if(gr.vip == true){

                                      return   gr.user_name;

                                      gs.print(gr.userName);

                            }else{

                                      return null;

                              }

                                           

                                }

The javascript compiler says invalid return .

Need your guidance on this.

Thanks,

Rahamathullah

1 ACCEPTED SOLUTION

var gr = new GlideRecord('sys_user');  


  gr.addQuery('sys_id','5b7c200d0a640069006b3845b5d0fa7c');  


                gr.query();  


  while(gr.next())


{  


                                  if(gr.vip == true)


{  


gs.print(gr.user_name);


                                    // return   gr.user_name;  


                                         


                            }


else{  


                                    // return null;  


                              }  


                                               


                                }  


Regards
Harish

View solution in original post

7 REPLIES 7

darius_koohmare
ServiceNow Employee
ServiceNow Employee

It will never reach the print statement if you put it after the return. It would need to go before the return.


still it says that invalid return


Steven1
Tera Expert
  1. var gr = new GlideRecord('sys_user');  
  2.   gr.addQuery('sys_id','62826bf03710200044e0bfc8bcbe5df1');  
  3.                 gr.query();  
  4.   if (gr.next()){  
  5.                                   if(gr.vip == true){  
  6.                                       gs.print(gr.user_name);  
  7.                             }else{  
  8.                                       return null;  
  9.                               }  
  10.                                                
  11.                                 }  

Hi, Sorry for the delayed response . This will not work for background script ,it will throw up the error saying "null value" . But the same can be used for ajax call . TO get the desired result,we actually need to use the function.