- 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-20-2015 03:58 AM
Antonio Vegue Martinpintado wrote:
Ok! Look, you can do this as a temporary workaround at the moment:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
var gr = new GlideRecord('x_medt2_ihs_customers');
if(gr.get(newValue))
g_form.setValue('company',gr.u_reference_1);
else
alert('User is not a customer and does not have Hospital assigned');
}
The best solution for a good performance is, as Paul Morris and Pradeep Sharma said, create and Ajax script and call it from the client script. Check this link out http://wiki.servicenow.com/index.php?title=Client_Script_Best_Practices#Example:_Asynchronous_GlideA...
Did it work?
Unfortunately getting this error:
onChange script error: TypeError: GlideRecord is not a function function (){var o=i(m,arguments);return l.apply(n,o)}
It's still interesting that the BR is able to get the info very easily, but the CS doesn't.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-20-2015 04:06 AM
Umm strange, I used that script on my dev instance (changing table and field to set) and it works perfectly. Could you send us an screenshot of the cliend script that you defined please?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-20-2015 04:20 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-20-2015 04:39 AM
Hi Rick,
Are you creating this as part of scoped application. Please confirm.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-20-2015 04:40 AM
Pradeep Sharma wrote:
Hi Rick,
Are you creating this as part of scoped application. Please confirm.
Correct Pradeep