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

Pradeep Sharma wrote:



Hi Rick,



You can achieve the above req with the help of dot-walking. Please go through the below link for more info.


http://wiki.servicenow.com/index.php?title=Dot-Walking#gsc.tab=0


Interesting to see is that .Caller --> User fields --> Hospital does not exist..


Would that explain these issues?


Hi Rick,



You mean that hospital field don't exist on the referenced table.


Can you please check if the field is available on the dictionary of the table.


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Rick,



Just an FYI :


Client scripting uses either data available on the client or data retrieved from the server. Use client data as much as possible to eliminate the need for time-consuming server lookups. The top ways to get information from the server are g_scratchpad, and asynchronous GlideAjax lookup.



The primary difference between these methods is that g_scratchpad is sent once when a form is loaded (information is pushed from the server to the client), whereas GlideAjax is dynamically triggered when the client requests information from the server.



Other methods, GlideRecord and g_form.getReference() are also available for retrieving server information. However, these methods are no longer recommended due to their performance impact. Both methods retrieve all fields in the requested GlideRecord when most cases only require one field.



http://wiki.servicenow.com/index.php?title=Client_Script_Best_Practices#gsc.tab=0


Hi Rick, Pradeep



Im afraid that in this case, use of g_scratchpad is no a valid solution since when you are creating the incident you don't know who the caller is an so that information cannot be retrieved from the database till the agent is filling field caller.



Anyway, It's not completely truth that using g_form.getReference() is not recommended, not if you use them within a callback function (asynchronous). It is not recommended to use it syncronously, but in the example I gave to Rick, good practices are met (callback function).



Regards,


Antonio.