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.

Auto populate the values using glideajax

vinod6
Tera Contributor

I have selected  the requested for then auto populate the manager and country;

Please find the below one's.

vinod6_0-1702047106119.png

And i am using the below script

Script inculdes:

vinod6_1-1702047172928.png

Onchange client script:

vinod6_2-1702047224855.png

Script:

var GroupManager = Class.create();
GroupManager.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserInfo : function(){
var usr=this.getparameter('sysparm_user');
var gr=new GlideRecord('sys_user');
gr.addQuery('sys_id',usr);
gr.query();
if(gr.next())
{
    return gr.manager+ ',' +gr.location.country;
}
},
    type: 'GroupManager'
});
Ajax script
var ga =new GlideAjax('GroupManager');
   ga.addParam('sysparm_name','getUserInfo');
   ga.addParam('sysparm_user','newvalue');
   ga.getXML(pop);
   function callback(response)
   {
    var getresponse=response.responseXML.documentElement.getAttribute('answer');
    var answerAr=getresponse.split(',');
    alert(getresponse);
    g_form.setValue('manager',answerAr[0]);
g_form.setValue('country',answerAr[1]);
   }
   
4 REPLIES 4

Shamma Negi
Kilo Sage
Kilo Sage

Try using this script. Things which are in Bold are corrected

 

Script:

var GroupManager = Class.create();
GroupManager.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserInfo : function(){
var usr=this.getParameter('sysparm_user');
var gr=new GlideRecord('sys_user');
gr.addQuery('sys_id',usr);
gr.query();
if(gr.next())
{
    return gr.manager+ ',' +gr.location.country;
}
},
    type: 'GroupManager'
});
 
 
Ajax script
var ga =new GlideAjax('GroupManager');
   ga.addParam('sysparm_name','getUserInfo');
   ga.addParam('sysparm_user','newvalue');
   ga.getXML(pop);
 
 
   function pop(response)
   {
    var getresponse=response.responseXML.documentElement.getAttribute('answer');
    var answerAr=getresponse.split(',');
    alert(getresponse);
    g_form.setValue('manager',answerAr[0]);
g_form.setValue('country',answerAr[1]);
   }
   
Regards,Shamma Negi

Rajesh_Bhise
Tera Guru

Hello @vinod6,

 

Instead Glide Ajax and script include(BIG code), you can explicitly use client script(little code) to achieve this.

Write OnChang Client script on requested for variable.

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
 
var userid = g_form.getRefRecord('requested_for', userRef);
  function userRef(userid){
    g_form.setValue('manager', userid.manager.toString());
            g_form.setValue('country', userid.location.country.toString());
  }
}

 

 

 

Hope this finds your easy solution.

Please appreciate the efforts of community contributors by marking appropriate response as correct answer✔️ and helpful👍, this may help other community users to follow correct solution in future.

Happy to help you in future😎.

 

Thank you,
Rajesh

 

-O-
Kilo Patron

Is the version of the instance Utah, or newer?

If yes, you could use the Auto-populate feature of Catalog Variables.

Not a single line of code would be needed.

If not, your code should work with a couple of corrections:

  1. JavaScript is case sensitive, so getparameter is not the same as getParameter; therefor you need to correct

 

var usr = this.getparameter('sysparm_user');​

to

var usr = this.getParameter('sysparm_user');​

When you are unsure, AbstractAjaxProcessor is a plain regular Script Include, you could open it up and have a look.
As suggested previously by @Shamma Negi.

 

  • When setting up the sending of the selected user, you need to pass in the variable newvalue, not the string newvalue; therefor you need to correct

 

ga.addParam('sysparm_user', 'newvalue');​

to

ga.addParam('sysparm_user', newvalue);​

 

b.t.w. you need to mind case here too: the correct form - OOB is newValue not newvalue.

Community Alums
Not applicable

This looks like a catalog item to me and if that's so you can use Auto-Populate option in the Maitain Catalog Item form.

JoroKlifov1_0-1702062890099.png