unable to set value in reference field via script on catalog form
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2022 07:03 PM
Hi,
below is the script that i written to auto-populate based on user i selected. but value not string in the form but i am able to find in script log's
var User = Class.create();
User.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCost: function() {
var id1 = this.getParameter('sysparm_userID');
var obj1 = {};
var gr1 = new GlideRecord('sys_user');
gr1.addQuery('sys_id', id1);
gr1.query();
if (gr1.next()) {
obj1.cost = gr1.u_cost_center.getDisplayValue();
gs.log('cost' + obj1.cost);
return JSON.stringify(obj1);
}
return '';
},
type: 'User'
});
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('User');
ga.addParam('sysparm_name', 'getCost');
ga.addParam('sysparm_userID', newValue);
ga.getXML(updateCampus);
function updateCampus(response) {
var info = response.responseXML.documentElement.getAttribute("answer");
var result = JSON.parse(info);
g_form.setValue('cost_center', result.cost);
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2022 08:08 PM
Please check if you script include is marked client callable.
Also change the script include name to something else. User is not an appropriate name.
You also mayn't need to covert you data to JSON in script include while returning. You can return it as string as use it.
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-19-2022 06:05 AM - edited 10-19-2022 06:06 AM
can you specify more about this? Your code looks fine.
try to replace
obj1.cost = gr1.u_cost_center.getDisplayValue(); with gr1.u_cost_center or gr1.getDisplayValue('u_cost_center')
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-19-2022 07:18 AM
Hi Lakshmi ,
I tried in my PDI. It worked. I have used different approach. I am population the cost center in the incident table on the field correlation id. Just for testing.
Please the script below.
Client Script: On Change based on the caller ID field.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var grcaller = g_form.getValue('caller_id');
var gr = new GlideAjax('CostCentre');
gr.addParam('sysparm_name','getcostCenter');
gr.addParam('sysparm_user',grcaller);
gr.getXML(callback);
function callback(response){
var answer = response.responseXML.documentElement.getAttribute('answer');
alert(answer);
g_form.setValue('correlation_id',answer);// setting the field here.
}
}
Script Include:
var CostCentre = Class.create();
CostCentre.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getcostCenter: function() {
var gruser = this.getParameter('sysparm_user');
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', gruser);
gr.query();
if (gr.next()) {
var grcost = gr.getDisplayValue('cost_center');
}
return grcost;
},
type: 'CostCentre'
});
Screenshot: