Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Function getValue is not allowed in scope sn_hr_sp

davilu
Mega Sage

Our team is developing a widget in the Human Resources Service Portal scope and want to pull column names from a table, match it up with an HR Profile, and then push those values into an array.  Here is our server script so far:

function() {

	data.user =  gs.getUserID();

	var employee_view_data_tbl = 'sn_hr_core_employee_view_category_data';
	var employee_cat_tbl = 'sn_hr_core_employee_view_categories';
	var user_tbl = 'sys_user';
	var hrProfile_tbl = 'sn_hr_core_profile';
	var arr = [];
	data.fieldsArr = [];
	var fieldsStr, column, value, displayValue, info;
	data.employee_cat_arr = [];

	var hr = new GlideRecord(hrProfile_tbl);
	hr.get('user', data.user);
	data.hr_profile = hr.getValue('sys_id');
	
	var eCat = new GlideRecord(employee_cat_tbl);
	eCat.addQuery('u_active', true);
	eCat.orderBy('u_order');
	eCat.query();
	while(eCat.next()) {
		var eCat_sysID = eCat.getValue('sys_id');

		data.employee_cat_arr.push({
			category_sysID: eCat_sysID,
			category_desc: eCat.getDisplayValue('u_category_name'),
			category_data_arr: makeArray(eCat_sysID, data.user)
		})
	}

	function makeArray(sys, user){
		arr = [];

		var gr = new GlideRecord(employee_view_data_tbl);
		gr.addQuery('u_employee_view_category', sys);
		gr.addQuery('u_active', true);
		gr.orderBy('u_order');
		gr.query();
		while(gr.next()){
			var table = gr.getValue('u_table');
			var fields = gr.getValue('u_fields');
			
			info = new GlideRecord(table);
			info.addQuery('user', user);
			info.query();
			if(info.next()) {
				value = info.getElement(fields);
			}

			arr.push({
				value: value
			})
		}
		return arr; 
	}
})();

In order for our makeArray function to work, we need to be able to get the column value through dot walking and know that

gr.getValue('user.manager');

does not works.  We read that getElement should work and when we do gs.addInfoMessage(info.getElement(fields)), it shows us the values in info messages, however when we push those values into our arr, it shows up in the console as value:{}.  We've also tried adding .getValue() afterwards (e.g. info.getElement(fields).getValue()), but we get an error message saying "Function getValue is not allowed in scope sn_hr_sp'.

Is there a workaround to this?  Why can't we use getValue in this fashion?

1 ACCEPTED SOLUTION

Anil Lande
Kilo Patron

I would suggest to use 

grObj.field_name.toString();

to avoid any errors when you try to get reference field values.

 

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

View solution in original post

6 REPLIES 6

Hi Anker, my apologies.  I tried changing my variables to not use getValue (e.g. var table = gr.u_table), but it did not work.  In the console, it would show up as an empty object.  When I used toString(), I got the results I needed.  Thanks.

No worries.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader