Using Ajax to pull a field value into a catalog item based on another field

richelle_pivec
Mega Guru

Using the script example Travis Toulson provided in this thread Query Catalog Item Variable, I have built out a client script and a script include for one of my catalog items. I have four fields that I want to fill in information from the User Table based on who is put in for the first field (person_impacted). One of them is a reference field, so getDisplayValue doesn't work...

I made the client script Type: onChange, and Variable Name: person_impacted.

Here's what I put in for the Client Script:

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

if(newValue == oldValue){

return;

}

        // If None is selected, clear the current attribute field  

        if (newValue == '') {  

                  g_form.setValue('person_impacted', '');  

                  return;  

        }

var ga = new GlideAjax('GetServerAttributeAjax');

ga.addParameter('sysparm_name','getServerAttribute');

ga.addParameter('sysparm_pi_cost_center', g_form.getValue('cost_center'));

ga.addParameter('sysparm_employee_number', g_form.getValue('user_name'));

ga.addParameter('sysparm_pi_job_code', g_form.getValue('u_job_code'));

ga.addParametere('sysparm_pi_title', g_form.getValue('title'));

ga.getXML(parseAttribResponse);            

function parseAttribResponse(response) {  

var answer = response.responseXML.documentElement.getAttribute('answer');  

g_form.setValue('person_impacted', answer);  

}

}

And here's what I put in for the Script include (Application: Global; Client callable: True):

var GetServerAttributeAjax = Class.create();  

     

GetServerAttributeAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {  

getServerAttribute: function() {  

var sysparm_name = this.getParameter('sysparm_name');

var cost_center = this.getParamter('sysparm_cost_center');  

var user_name = this.getParamter('sysparm_user_name');

var job_code = this.getParamter('sysparm_job_code');

var title = this.getParamter('sysparm_title');

var gr = new GlideRecord('sys_user');  

if (gr. get(sys_user)) {  

      return gr[cost_center].getDisplayValue();

                          }  

                          else {  

                                 

                                    return '';  

                          }  

                },  

                   

                type: "GetServerAttributeAjax"  

         

      });  

Alas, when I put a name in the Person Impacted field on the catalog item, now fields are auto-filling.

Any ideas on what I'm missing?

Thanks,

Richelle

5 REPLIES 5

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

HI Richelle,



Your code is wrong. Basically you need to pass the person_impacted value at the script include side and then do a query. In your code you are passing the fields for which you need to set the values.


Please check if by setting the default value solves your issue. Refer thread Auto populate service catalog variable with requesters information



If you are still blocked, share the form screenshot, I can help you out with the code.


Okay, I sort of have it working. Two questions.



1. How do you guys get the script to paste into threads with the numbers and the script formatting? and



2. How do I get the value to display and not the Sys_Id? I've tried several different things with no result...



Here's what I get now when I put a Name in the "Name of Person Impacted" field...


IUser.png




Person Impacted — Cost Center Client Script



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




if(newValue == oldValue){


return;


}


else{


var gp = new GlideRecord('sys_user');


var euser = g_form.getValue('person_impacted');


  • gp.addQuery('sys_id',euser);
  • gp.query();


if(gp.next()){


g_form.setValue('employee_number', gp.user_name);


g_form.setValue('pi_job_code', gp.u_job_code);


g_form.setValue('pi_title', gp.title);


  g_form.setValue('pi_cost_center', gp.cost_center);




var ga = new GlideAjax('PopulateCostCenter');


  • ga.addParam('sysparm_name','populateCostCenter');
  • ga.addParam('sysparm_person_impacted', euser);
  • ga.getXML(PopAppParse);


}


function PopAppParse(response){


    var answer = response.responseXML.documentElement.getAttribute("answer");



}


}


}



PopulateCostCenter Script Include



var PopulateCostCenter = Class.create();


  1. PopulateCostCenter.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    populateCostCenter: function() {


          var pi = this.getParameter('sysparm_person_impacted');


var gr = new GlideRecord('sys_user');  



          return sys_user;


          }


          });



thanks much,



Richelle


Hi Richelle,



I haven't gone through your whole code but however the script include should be


var PopulateCostCenter = Class.create();


PopulateCostCenter.prototype = Object.extendsObject(AbstractAjaxProcessor, {


    populateCostCenter: function() {


          var pi = this.getParameter('sysparm_person_impacted');


var gr = new GlideRecord('sys_user');


      gr.addQuery('sys_id',pi);


      gr.query();


      if(gr.next())


      {


  return gr.cost_center.getDisplayValue();    


      }



       


          }


          });



To answer your first question you have to click on advanced editor to have the script field.


venkatiyer1
Giga Guru

Hi Richelle,



First, we dont need sysparm_name and second i see e missing in parameter



getServerAttribute: function() {


var sysparm_name = this.getParameter('sysparm_name'); // dont need this line


var cost_center = this.getParamter('sysparm_cost_center');   // e missing


var user_name = this.getParamter('sysparm_user_name'); // e missing


var job_code = this.getParamter('sysparm_job_code'); // e missing


var title = this.getParamter('sysparm_title'); //e missing