Service Portal error : Server JavaScript error Cannot set property "events" of undefined to ""

SN Emy
Tera Guru

Hi,

I have the below code that get data from the server and save it to data.events table

when I test that i got this error : Server JavaScript error Cannot set property "events" of undefined to "" 

Do you have any idea?

 

(function($sp, input, data, options, gs) {
/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */

if(!input){
data.events = [];
//spaceInfo.userId = userId; // haven't yet figured out if this is useful info
var userId = 'my name';
var gr = new GlideRecord('fm_m2m_user_to_space');
gr.addQuery("associated_user", userId);
gr.orderByDesc("primary_location");
gr.setLimit(1);
gr.query();

while (gr.next()) {
data.events.push({

'campusSysId': gr.facilities_space.building.campus.sys_id.toString(),
'externalBuildingId':gr.facilities_space.building.external_building_id.toString(),
'spaceInfo.externalLevelId': gr.facilities_space.floor.external_level_id.toString(),
'spaceInfo.externalSpaceId': gr.facilities_space.external_space_id.toString()
});
}


}




 

 

find_real_file.png

1 ACCEPTED SOLUTION

The SN Nerd
Giga Sage
Giga Sage

You are missing the closure of your function

(function($sp, input, data, options, gs) {
/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */

	if(!input) { 
		data.events = [];
		//spaceInfo.userId = userId; // haven't yet figured out if this is useful info
		var userId = 'my name';
		var gr = new GlideRecord('fm_m2m_user_to_space');
		gr.addQuery("associated_user", userId);
		gr.orderByDesc("primary_location");
		gr.setLimit(1);
		gr.query();

		while (gr.next()) {
			data.events.push({

				'campusSysId': gr.facilities_space.building.campus.sys_id.toString(),
				'externalBuildingId':gr.facilities_space.building.external_building_id.toString(),
				'spaceInfo.externalLevelId': gr.facilities_space.floor.external_level_id.toString(),
				'spaceInfo.externalSpaceId': gr.facilities_space.external_space_id.toString()
				});
		}
	}


})($sp, input, data, options, gs) //Function closure here

ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

View solution in original post

2 REPLIES 2

Aleksas Kucins1
Giga Expert

Hi,

First line is not required - (function($sp, input, data, options, gs) {.

The SN Nerd
Giga Sage
Giga Sage

You are missing the closure of your function

(function($sp, input, data, options, gs) {
/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */

	if(!input) { 
		data.events = [];
		//spaceInfo.userId = userId; // haven't yet figured out if this is useful info
		var userId = 'my name';
		var gr = new GlideRecord('fm_m2m_user_to_space');
		gr.addQuery("associated_user", userId);
		gr.orderByDesc("primary_location");
		gr.setLimit(1);
		gr.query();

		while (gr.next()) {
			data.events.push({

				'campusSysId': gr.facilities_space.building.campus.sys_id.toString(),
				'externalBuildingId':gr.facilities_space.building.external_building_id.toString(),
				'spaceInfo.externalLevelId': gr.facilities_space.floor.external_level_id.toString(),
				'spaceInfo.externalSpaceId': gr.facilities_space.external_space_id.toString()
				});
		}
	}


})($sp, input, data, options, gs) //Function closure here

ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022