- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2015 04:22 AM
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.
Hope you guys can help me out!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-21-2015 06:53 AM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2015 04:32 AM
Write a before BR for insert, if the caller field is readonly else also check update.
add a condition if caller changes.
Script:
current.u_hospital = current.caller.company;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2015 04:59 AM
Thanks Ashish, this works!
However, the value gets populated when I save the record instead of immediately after populating the caller field.
Can this be done by a BR as well?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2015 06:02 AM
As mentioned below BR only run on server side and need a trigger (e.g: a submit, save or load) to be actioned. A client script will be able to be triggered by actions on the form so this will give the live update you are looking for.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-19-2015 04:34 AM
1) You can do that with onChange client script with getReference and callback function.
http://wiki.servicenow.com/index.php?title=GlideForm_(g_form)#getReference
Your code should be like.
function onChange(control, oldValue, newValue, isLoading) {
var caller = g_form.getReference('caller_id', setHospital);
}
function setHospital(caller)
{
g_form.setValue('u_hosptial', caller.u_hospital); // I assume field name is u_hospital is field name of the form you want to populate as well as on the user table
}