
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2024 05:16 AM
Hi,
I have a requirement to autopopulate the cost center code, I am unable to populate it. Kindly help.
GlideAJAX
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var off = g_form.getValue('officer_name');
var ga = new GlideAjax('officerNameClass');
ga.addParam('sysparm_name', 'officerNameFunction');
ga.addParam('sysparm_id', off);
ga.getXML(callBackFunction);
function callBackFunction(response){
var answer = response.responseXML.documentElement.getAttribute('answer');
var op = JSON.parse(answer);
//g_form.addInfoMessage(op.costCenter);
g_form.setValue('user_id_officer', op.userid);
g_form.setValue('office_phone_officer', op.officePhone);
g_form.setValue('cost_center_officer', op.costCenter);
}
}
Script Include
var officerNameClass = Class.create();
officerNameClass.prototype = Object.extendsObject(AbstractAjaxProcessor, {
officerNameFunction: function() {
var officeKey = this.getParameter('sysparm_id');
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', officeKey);
gr.query();
if (gr.next()) {
var jsonObj = {};
jsonObj.userid = gr.getValue('user_name');
jsonObj.officePhone = gr.getValue('phone');
jsonObj.costCenter = gr.getValue(cost_center.code);
return JSON.stringify(jsonObj);
}
},
type: 'officerNameClass'
});
Regards
Suman P.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2024 05:24 AM
cost_center.code is undefined causing the script to not run. Try one of these 2 formats:
jsonObj.costCenter = gr.getValue('cost_center.code');
jsonObj.costCenter = gr.cost_center.code.toString();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2024 05:22 AM - edited 12-11-2024 05:25 AM
Hello @Community Alums ,
You can make a small change in your script include while fetching the cost center code add quotes inside the getValue like 'cost_center.code':
var officerNameClass = Class.create();
officerNameClass.prototype = Object.extendsObject(AbstractAjaxProcessor, {
officerNameFunction: function() {
var officeKey = this.getParameter('sysparm_id');
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', officeKey);
gr.query();
if (gr.next()) {
var jsonObj = {};
jsonObj.userid = gr.getValue('user_name');
jsonObj.officePhone = gr.getValue('phone');
jsonObj.costCenter = gr.getValue('cost_center.code');
return JSON.stringify(jsonObj);
}
},
type: 'officerNameClass'
});
Please mark my solution as Helpful and Accepted, if it works for you in any way!
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2024 05:24 AM
cost_center.code is undefined causing the script to not run. Try one of these 2 formats:
jsonObj.costCenter = gr.getValue('cost_center.code');
jsonObj.costCenter = gr.cost_center.code.toString();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2024 06:17 AM
Hi @Community Alums ,
jsonObj.costCenter = gr.getValue('cost_center.code');
If my response helped, please mark it as the accepted solution ✅ and give a thumbs up👍.
Thanks,
Anand

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2024 07:25 AM
This worked for me.
GlideAJAX
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var off = g_form.getValue('officer_name');
var ga = new GlideAjax('officerNameClass');
ga.addParam('sysparm_name', 'officerNameFunction');
ga.addParam('sysparm_id', off);
ga.getXML(callBackFunction);
function callBackFunction(response){
var answer = response.responseXML.documentElement.getAttribute('answer');
var op = JSON.parse(answer);
//g_form.addInfoMessage(op.costCenter);
g_form.setValue('user_id_officer', op.userid);
g_form.setValue('office_phone_officer', op.officePhone);
g_form.setValue('cost_center_officer', op.costCenter);
}
}
Script Include
var officerNameClass = Class.create();
officerNameClass.prototype = Object.extendsObject(AbstractAjaxProcessor, {
officerNameFunction: function() {
var officeKey = this.getParameter('sysparm_id');
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', officeKey);
gr.query();
if (gr.next()) {
var jsonObj = {};
jsonObj.userid = gr.getValue('user_name');
jsonObj.officePhone = gr.getValue('phone');
jsonObj.costCenter = gr.cost_center.code.toString();
return JSON.stringify(jsonObj);
}
},
type: 'officerNameClass'
});