- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā09-29-2020 06:14 PM
I understood GlideAjax to return single values as very simple and getXMLAnswer() is very easy to use. To return multiple values or object or array what is recommended for someone to start learn and practice from the beginning if never tried?
I find two approaches one with XML (newItem method) and other JSON.encode/parse. Appreciate any recommendation on what approach to practice or any scenarios that both are needed in different situations and any link to get started.
Wondering any approach is recommended as best practice or each has its own use cases.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā09-29-2020 06:46 PM
Hi,
The best resource I can give you is the GlideAjax cheat sheet. It literally breaks it all down for you AND shows you how to return multiple values: https://community.servicenow.com/community?id=community_article&sys_id=9f7ce2e1dbd0dbc01dcaf3231f961...
Please mark reply as Helpful/Correct, if applicable. Thanks!
Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā09-30-2020 06:51 AM
Thanks for the replies. I will stick to JSON. But one should know all approaches to support existing code. My notes from here is
1. Use JSON.stringify() and JSON.parse() to convert object, array and send from server to client.
2. Existing code might be using JSON.encode() or evalJSON and JSON.decode(). But stringify/parse is recommended
//Server side
JSON.encode(myobj)
//Client side
var returneddata = answer.evalJSON(true);
or
var returndata = answer.decode()
3. For XML type of data, creating an element is done with newItem() and and setAttribute will add attribute to be used with data to be sent
/*server side */
var result = this.newItem("result");
// get parameter from client request, force to string so switch is happy
var item = this.getParameter("sysparm_item") + "";
// add requested item to response for debugging
result.setAttribute("requested_item", item);
<result requested_item="beer"></result>
3b. For parsing (assuming call back method receives 'serverResponse' object)
/*client side */
var result = serverResponse.responseXML.getElementsByTagName("result");
var reqItem = result[0].getAttribute("requested_item");
4. To send one value just return that value from server and use getXMLAnswer(answer) to get the value
I will modify my notes if I get better understanding.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā09-30-2020 08:14 AM
Sorry, there are some misinformation.
First, encode() and decode() methods are deprecated. That is, support is planned to end.
Second, beside encode() and decode() shouldn't be used, encode isn't a property in JSON static class. That is, it's necessary to instantiate it with "new JSON()". Refer to the example in ServiceNow JSON documentation.
https://developer.servicenow.com/dev.do#!/reference/api/orlando/server_legacy/c_JSONAPI
//Server side
JSON.encode(myobj)
//Client side
var returneddata = answer.evalJSON(true);
or
var returndata = answer.decode()
Third, most developers don't use newItem() to create xml document. We just create a string with xml elements and then just parse it. Refer to ServiceNow documentation on XMLDocument2.
var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(xmlString);
Forth, my bad on this but JSON.stringify() will convert a object to a single string so getXMLAnswer() is able to fetch the string.
Client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ajax = new GlideAjax('getXMLAnswerTest');
ajax.addParam('sysparm_name', 'getData');
ajax.getXMLAnswer(function(answer) {
var json_data = JSON.parse(answer);
g_form.setValue('field2', json_data[0].first + " " + json_data[0].last);
});
}
Script Include
var getXMLAnswerTest = Class.create();
getXMLAnswerTest.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getData: function() {
var test_data = [{'first':'hitoshi','last':'ozawa'},{'first':'joe','last':'doe'}];
return JSON.stringify(test_data);
},
type: 'getXMLAnswerTest'
});