I want show only Subcategory choices based on the user role on incident.

Mannam Praveen
Tera Expert

Hello Everyone,

 

I had requirement to show subcategory choices based on role. if user roe admin, then I need to show few subcategory choices under subcategory field or if it is not admin then need show all the values. I am using below onload script but it is not working. can some help here. 

function onLoad() {
  var role = g_user.hasRole('admin');
if (role) {
    g_form.removeOption('subcategory', 'Caller ID Issues');
} else {
    return true;
}

   
}
1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

You need to be certain you are using the choice value not Label in removeOption.  Since this script is onLoad you don't need to return anything, so the script can look more like this:

function onLoad() {
    if (g_user.hasRole('admin')) {
        g_form.removeOption('subcategory', 'Caller ID Issues');
    } 
}

The other issue with this field is that out of the box it is dependent on Category.  So on a new record, you'll see you don't have any Subcategory options because Category is empty.  If you are testing by opening an existing record that has a Category selected, for which this Subcategory is a valid dependency then the onLoad script will work.  Otherwise, you will need an onChange script.  OnChange scripts also run when a form loads, so you can replace your onLoad script with this:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (g_user.hasRole('admin')) {
        g_form.removeOption('subcategory', 'Caller ID Issues');
    } 
}

If you are certain the choice value is correct and this still doesn't work, while the form load test with the Category already selected does work then it's a timing issue - Subcategory is dependent on Category so the valid choices are loading after the Client script runs to remove the option.  Let me know if this is happening and we'll see if there's a way around it.

View solution in original post

2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

You need to be certain you are using the choice value not Label in removeOption.  Since this script is onLoad you don't need to return anything, so the script can look more like this:

function onLoad() {
    if (g_user.hasRole('admin')) {
        g_form.removeOption('subcategory', 'Caller ID Issues');
    } 
}

The other issue with this field is that out of the box it is dependent on Category.  So on a new record, you'll see you don't have any Subcategory options because Category is empty.  If you are testing by opening an existing record that has a Category selected, for which this Subcategory is a valid dependency then the onLoad script will work.  Otherwise, you will need an onChange script.  OnChange scripts also run when a form loads, so you can replace your onLoad script with this:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (g_user.hasRole('admin')) {
        g_form.removeOption('subcategory', 'Caller ID Issues');
    } 
}

If you are certain the choice value is correct and this still doesn't work, while the form load test with the Category already selected does work then it's a timing issue - Subcategory is dependent on Category so the valid choices are loading after the Client script runs to remove the option.  Let me know if this is happening and we'll see if there's a way around it.

Thanks, on change script working