The CreatorCon Call for Content is officially open! Get started here.

UI Policy is not able to filter dependent field

saptarshiamc
Tera Contributor

In the Change Request form, the 'Category' field depends on the 'u_area' field. When a user selects 'UTA' in the 'u_area' field, four options ('Asset Replacement', 'Asset Transfer', 'Asset Name Change', and 'Asset Disposal') are available in the 'Category' field.

However, a specific requirement entails that if the user selects 'UTA' in the 'u_area' field and possesses the role of 'Disposal Manager', only the 'Asset Disposal' option should be visible in the 'Category' field along with the others, but not visible otherwise.

 

This is working for other fields that are not dependent, however not working on dependent fields. Need your suggestion

1 ACCEPTED SOLUTION

In both cases the system is retrieving the dependent category choices when the area changes, likely after your script has completed.  Try setting an interval or timeout prior to the clear or remove.  Since it's reloading the choices with every change, you should just need to remove the one choice if they don't have the role - no need to clear all and re-add the other choices:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var uArea = g_form.getValue('u_area');
    if (uArea ==='8ba3a6aa47c1b9185f03bd01336d43bc') {
        var isAdmin = g_user.hasRole('Disposal Manager');
        if (!isAdmin) {
            alert ('not Admin');    
            setInterval(function() {
                g_form.removeOption('category', 'Asset Disposal');
            }, 2000);
        }
    }
}

wrapping the removeOption in a setInterval function may also work in a UI Policy script, depending on the timing of a UI Policy execution vs Client Script and the dependent field retrieval.

View solution in original post

6 REPLIES 6

Brad Bowman
Kilo Patron
Kilo Patron

If the same logic in the Conditions is working for fields that are not dependent, scrap the UI Policy for this one and try a Client Script instead.  You'll have to use g_form.getValue('u_area') to test the value of this field, and a getReference on requested_for to check the Roles field - being sure to test for the value of the Disposal Manager role, not the display name.

https://docs.servicenow.com/bundle/tokyo-api-reference/page/app-store/dev_portal/API_reference/Glide... 

saptarshiamc
Tera Contributor

Hi @Brad Bowman   Thanks for your reply. I have tried with the following client script, but it is also not working

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
   var uArea = g_form.getValue('u_area');
   
 if(uArea ==='8ba3a6aa47c1b9185f03bd01336d43bc')
 {
var isAdmin = g_user.hasRole('Disposal Manager');
if (isAdmin){
          g_form.clearOptions('category');
  g_form.addOption('category','Asset Replacement');
          g_form.addOption('category','Asset Name Change');
                          g_form.addOption('category','Asset Transfer');
                          g_form.addOption('category','Asset Disposal');
 
}
else
{
          g_form.clearOptions('category');
  g_form.addOption('category','Asset Replacement');
          g_form.addOption('category','Asset Name Change');
                          g_form.addOption('category','Asset Transfer'); }
 
 }
  
   
}

@Brad Bowman  I checked with a message that the client is executing, but it is not changing the value of the dependent field. If I did the same for another field which is not dependent, the script is working fine as expected.

In both cases the system is retrieving the dependent category choices when the area changes, likely after your script has completed.  Try setting an interval or timeout prior to the clear or remove.  Since it's reloading the choices with every change, you should just need to remove the one choice if they don't have the role - no need to clear all and re-add the other choices:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var uArea = g_form.getValue('u_area');
    if (uArea ==='8ba3a6aa47c1b9185f03bd01336d43bc') {
        var isAdmin = g_user.hasRole('Disposal Manager');
        if (!isAdmin) {
            alert ('not Admin');    
            setInterval(function() {
                g_form.removeOption('category', 'Asset Disposal');
            }, 2000);
        }
    }
}

wrapping the removeOption in a setInterval function may also work in a UI Policy script, depending on the timing of a UI Policy execution vs Client Script and the dependent field retrieval.