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

Aditya,

To get name and email separately, use key/value instead of an array.

Script Include

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['name'] = incidentGR.name.toString();
            array['email'] = incidentGR.email.toString();
        }
        return JSON.stringify(array); // convertkey/value to string
    },
    type: 'TASK_return_array_SI'
});

Client Script

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) {
        answer = JSON.parse(answer); // parse answer
		g_form.setValue('description', answer.name + ',' + answer.email);
    }
}

Execution result

find_real_file.png

 

Hello @Hitoshi Ozawa ,

I want to fetch the choices of subcategory field form case table to the u_subcategory filed of my custom table u_order_support_rules but as you know we can't reference the choice table so what should I do to fetch the case.subcategory to the custom filed.

Any help appreciated .

 

Regards,

Alex

Kartik Sethi
Tera Guru
Tera Guru

Hi @Aditya Kumar 

 

You could follow the approach provided by @Ankur Bawiskar as JSON object is a better way to pass data.

As you mentioned that you want to continue with the array so there will be a few minor changes required in your script. 

 

Client Script:

  • getXMLAnswer instead of getXML. No difference except it required less code
  • You are always overwriting the description with the last element of the array
  • Script:
    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 desc = g_form.getValue('description');
    	if (answer) {
    		var userData = answer.split(',');
    		for (a = 0; a < userData.length; a++); {
    			desc += userData[a] + '\n';
    		}
    	}
    
    	//Set the final value in Description field
    	g_form.setValue('description', a);
    }

Script Include Method:

  • Ensure Script Include is Client Callable
  • returning array as a string using .join function
  • Script:
    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.join(',');
        },

 


Please mark my answer as correct if this solves your issues!

If it helped you in any way then please mark helpful!

 

Thanks and regards,

Kartik

the output is not as expected it might be something else cuz everything is right if look closely logically every approach is right but out is gibberish ....