UI Policy - get the reference field value (not working)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-22-2022 03:28 PM
Hi All,
I need help to get the reference field "model category" value on to alm_hardware table.
I created a UI policy script but it is working partially because it's hiding the tabs(form section) based on role and not on type of records.I would like to hide the tabs based on role and type of model category. Kindly review my scripts, I have multiple role and multiple category.
function onCondition(){
var gr = g_form.getReference('model_category');
if(g_user.hasRole('service_desk') && gr.category_type == 'desk') {
g_form.setSectionDisplay('financial', true);
g_form.setSectionDisplay('activities', true);
}
//else if (g_user.hasRole('itil') && gr.u_category_type == 'service'){
else{
g_form.setSectionDisplay('financial', false);
g_form.setSectionDisplay('activities', false);
}
}
Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-22-2022 03:57 PM
Hello MR,
What are the names of your sections in the form?
As per definition of the setSectionDisplay, the name of the section needs to be passed with an underscore replacing the first space and then all the other spaces removed. Example: a section named "This is my section" will be referenced in the setSectionDisplay as "this_ismysection".
Section names can be found by using the getSectionNames() method.
Check this to see if you are using the right names.
Additionally, I would recommend you to use the getReference method with a callback function.
If a callback function is present, this routine runs asynchronously, and browser (and script) processing will continue normally until the server returns the reference value, at which time the callback function will be invoked. If a callback function is not present, this routine runs synchronously and processing will halt (causing the browser to appear to hang) while waiting on a server response.
So, it is strongly recommended that a callback function be used.
Reference: GlideForm - getReference
Here is an example:
function onChange(control, oldValue, newValue, isLoading) {
g_form.getReference('caller_id', doAlert); // doAlert is our callback function
}
function doAlert(caller) { // reference is passed into callback as first arguments
if (caller.getValue('vip') == 'true') {
alert('Caller is a VIP!');
}
}
Also, please check if the current user has that "service_desk" role, otherwise the sections won't be displayed.
Hope this helps you!!
Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!
Best Regards,
Filipe Cruz
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-22-2022 04:21 PM
The name of the sections are correct and I am already using the getReference method.
Could help me with the callback function? and check my codes?
Model category is a reference field on alm_hardware table(UI policy is also on alm_hardware).
model category is on "cmdb_model_category" and on same table we have another field "category type"(choice list). Based on that field and role, show/hide of the form sections on alm_hardware records
function onCondition(){
var gr = g_form.getReference('model_category');
if(g_user.hasRole('service_desk') && gr.category_type == 'desk') {
g_form.setSectionDisplay('financial', true);
g_form.setSectionDisplay('activities', true);
}
else{
g_form.setSectionDisplay('financial', false);
g_form.setSectionDisplay('activities', false);
}
}
Thanks for the quick response!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-22-2022 04:14 PM
Hi MR,
I tried it at my end with your code (slightly adjusted) and here are my remarks
a. Any particular reason why you are using UI policy and not onLoad client script? (I used client script)
b. Use callback function with getReference as suggested by
c. use getValue for the referrenced GR, i.e. gr.getValue('category_type')
d. the table [cmdb_model_category] where model is located does not contain any field called category_type, is this a custom field?
Here is my testing script with different parameters (missing the callback function though) and that works
function onLoad() {
var gr = g_form.getReference('model_category');
if(g_user.hasRole('admin') && gr.getValue('name') == 'Computer') {
g_form.setSectionDisplay('financial', false);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-22-2022 04:48 PM
Unfortunately, when I try to do it via onLoad client script it didn't help me. I tired your example but it didn't work.
Yes, category type is a custom field on cmdb_model_category