Auto populate reference field based on other reference field

rickw1
Giga Contributor

Hi there,

I'm figuring out the best way to do this..

In my form, I have 2 fields: caller and hospital.

Caller refers to the users table, Hospital refers to the company table.

My goal is: whenever I populate the caller field, the system needs to lookup the hospital of that caller and needs to auto-populate that.

Example: Customer X is part of Hospital X. Customer X is the caller so will be populated in the caller field. The system recognizes that Customer X is part of Hospital X so hospital will be Hospital X.

customer_hospital.png

Hope you guys can help me out!

1 ACCEPTED SOLUTION

rickw1
Giga Contributor

We got it working!



Client script:


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


    if (isLoading || newValue == '') {


          return;


    }


   


    var ga = new GlideAjax('GetCustomerData');


  ga.addParam('sysparm_name','getFieldValue');


  ga.addParam('sysparm_field','u_reference_1');


  ga.addParam('sysparm_user_id', g_form.getValue('caller'));


      ga.getXML(DoSomething);


       


      function DoSomething(response) {


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


            g_form.setValue('company',company);


      }


}



Script include:


var GetCustomerData = Class.create();


GetCustomerData.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {


    getFieldValue: function() {


              var user = new GlideRecord('x_medt2_ihs_customers');


              user.get(this.getParameter('sysparm_user_id'));


              if(! user.sys_id){


                                return false;


              }


        return user[this.getParameter('sysparm_field')];


    }


});



Thanks everybody for the great help on this!


View solution in original post

58 REPLIES 58

g_form.getReference() is still not recommended because it returns the entire record rather than just the field you need.


This is OK at the start, but as you keep filling up your client scripts with getReference() your system will degrade and eventually become slow, resulting in end user complaints. Alas, it is not good practice to use getReference().




My recommendation is to create a re-usable AJAX script to get data from any given table, reducing the overhead of developing ajax scripts.


In my instances I have reduced GlideAjax calls like this to one line of client script code using these methods.



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Ok, it's truth that it returns the whole GlideRecord on that case, which it would be, depending on the data of the record, a little bit slower than the solution with AJAX script, which is definatelly the most efficient.



The solution you did on your instances is so elegant, could you share with us that "Universal script" of the place where you found it?



Thank you!


rickw1
Giga Contributor

We got it working!



Client script:


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


    if (isLoading || newValue == '') {


          return;


    }


   


    var ga = new GlideAjax('GetCustomerData');


  ga.addParam('sysparm_name','getFieldValue');


  ga.addParam('sysparm_field','u_reference_1');


  ga.addParam('sysparm_user_id', g_form.getValue('caller'));


      ga.getXML(DoSomething);


       


      function DoSomething(response) {


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


            g_form.setValue('company',company);


      }


}



Script include:


var GetCustomerData = Class.create();


GetCustomerData.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {


    getFieldValue: function() {


              var user = new GlideRecord('x_medt2_ihs_customers');


              user.get(this.getParameter('sysparm_user_id'));


              if(! user.sys_id){


                                return false;


              }


        return user[this.getParameter('sysparm_field')];


    }


});



Thanks everybody for the great help on this!


ewereer
Giga Contributor

Hi ,


I have a issue. I need to populate a reference field based on the location of the logged-in user.



ex- if the logged-in user is from X country ,the fulfillment group should populate Y.


          and if logged-in user is from A country, the fulfillment group should populate B.




Thanks.