Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

return cannot be used in background script


Regards
Harish

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

Rajesh T
Giga Expert

Try using onChange client script ..



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


    if (isLoading) {


          return;


    }



var caller=g_form.getReference('caller_id',getUserCheck);


}


function getUserCheck(caller){


  if(caller)


  {


  var uname= caller.user_name;


                var vp=caller.vip;


  //alert(uname);


                //alert(vp);


                if(vp=='true')


  {


  g_form.setValue('u_user',uname);


                }


  }





  }