I want to return Array from script include and use it in the glide ajax of client script what's wrong in my approach...

Aditya Kumar
Kilo Guru

client script

    var ga = new GlideAjax('TASK_return_array_SI');
    ga.addParam('sysparm_name', 'getCallerDetails');
    ga.addParam('sysparm_inc', g_form.getValue('caller_id'));
    ga.getXML(showDetails);

    function showDetails(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        for (a = 0; a < answer.length; a++); {
           g_form.setValue('description',a);
        }
    }

script include

	getCallerDetails: function() {
        var caller_sysID = this.getParameter('sysparm_inc');
        var incidentGR = new GlideRecord('sys_user');
        incidentGR.addquery('sys_id', caller_sysID);
        incidentGR.query();
		var array = [];
        if (incidentGR.next()) {
			array.push(incidentGR.name.toString());
			array.push(incidentGR.email.toString());
        }
        return array;
    },
	
1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi again,

This will probably be a repeat of what others have already replied. Added further information on why the original script is not working.

Since ServiceNow is only capable of returning a string, it's is now necessary to convert object to string in Script Include so the original script will become like below as others have already replied.

var TASK_return_array_SI = Class.create();
TASK_return_array_SI.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getCallerDetails: function() {
        var caller_sysID = this.getParameter('sysparm_inc');
        var incidentGR = new GlideRecord('sys_user');
        incidentGR.addquery('sys_id', caller_sysID);
        incidentGR.query();
        var array = [];
        if (incidentGR.next()) {
            array.push(incidentGR.name.toString());
            array.push(incidentGR.email.toString());
        }
        return JSON.stringify(array); // convert array to string
    },
    type: 'TASK_return_array_SI'
});

Since there is no longer any benefit of using getXML over getXMLAnswer, I've converted to use getXMLAnswer to avoid xml parsing.

I'm parsing the returned string to get an object back using JSON.parse.

In the original code in the question, there is a semi-colon ";" in the for loop. This will end the for loop with this 1 statement instead of processing the intended g_form.setValue() statement. The value of variable "a" will also become 2 because the loop has ended. Since there is no element a[2], the value of field "description" will become "undefined".

for (a = 0; a < answer.length; a++); {

I've removed this semi-colon to loop. The setValue, however, will over-write the previously set value so the displayed value will be email address only.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var ga = new GlideAjax('TASK_return_array_SI');
    ga.addParam('sysparm_name', 'getCallerDetails');
    ga.addParam('sysparm_inc', g_form.getValue('caller_id'));
    ga.getXMLAnswer(showDetails);

    function showDetails(answer) {
        //var answer = response.responseXML.documentElement.getAttribute("answer");
        answer = JSON.parse(answer); // parse answer
        for (var a = 0; a < answer.length; a++) { // remove ";"
            g_form.setValue('description', answer[a]);
        }
        //g_form.setValue('description', answer.join(', '));  // to display both name and email
    }
}

Execution result

 

View solution in original post

18 REPLIES 18

Raghu Ram Y
Kilo Sage

Hi,

Why you are using an array here? you are passing only one caller id,, so obviously you will get only one record..

so do like below and test it.

Script Include

getCallerDetails: function() {
        var caller_sysID = this.getParameter('sysparm_inc');
        var incidentGR = new GlideRecord('sys_user');
        incidentGR.addquery('sys_id', caller_sysID);
        incidentGR.query();
        if (incidentGR.next()) {
        var nm = "Name : " +incidentGR.name + "Email "+incidentGR.email; 
        }
        return nm;
    },
	

 

Client script

 var ga = new GlideAjax('TASK_return_array_SI');
    ga.addParam('sysparm_name', 'getCallerDetails');
    ga.addParam('sysparm_inc', g_form.getValue('caller_id'));
    ga.getXML(showDetails);

    function showDetails(response) {
        var gt = response.responseXML.documentElement.getAttribute("answer");
           g_form.setValue('description',gt);
        }
    }

no it's for the learning purpose... i know i can do it with JSON and concatenation but using an array is also an option right? but i got no used case for that

Hi,

See.. a simple question..

From script include you will get the name and email right?

and in client script there is only one field.. ok? if you run a loop.. what it do? if you have mutiple records then you do..

If my answer helps you, please mark it as correct/helpful.

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

I would suggest to avoid array

Instead use json object and return that

Then parse it at client side

getCallerDetails: function() {
        var caller_sysID = this.getParameter('sysparm_inc');
        var incidentGR = new GlideRecord('sys_user');
        incidentGR.addquery('sys_id', caller_sysID);
        incidentGR.query();
		var obj = {};
        if (incidentGR.next()) {
			obj.name = incidentGR.name.toString();
			obj.email = incidentGR.email.toString();
        }
        return JSON.stringify(obj);
    },

Client script

var ga = new GlideAjax('TASK_return_array_SI');
ga.addParam('sysparm_name', 'getCallerDetails');
ga.addParam('sysparm_inc', g_form.getValue('caller_id'));
ga.getXML(showDetails);

function showDetails(response) {
	var answer = response.responseXML.documentElement.getAttribute("answer");
	var parser = JSON.parse(answer);
	var name = parser[name];
	var email = parser[email];
	g_form.setValue('description', name + ' ' + email);

}

Regards
Ankur

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