- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-16-2022 04:30 PM
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-16-2022 07:51 PM
I would suggest to use
grObj.field_name.toString();
to avoid any errors when you try to get reference field values.
Thanks,
Anil Lande
Thanks
Anil Lande
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2022 05:27 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2022 05:52 AM
No worries.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
