How to push an array from Server Script to HTML on the Service Portal

Jay D1
Tera Guru

Hello,

I'm struggling to copy data from an array from server side to HTML on a Service Portal widget. Here is a snippet of code that I've been using. My point of reference was community.servicenow.com/community?id=community_article&sys_id=19e6a987db50041413b5fb243996191b

Server Script

//Find Year(s)
	var getYear = new GlideRecord('sn_customerservice_case_payment_m2m');
	getYear.addQuery('u_payment_number', '=', data.sys_id);
	getYear.query();

	data.rowcount = getYear.getRowCount();

	var all_years = new Array(data.rowcount);
	var tempArray = [];

	while (getYear.next()) {
		tempArray.push(getYear.getValue('u_application_case_number.u_cy_calendar'));
		all_years.push(tempArray);
	}

HTML Template

<div style="display:flex; justify-content: space-between; width: 65%;  height: 1.5em;">
          <p style="width: 50%;">Calendar Year(s)</p>
          <p style="width: 50%;">{{data.all_years}}</p>
          <p style="width: 50%;">{{data.rowcount}}</p>
        </div>

I am getting the correct Row Count but unable to print the array. Is there something I'm missing?

1 ACCEPTED SOLUTION

the blanks are 2 more entries for which the while loop runs but there is no data in it.

Use this

//Find Year(s)
	var getYear = new GlideRecord('sn_customerservice_case_payment_m2m');
	getYear.addQuery('u_payment_number', '=', data.sys_id);
	getYear.orderBy('u_application_case_number.u_cy_calendar.u_calendar_year');
	getYear.query();

	data.rowcount = getYear.getRowCount();

	var all_years = new Array(data.rowcount);

	while (getYear.next()) {
if(getYear.u_application_case_number.u_cy_calendar.u_calendar_year != '')
		all_years.push(' ' + getYear.u_application_case_number.u_cy_calendar.u_calendar_year);
	}

	data.all_years = all_years.toString();
-Anurag

View solution in original post

10 REPLIES 10

the blanks are 2 more entries for which the while loop runs but there is no data in it.

Use this

//Find Year(s)
	var getYear = new GlideRecord('sn_customerservice_case_payment_m2m');
	getYear.addQuery('u_payment_number', '=', data.sys_id);
	getYear.orderBy('u_application_case_number.u_cy_calendar.u_calendar_year');
	getYear.query();

	data.rowcount = getYear.getRowCount();

	var all_years = new Array(data.rowcount);

	while (getYear.next()) {
if(getYear.u_application_case_number.u_cy_calendar.u_calendar_year != '')
		all_years.push(' ' + getYear.u_application_case_number.u_cy_calendar.u_calendar_year);
	}

	data.all_years = all_years.toString();
-Anurag

So we updated the original code and opted to simplify with one GlideRecord query without storing the years in an array. We were able to build a String without taking the original array approach. Thanks for your guidance and assistance for this.

Here's the code for reference.

var CaseInfo = new GlideRecord('sn_customerservice_case_payment_m2m');
CaseInfo.addQuery('u_payment_number', '=', data.sys_id);
CaseInfo.orderBy('u_application_case_number.u_cy_calendar.u_calendar_year');
CaseInfo.query();

var allYears = '';
var years = '';

while (CaseInfo.next()) {

	var findYear = CaseInfo.u_application_case_number.u_cy_calendar.getDisplayValue('u_calendar_year');
	var nonparent = CaseInfo.getDisplayValue('u_application_case_number.parent');

	if (nonparent == "" || nonparent == undefined){
		data.parent_case = CaseInfo.u_application_case_number.getDisplayValue('number');
	} else {
		data.parent_case = CaseInfo.getDisplayValue('u_application_case_number.parent');
	}

	allYears += (findYear + ', ');
}

if (allYears.slice(-2) === ', ') {
	years = allYears.substring(0, (allYears.length - 2));
}

data.allYears = years;

So the if condition to check and ignore blanks worked.

Great!!

Please close the thread by marking the appropriate answer as correct.

 

-Anurag

John Dahl AJUVO
Tera Expert

Convert the array to a string before sending it to the client. In the server script, use JSON.stringify( arr ) to convert it to a string. On the client side, use JSON.parse( arr ) to turn it back into an object. 

Thanks for that but didn't work. 😞