Getting "undefined" while passing string data into another string field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2022 05:46 AM
Need to auto populate APM Number of issue (sn_grc_issue) form from entity field which is reference to profile (sn_grc_profile) table. I am getting "undefined" in APM Number field as shown in below.
I have written below script include and onload client script but no use. Please help me to achieve this.
Script Include:
var autopopulateentityAPMnumber = Class.create();
autopopulateentityAPMnumber.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getAPMnumber: function() {
var object = {};
var num = this.getParameter('sysparm_sys');
var appGr = new GlideRecord('sn_grc_profile');
appGr.addQuery('sys_id', num);
appGr.query();
if (appGr.next()) {
object.u_amp_number = appGr.u_amp_number.toString();
}
val = JSON.stringify(object);
return val;
},
type: 'autopopulateentityAPMnumber'
});
OnLoad Client Script:
function onLoad() {
var ga = new GlideAjax('autopopulateentityAPMnumber');
ga.addParam('sysparm_name', 'getAPMnumber');
ga.addParam('sysparm_sys', g_form.getValue('profile'));
ga.getXML(callBackFunction);
function callBackFunction(response){
var answer = response.responseXML.documentElement.getAttribute('answer');
var val = JSON.parse(answer);
g_form.setValue('u_amp_number', val.u_amp_number);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-11-2022 03:07 AM
Thanks for your reply,
Background script is working fine but onChange client script is not working.
Note: I have to auto populate APM number field while loading the form. Please provide me onLoad client script.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-11-2022 03:32 PM
Please try the following scripts. This should popup an alert with message 'OK'. If it doesn't it means Script Include is not being called.
Script Include
var autopopulateentityAPMnumber = Class.create();
autopopulateentityAPMnumber.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getAPMnumber: function() {
return 'OK';
},
type: 'autopopulateentityAPMnumber'
});
Client Script (onLoad)
function onLoad() {
var ga = new GlideAjax('autopopulateentityAPMnumber');
ga.addParam('sysparm_name', 'getAPMnumber');
ga.addParam('sysparm_sys', g_form.getValue('profile'));
ga.getXMLAnswer(callBackFunction);
function callBackFunction(answer){
alert(answer);
}
}
If the alert message pops up, please try the following scripts.
Script Include
var autopopulateentityAPMnumber = Class.create();
autopopulateentityAPMnumber.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getAPMnumber: function() {
var num = this.getParameter('sysparm_sys');
var appGr = new GlideRecord('sn_grc_profile');
if (appGr.get(num())) {
return appGr.u_amp_number.toString();
}
},
type: 'autopopulateentityAPMnumber'
});
Client Script (onLoad)
function onLoad() {
var ga = new GlideAjax('autopopulateentityAPMnumber');
ga.addParam('sysparm_name', 'getAPMnumber');
ga.addParam('sysparm_sys', g_form.getValue('profile'));
ga.getXMLAnswer(callBackFunction);
function callBackFunction(answer){
alert(answer);
if (answer.length > 0) {
g_form.setValue('u_amp_number', answer);
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-12-2022 05:27 AM
I am getting "Ok" in alert message for first script.
But I am getting null in alert for 2nd script include and also getting empty value in APM Number field.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2022 07:02 PM
My bad. There shouldn't be a "()" after num in the .get() statement.
var autopopulateentityAPMnumber = Class.create();
autopopulateentityAPMnumber.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getAPMnumber: function() {
var num = this.getParameter('sysparm_sys');
var appGr = new GlideRecord('sn_grc_profile');
if (appGr.get(num)) {
return appGr.u_amp_number.toString();
}
},
type: 'autopopulateentityAPMnumber'
});
If this doesn't work, try running the following script in Scripts - Background. Set "num" to be a sys_id of a record in sn_grc_profile table.
var num = '<sys_id of sn_grc_profile record>';
var appGr = new GlideRecord('sn_grc_profile');
if (appGr.get(num)) {
return appGr.u_amp_number.toString();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-11-2022 03:13 AM
why not just show the APM number field as Dot walked field from your Entity reference field?
then no need of any script
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader