- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-19-2016 05:08 PM
I have a record producer where Department , CostCenter is added. CostCenter is dependent on Department. All these values lies in cmn_department table.
i added a Catalog Client script to pickup the values But it shows sys_id instead of values since CostCenter is Reference field. Pls help how can i get the value of Cost Center ?
Catalog Client Script
OnChange
Department
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var dept_new = g_form.getValue(newValue);
var gr = new GlideRecord('cmn_department');
gr.addQuery('name', newValue);
gr.query();
if(gr.next())
{
//var cc_value = getRefernce(gr.cost_center);
var cc_value = gr.getDisplayBox("cost_center").value;
g_form.setValue('cost_centre_new', cc_value);
g_form.setValue('gl_code_new', gr.u_gl_code);
}
//Type appropriate comment here, and begin script below
}
when i use g_form.setValue('cost_center_new', gr.cost_centre); it gives me Sys_id
Pls Help
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-21-2016 06:48 AM
Pls try below script include code is working:-
var getDepartment = Class.create();
getDepartment.prototype = Object.extendsObject(AbstractAjaxProcessor, {
deptName:function() {
var val='';
var deptId = this.getParameter('sysparm_dept');
var gr = new GlideRecord('cmn_department');
gr.addQuery('sys_id',deptId);
gr.query();
if (gr.next())
{
val=gr.getDisplayValue('cost_center');
}
return val;
}
});
-------
Mark Correct if it solved your issue or hit Like and Helpful if you find my response worthy.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-19-2016 05:45 PM
Hi Venkat,
You can append getDisplayValue() to return the display value of the reference field. However this is not supported at client side, hence you have to make a GlideAjax call and then return the response. More info here.
http://wiki.servicenow.com/index.php?title=GlideAjax
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-19-2016 07:51 PM
Hi Pradeep, Thanks for the reply
I tired creating Glide Ajax but the value is returing as undefined. here is the script
catalog Client script
onChange
department
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
alert("Returing");
return;
}
var ga = new GlideAjax("getDepartment");
alert("Inside CS");
ga.addParam("sysparm_name", "deptName");
ga.addParam('sysparm_dept',newValue);
alert("newValue===>"+newValue);
ga.getXMLWait();
var result = ga.requestObject.responseXML.getElementsByTagName("result");
g_form.setValue('cost_center',result[0].getAttribute("cost_center"));
g_form.setValue('gl_code',result[0].getAttribute("u_gl_code"));
//Type appropriate comment here, and begin script below
}
Script include
Client Callable true
var getDepartment = Class.create();
getDepartment.prototype = Object.extendsObject(AbstractAjaxProcessor, {
deptName:function() {
var deptId = this.getParameter('sysparm_dept');
gs.log("deptId===>"+deptId);
var result = this.newItem("result");
var gr = new GlideRecord('cmn_department');
gr.addQuery('sys_id',deptId);
gr.query();
gs.log("Inside SI 1");
//if(gr.next()){
gs.log("Inside SI 2");
result.setAttribute('cost_center', gr.cost_center.getDisplayValue());
gs.log("Cost Center====>"+ gr.cost_center.getDisplayValue());
result.setAttribute('u_gl_code',gr.u_gl_code.getDisplayValue());
gs.log("GL Code====>"+ gr.u_gl_code);
// }
},
type: 'getDepartment'
});
It give output as below
Somewhere i'm doing bad , Can you pls help
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-19-2016 11:27 PM
Any help on this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-20-2016 05:34 AM
Hi Venkat,
Something like below should work.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
alert("Returing");
return;
}
var ga = new GlideAjax("getDepartment");
alert("Inside CS");
ga.addParam("sysparm_name", "deptName");
ga.addParam('sysparm_dept',newValue);
ga.getXML(ajaxResponse);
}
function ajaxResponse(serverResponse) {
var result = serverResponse.responseXML.getElementsByTagName("result");
g_form.setValue('cost_center',result.getAttribute("cost_center"));
g_form.setValue('gl_code',result.getAttribute("u_gl_code"));
}