- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2017 02:49 AM
I have custom department string field.(This is code is working fine for department as reference field but not for string field)I am having an issue populate department based on Caller selection
Below script returning department sys_id instead of this, i want to populate department Name
Client Script details:
Type: onChange
Table: Incident
Field Name: Caller
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
var caller = g_form.getReference('caller_id');
g_form.setValue('u_custom_department',caller.department);
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-09-2017 11:47 PM
Hi Rahul,
You have to write down script that return department name.Using GlideAjax you have to call it .On change of Caller you have write down the script .
Script Include:
var getDepartmentName = Class.create();
getDepartmentName.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getDept :function()
{
var name='';
var id = this.getParameter('sysparm_id');
var dep=new GlideRecord('cmn_department');
dep.addQuery('sys_id', id);
dep.query();
if(dep.next())
{
name=dep.name;
}
return name;
},
type: 'getDepartmentName'
});
Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
var caller = g_form.getReference('caller_id');
g_form.setValue('u_department',caller.department);
var graj=new GlideAjax('getDepartmentName');
graj.addParam('sysparm_name', 'getDept');
graj.addParam('sysparm_id', caller.department);
graj.getXML(HelloWorldParse);
function HelloWorldParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_custom_department', answer);
}
}
Please like, mark as correct/helpful if it is right.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2017 05:32 AM
Hi Rahul,
Please write the onChange Client Script as below. I have tested this in my personal instance and this is working also. Please use the code and confirm on the same.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var caller = g_form.getReference('caller_id', getDepartment);
}
function getDepartment(caller ) {
var dept = caller.department;
var deptName = "";
var deptRec = new GlideRecord("cmn_department");
deptRec.addQuery("sys_id", dept );
deptRec.query();
if(deptRec.next()){
deptName = deptRec.name;
g_form.setValue('u_custom_department',deptName );
}
}
I hope this helps.Please mark correct/helpful based on impact
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2017 01:27 AM
Thank you Amlan its working fine
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2017 01:36 AM
Hi Rahul,
The pleasure is mine. If it helps you,could you please mark the answer correct and close the thread?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-07-2017 04:47 AM
Sorry to reopen the same thread but as per best practices using GlideRecord at client side reduce the performance.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-07-2017 04:51 AM
As Per best practice Use GlideAjax for performing server side actions.
http://wiki.servicenow.com/index.php?title=GlideAjax#gsc.tab=0
Examples of asynchronous GlideAjax