The CreatorCon Call for Content is officially open! Get started here.

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

cp11
ServiceNow Employee
ServiceNow Employee

Is the line below returning any records - can you log that out - possibly that is causing the issue?

Also add an alert statement on your client script to alert(val) - what do you see there?

If they are both returning values then try setting form value as val.u_amp_number.toString()

appGr.addQuery('sys_id', num);

appGr.query();

add an alert statement on your client script to alert(val) - what do you see there?

Its returning object Object in alert.

If they are both returning values then try setting form value as val.u_amp_number.toString()

Tried this as well but no luck.

 

Please suggest any other Idea.

Ian Mildon
Tera Guru

You already have the "val.u_amp_number" defined in your Script Include and are passing the stringified value as "val".

So the g_form.setValue line in your Client Script is (as shown) trying to extract "u_amp_number" from "u_amp_number" which is undefined.

Try using just g_form.setValue('u_amp_number', val);

Hi Mildon,

It is returning empty value. Please suggest another if possible