<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>question Re: Passing an Array of objects via glideAjax in Developer forum</title>
    <link>https://www.servicenow.com/community/developer-forum/passing-an-array-of-objects-via-glideajax/m-p/1473745#M130671</link>
    <description>&lt;P&gt;Hello Simon,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you return either Arrays or Objects to a client script via GlideAJAX you need to stringify the returned value. Then in the client script you can parse the result and do with it as you wish. For example in the provided the Script Include is attempting to return the KB List using the following:&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;return new JSON().encode(kbList);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Instead of the above try the following:&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;return JSON.stringify(kbList);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then in your Client Script you are attempting to determine one of three possible return values: 1. 'No Attached KB' 2. 'No Access' or 3. An Array of Objects. Because of that I would compare the returned 'answer' to the possible strings first then attempt to parse and utilize the array like so:&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;var answer = response.responseXML.documentElement.getAttribute("answer");

if (answer == 'No Attached KB') {

alert('Please attach a knowledge article to this incident for the caller before resolving');
	
}
else if (answer == 'No Access') {

alert('The caller does not have access to view the attached knowledge articles\nPlease select a different article');
					
}
else {

    answer = JSON.parse(answer);
				
    for (var i=0; i&amp;lt; answer.length; i++) {

	   g_form.addInfoMessage(answer[i].number + ': ' + answer[i].short_desc);

    }
				
}
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please let me know if this helps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;--David&lt;/P&gt;</description>
    <pubDate>Thu, 13 Jun 2019 15:05:33 GMT</pubDate>
    <dc:creator>DScroggins</dc:creator>
    <dc:date>2019-06-13T15:05:33Z</dc:date>
    <item>
      <title>Passing an Array of objects via glideAjax</title>
      <link>https://www.servicenow.com/community/developer-forum/passing-an-array-of-objects-via-glideajax/m-p/1473743#M130669</link>
      <description>&lt;P&gt;Afternoon All,&lt;/P&gt;
&lt;P&gt;So I've been working on a script where I am testing to see if an incidents caller has access to any of the attached KBs using GlideAjax. I want to then pass that list of KBs (as objects containing, number, short_desc and sys_id) back to the client script but am having a hard time outputting the results on the client side.&lt;/P&gt;
&lt;P&gt;Could someone look it over for me and see what I'm doing wrong:&lt;/P&gt;
&lt;P&gt;ServerSide Script:&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;var kbResolveCriteriaCheck = Class.create();
kbResolveCriteriaCheck.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	verifyAccess: function(){
		//defines base variables
		var logText = '';
		var caller = this.getParameter('sysparm_caller');
		var inc_num = this.getParameter('sysparm_inc_num');
		var kbArticle = '';
		var kbBase = '';
		var canReadKBBase = '';
		var kbList = [];
		
		//gets the incidents sys_id
		var inc_sys_id = new GlideRecord('incident');
		inc_sys_id.get('number',inc_num);
		inc_sys_id = inc_sys_id.sys_id;
		
		logText = 'Caller: ' + caller + '\n';
		logText += 'Incident Number: ' + inc_num + '\n';
		logText += 'Incident SYS_ID: ' + inc_sys_id + '\n';
		
		//gets the users list of criteria groups
		var callersCriteria = SNC.UserCriteriaLoader.getAllUserCriteria(caller).toString();
		//callersCriteria = callersCriteria.split(',');
		logText += 'User Criteria groups: ' + callersCriteria.toString() + '\n';
		
		//Gets the Last KB and the associated KB Base for the last KBA attached to the incident
		var getKB = new GlideRecord('m2m_kb_task');
		getKB.addQuery('task',inc_sys_id);
		getKB.orderByDesc('sys_created_on');
		//getKB.setLimit(1);
		getKB.query();
		//if no KB's returned none were attached
		if (!getKB.hasNext()) {
			logText += 'No KB Attached \n';
			gs.log(logText,'kbResolveCriteriaCheck');
			return 'No Attached KB';
		}
		while (getKB.next()) {
			kbArticle = getKB.kb_knowledge;
			kbBase = getKB.kb_knowledge.kb_knowledge_base;
			logText += 'KBArticle: ' + kbArticle + '\n';
			logText += 'kbBase: ' + kbBase + '\n';
			//lookups the KB Base to see if the user has access to it
			var getKBCriteria = new GlideRecord('kb_uc_can_read_mtom');
			getKBCriteria.addQuery('kb_knowledge_base',kbBase);
			getKBCriteria.addQuery('user_criteria','IN',callersCriteria);
			getKBCriteria.query();
			if (getKBCriteria.next()) {
				canReadKBBase = 'true';
				logText += 'User can read KB Base\n';
			}
			//user does not have access to KBbase so break
			else if (!getKB.hasNext()){
				break;
			}
			
			//get's the articles roles
			if (canReadKBBase == 'true') {
				var kbRoles = kbArticle.roles;
				logText += 'KBA Roles: ' + kbRoles + '\n';
				//looks to see if the end user has that role or if the role is empty;
				if (gs.getUser().getUserByID(caller).hasRole(kbRoles) || JSUtil.nil(kbRoles)) {
					//if has roles or role is empty builds the link
					var obj = {};
						obj.number = kbArticle.number;
						obj.short_desc = kbArticle.short_description;
						obj.sys_id = kbArticle.sys_id;
						kbList.push(obj);
					}
					
				}
			//user does not have roles to access kb so break
				else if(!getKB.hasNext()) {
					break;
				}
			}
		//if array is length is not 0 we assume they had access
			if(kbList.length != 0) {
				logText += 'Answer: ' + JSON.stringify(kbList) + '\n';
				gs.log(logText,'kbResolveCriteriaCheck');
				return new JSON().encode(kbList);
			}
		//if array length is 0 they had no access to any of the attached KBs
			else {
				logText += 'Returning false' + '\n';
				gs.log(logText,'kbResolveCriteriaCheck');
				return 'No Access';
			}
		},
		
		type: 'kbResolveCriteriaCheck'
	});&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Appropriate section of client script (I'm just trying to return it as a list of infomessages right now):&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;function kbCheckResult(response) {
				var answer = response.responseXML.documentElement.getAttribute("answer");
				//answer = JSON.stringify(answer);
				//answer = answer.toString();
				answer = answer.evalJSON();
				
				if (answer == 'No Attached KB') {
					alert('Please attach a knowledge article to this incident for the caller before resolving');
					return false;
				}
				else if (answer == 'No Access') {
					alert('The caller does not have access to view the attached knowledge articles\nPlease select a different article');
					return false;
				}
				else {
					for (var i=0; i&amp;lt; answer.length; i++) {
						g_form.addInfoMessage(answer[i].number + ': ' + answer[i].short_desc);
					}
				}
			}
		}&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 12 Jun 2019 18:42:42 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/passing-an-array-of-objects-via-glideajax/m-p/1473743#M130669</guid>
      <dc:creator>Community Alums</dc:creator>
      <dc:date>2019-06-12T18:42:42Z</dc:date>
    </item>
    <item>
      <title>Re: Passing an Array of objects via glideAjax</title>
      <link>https://www.servicenow.com/community/developer-forum/passing-an-array-of-objects-via-glideajax/m-p/1473744#M130670</link>
      <description>&lt;P&gt;Hi Simon,&lt;/P&gt;
&lt;P&gt;I usually just send the array with a join(",") and split the info again on client side.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;PS: Using glidedialogwindow with the ui page 'glide_confirm_standard' or 'glide_info' might improve your client experience icw alert;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://docs.servicenow.com/bundle/helsinki-application-development/page/script/client-scripts/reference/r_DisplayingACustomDialog.html" rel="nofollow"&gt;https://docs.servicenow.com/bundle/helsinki-application-development/page/script/client-scripts/reference/r_DisplayingACustomDialog.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Regards Roel&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2019 09:48:16 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/passing-an-array-of-objects-via-glideajax/m-p/1473744#M130670</guid>
      <dc:creator>Roel3</dc:creator>
      <dc:date>2019-06-13T09:48:16Z</dc:date>
    </item>
    <item>
      <title>Re: Passing an Array of objects via glideAjax</title>
      <link>https://www.servicenow.com/community/developer-forum/passing-an-array-of-objects-via-glideajax/m-p/1473745#M130671</link>
      <description>&lt;P&gt;Hello Simon,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you return either Arrays or Objects to a client script via GlideAJAX you need to stringify the returned value. Then in the client script you can parse the result and do with it as you wish. For example in the provided the Script Include is attempting to return the KB List using the following:&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;return new JSON().encode(kbList);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Instead of the above try the following:&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;return JSON.stringify(kbList);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then in your Client Script you are attempting to determine one of three possible return values: 1. 'No Attached KB' 2. 'No Access' or 3. An Array of Objects. Because of that I would compare the returned 'answer' to the possible strings first then attempt to parse and utilize the array like so:&lt;/P&gt;
&lt;PRE class="language-javascript"&gt;&lt;CODE&gt;var answer = response.responseXML.documentElement.getAttribute("answer");

if (answer == 'No Attached KB') {

alert('Please attach a knowledge article to this incident for the caller before resolving');
	
}
else if (answer == 'No Access') {

alert('The caller does not have access to view the attached knowledge articles\nPlease select a different article');
					
}
else {

    answer = JSON.parse(answer);
				
    for (var i=0; i&amp;lt; answer.length; i++) {

	   g_form.addInfoMessage(answer[i].number + ': ' + answer[i].short_desc);

    }
				
}
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please let me know if this helps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;--David&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jun 2019 15:05:33 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/passing-an-array-of-objects-via-glideajax/m-p/1473745#M130671</guid>
      <dc:creator>DScroggins</dc:creator>
      <dc:date>2019-06-13T15:05:33Z</dc:date>
    </item>
    <item>
      <title>Re: Passing an Array of objects via glideAjax</title>
      <link>https://www.servicenow.com/community/developer-forum/passing-an-array-of-objects-via-glideajax/m-p/1473746#M130672</link>
      <description>&lt;P&gt;Thanks David, this is what I ended up doing, I made my answer a JSON payload and then used JSON.parse client side to handle the returned data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Jun 2019 11:57:07 GMT</pubDate>
      <guid>https://www.servicenow.com/community/developer-forum/passing-an-array-of-objects-via-glideajax/m-p/1473746#M130672</guid>
      <dc:creator>Community Alums</dc:creator>
      <dc:date>2019-06-14T11:57:07Z</dc:date>
    </item>
  </channel>
</rss>

