Getting "undefined" while passing string data into another string field

venkysana
Tera Expert

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.

find_real_file.png

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);
	}

}
20 REPLIES 20

Try changing your query in the Script Include to this:

appGr.addEncodedQuery('sys_idLIKE' +num);
 
 

I tried but no luck.

Instead of populating related APM Number its populating one common number in APM number field. 

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.

While also not working, its returning empty value.

Hitoshi Ozawa
Giga Sage
Giga Sage

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".

find_real_file.png

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);
	}
}