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

zica
Giga Guru

Hi Richelle,



On top of the Venkat tips, I've also noticed one typo in your getServerAttribute: function() .


Into your if statement, there is a blank between the 'gr.' and 'get(sys_user)'


Whereas It should be attached



Kind regards,


ZA



Do not feel shy to mark helpful if it helps