Client script returning [object Object] on field

ceraulo
Mega Guru

Hello!

I have Script Include shown below.

var CustomUtils = Class.create();
CustomUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    getCustom: function() {
        var location = this.getParameter('sysparm_location');
        var requested_by = this.getParameter('sysparm_requested_by');
        var u_recipient = this.getParameter('sysparm_recipient');
        var u_name = this.getParameter('sysparm_name');

        var obj = {};

        var gr = new GlideRecord("cmn_location");
        gr.get(location);

        var gr2 = new GlideRecord("sys_user");
        gr2.get(requested_by);
     
	obj.u_address = gr.u_address;

        if (u_recipient == 'true') 
            obj.u_custom_field = gr.name + "\n" + u_name + "\n" + gr.u_signature;
        else 
            obj.u_custom_field = gr.name + "\n" + gr2.name + "\n" + gr.u_signature;
        
        var json = new JSON();
        var data = json.encode(obj);

        return data;

    },

    type: 'CustomUtils'
});

 

This is my client script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (newValue === '') { //isLoading ||
        g_form.setValue('customf', '');
        g_form.setValue('address', '');
        return;
    }
    if (oldValue != newValue) {
        var ga = new GlideAjax('CustomUtils');
        ga.addParam('sysparm_name', 'getCustom');
        ga.addParam('sysparm_location', newValue);
        ga.addParam('sysparm_requested_by', g_form.getValue('requested_by'));
        ga.addParam('sysparm_recipient', g_form.getValue('u_recipient'));
	ga.addParam('sysparm_name', g_form.getValue('u_name'));
        ga.getXML(CustomDetails);
    }
}

function CustomDetails(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    answer = answer.evalJSON();

    g_form.setValue('address', answer.u_address);
    g_form.setValue('customf', answer.u_custom_field);
}

 

When the client script runs, the value of customf is correct BUT the value of address is [object Object]

What is causing this issue?

Please help!

Thank you.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@ceraulo 

use this

1) don't use eval JSON

Script Include:

var CustomUtils = Class.create();
CustomUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getCustom: function() {
		var location = this.getParameter('sysparm_location');
		var requested_by = this.getParameter('sysparm_requested_by');
		var u_recipient = this.getParameter('sysparm_recipient');
		var u_name = this.getParameter('sysparm_name');

		var obj = {};

		var gr = new GlideRecord("cmn_location");
		gr.get(location);

		var gr2 = new GlideRecord("sys_user");
		gr2.get(requested_by);

		obj.u_address = gr.u_address.toString();

		if (u_recipient == 'true') 
			obj.u_custom_field = gr.name.toString() + "\n" + u_name + "\n" + gr.u_signature.toString();
		else 
			obj.u_custom_field = gr.name.toString() + "\n" + gr2.name + "\n" + gr.u_signature.toString();

		return JSON.stringify(obj);

	},

	type: 'CustomUtils'
});

Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (newValue === '') { //isLoading ||
        g_form.setValue('customf', '');
        g_form.setValue('address', '');
        return;
    }
    if (oldValue != newValue) {
        var ga = new GlideAjax('CustomUtils');
        ga.addParam('sysparm_name', 'getCustom');
        ga.addParam('sysparm_location', newValue);
        ga.addParam('sysparm_requested_by', g_form.getValue('requested_by'));
        ga.addParam('sysparm_recipient', g_form.getValue('u_recipient'));
        ga.addParam('sysparm_name', g_form.getValue('u_name'));
        ga.getXML(CustomDetails);
    }
}

function CustomDetails(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    answer = JSON.parse(answer);

    g_form.setValue('address', answer.u_address);
    g_form.setValue('customf', answer.u_custom_field);
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

4 REPLIES 4

Sushma R1
Tera Expert

Instead

function CustomDetails(response) {

var answer = response.responseXML.documentElement.getAttribute("answer");

var vals= JSON.parse(answer);

g_form.setValue('address', vals.u_address);

g_form.setValue('customf', vals.u_custom_field);

}

 

at SI side use JSON.stringify(arrReturn);

Hi Sushma,

This did not work.
The client script triggers on 'On Change' but nothing happens.:-(

 

Did you change your SI a bit 

var CustomUtils = Class.create();

CustomUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getCustom: function() {

var location = this.getParameter('sysparm_location');

var requested_by = this.getParameter('sysparm_requested_by');

var u_recipient = this.getParameter('sysparm_recipient');

var u_name = this.getParameter('sysparm_name');

var arr= [];

var gr = new GlideRecord("cmn_location");

gr.get(location); // assuming this variable consists of sys_id

var gr2 = new GlideRecord("sys_user");

gr2.get(requested_by); // assuming this variable consists of sys_id

obj.u_address = gr.u_address;

if (u_recipient == 'true')

arr.push(gr.name + "\n" + u_name + "\n" + gr.u_signature);

else

arr.push(gr.name + "\n" + gr2.name + "\n" + gr.u_signature);

 

return arr.toString();

},

type: 'CustomUtils' });

Ankur Bawiskar
Tera Patron
Tera Patron

@ceraulo 

use this

1) don't use eval JSON

Script Include:

var CustomUtils = Class.create();
CustomUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getCustom: function() {
		var location = this.getParameter('sysparm_location');
		var requested_by = this.getParameter('sysparm_requested_by');
		var u_recipient = this.getParameter('sysparm_recipient');
		var u_name = this.getParameter('sysparm_name');

		var obj = {};

		var gr = new GlideRecord("cmn_location");
		gr.get(location);

		var gr2 = new GlideRecord("sys_user");
		gr2.get(requested_by);

		obj.u_address = gr.u_address.toString();

		if (u_recipient == 'true') 
			obj.u_custom_field = gr.name.toString() + "\n" + u_name + "\n" + gr.u_signature.toString();
		else 
			obj.u_custom_field = gr.name.toString() + "\n" + gr2.name + "\n" + gr.u_signature.toString();

		return JSON.stringify(obj);

	},

	type: 'CustomUtils'
});

Client Script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (newValue === '') { //isLoading ||
        g_form.setValue('customf', '');
        g_form.setValue('address', '');
        return;
    }
    if (oldValue != newValue) {
        var ga = new GlideAjax('CustomUtils');
        ga.addParam('sysparm_name', 'getCustom');
        ga.addParam('sysparm_location', newValue);
        ga.addParam('sysparm_requested_by', g_form.getValue('requested_by'));
        ga.addParam('sysparm_recipient', g_form.getValue('u_recipient'));
        ga.addParam('sysparm_name', g_form.getValue('u_name'));
        ga.getXML(CustomDetails);
    }
}

function CustomDetails(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    answer = JSON.parse(answer);

    g_form.setValue('address', answer.u_address);
    g_form.setValue('customf', answer.u_custom_field);
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader