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

no its for the learning pupose... i know i can do it with json and concatination but using array is also option right ? but i got no used case for that

Hi,

you can iterate the array in client side and then do whatever processing you wish to do

what's the challenge?

in your case name would be in 1st position in array i.e. answer[0] and email in answer[1]

Remember you need to split() the string with , to get the array and then use the array elements

 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");
        answer = answer.toString().split(',');
        for (a = 0; a < answer.length; a++); {
           g_form.setValue('description',a);
        }
    }

Regards
Ankur

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

@Aditya Kumar 

Hope you are doing good.

Did my reply answer your question?

If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.

Regards
Ankur

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

Aman Kumar S
Kilo Patron

even array should be fine, essentially you are passing on the string value, but the use of for loop is incorrect

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

g_form.setValue('description',a);// This will be called twice and set the value twice, so most probably it will be storing email in the description

}

 

Instead you should be doing something like

g_form.setValue('description', a[0] + "  " +a[1] );

 

Feel free to mark correct, If I answered your query.

Will be helpful for future visitors looking for similar questions 🙂

Best Regards
Aman Kumar

Hey,

Didn't hear back on this.

Is your issue resolved? If yes, feel free to mark helpful/correct, so it will be helpful for others looking for similar query.

Best Regards
Aman Kumar