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-10-2022 07:39 AM
Try changing your query in the Script Include to this:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2022 09:22 AM
I tried but no luck.
Instead of populating related APM Number its populating one common number in APM number field.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2022 09:37 AM
If it is now just giving one number, change the "if" to "while" as the "if" will stop at the first record it finds that matches.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-11-2022 02:53 AM
While also not working, its returning empty value.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-10-2022 07:18 PM
Hi Venkysana,
Not too sure but followings.
1. Client script is passing value from field "profile". Is the name of field "entity" "profile"?
>Need to auto populate APM Number of issue (sn_grc_issue) form from entity field
ga.addParam('sysparm_sys', g_form.getValue('profile'));
2. Reference field usually have a magnifying glass like below. There isn't one in field "Entity". Would want to see the variable definition of field "entity".
3. The query doesn't seem to returning any value. Try running the following script in Script Background to make sure it returns a value.
var num = '<sys_id of record in sn_grc_profile>'; // replace with valid sys_id
var appGr = new GlideRecord('sn_grc_profile');
appGr.addQuery('sys_id', num);
appGr.query();
if (appGr.next()) {
gs.info(appGr.u_amp_number.toString());
}
4. 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'
});
onChange() Client Script on field "entity"
function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '') {
return;
}
var ga = new GlideAjax('autopopulateentityAPMnumber');
ga.addParam('sysparm_name', 'getAPMnumber');
ga.addParam('sysparm_sys', newValue);
ga.getXMLAnswer(callBackFunction);
function callBackFunction(response){
g_form.setValue('u_amp_number', u_amp_number);
}
}