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

Hitoshi Ozawa
Giga Sage
Giga Sage

Try converting objects to String when pushing into an array.

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').toString());
    all_years.push(tempArray.toString());
}