- 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-16-2022 07:39 PM
Hi,
it's not allowed in scoped app
you can directly get field value like this
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.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.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.u_table;
var fields = gr.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;
}
})();
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- 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:12 AM
Thanks Anil! This is what we ended up doing and it works:
var info = new GlideRecord(table);
info.addQuery('user', user);
info.query();
if(info.next()) {
var ele = info.getElement(fields);
var ed = ele.getED();
label = ed.getLabel();
name = ed.getName();
value = ele.toString();
displayValue = ele.getDisplayValue();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2022 05:20 AM
Did you mistakenly marked other response as correct?
As 1st response to your post I shared something similar to directly access the fields using object.
You can mark your own response as correct so that it helps future readers.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
