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

@Ashish I agree however there was no mention of inbound emails here unless I've missed something this looks like user input on to a form.


(BP) Set Location to User


This is an OOB client script on incident table which sets user location. You can take a look at that as well to fulfill you requirement.


As Daryll pointed, client script will work on change of a caller field, will set hospital field.


Business rule will work after submission of record / saving of record.


Daryll Conway
Giga Guru

Another approach would be if you have the hospital referenced on the caller's table you could scrap the hospital field you currently have and replace with the caller.hospital field then it'll populate automatically when you select the caller


rickw1
Giga Contributor

Okay, so I used Ashish's BR which works when saving the form.


However, as I want to have this field populated by onChange(), I'm also going to create a client script. (Hopefully this is a good idea).



Unfortunately, my labels are not that intuitive as I'm not able to change them.


On the form:


Caller = caller


Hospital = company



In the customers table (extended from users):


Hospital = u_reference_1



Based on that, the script would look like this:



function onChange(control, oldValue, newValue, isLoading) {       var caller_id = g_form.getReference('caller', setHospital);   }   function setHospital(caller_id)   {       g_form.setValue('company', caller.u_reference_1); }  



Unfortunately, nothing happens when population the caller field


put in an alert to check you are getting the values you are expecting