Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

UI policy Conflict

AakashM94578595
Tera Contributor

I have two Location Category options: Airport Location and Town Office Location.
The Location Action options are: Create, Modify, and Deactivate.
I also have a third variable, Location Type, with the values: None, Terminal, Floor, Room, and Rack.

I want to show specific fields based on these combinations, but using multiple UI Policies is creating a lot of conflicts. 

 

 

2 REPLIES 2

Sanjay191
Tera Sage
Tera Sage

Hi @AakashM94578595 

 

Can you please tell me what exact issue you are facing? Apart from that, please consider the points below:

  1. First, check which fields need to be visible based on the specific condition. Set only those fields to visible = true, and set all other fields to visible = false.

  2. You can also use the reverse logic — if the condition is false, set visibility accordingly. This can also help in your scenario.

  3. Also, please make sure to adjust the order of your UI Policies so they execute correctly.

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Thank You


  

Vishal_Jaiswal
Mega Guru

Hi @AakashM94578595 ,

 

- The reason you are facing conflicts is likely due to the "Reverse if false" feature in UI Policies. When you have overlapping conditions (e.g., one policy says "Hide Region" and another says "Show Region" based on a different variable), they fight for control.


Please below steps for solution.
- Step 1: Disable your UI Policies
- Step 2: Create the Logic,You will need to apply this logic in four places:
onLoad
onChange (Variable: Location Category)
onChange (Variable: Location Action)
onChange (Variable: Location Type)

- To avoid writing the code 4 times, we will write the logic once in the onLoad script and make it available to the others.

1. Create an onLoad Catalog Client Script:
Name: Main Visibility Logic
UI Type: All
Script:

function onLoad() {
    updateLocationFields();
}

// We define this function globally so other scripts can call it
// Note: In Service Portal/Mobile, window object might behave differently, 
// but this pattern generally works for standard Catalog Items.
window.updateLocationFields = function() {
    
    // --- STEP 1: GET VALUES ---
    var category = g_form.getValue('location_category'); // Replace with your actual variable name
    var action = g_form.getValue('location_action');     // Replace with actual name
    var type = g_form.getValue('location_type');         // Replace with actual name

    // --- STEP 2: THE RESET (Hide EVERYTHING first) ---
    // List every single field that appears/disappears in this array
    var dynamicFields = ['region', 'country', 'city', 'airport', 'terminal_name', 'floor_number', 'room_details', 'rack_id']; 
    
    for (var i = 0; i < dynamicFields.length; i++) {
        g_form.setDisplay(dynamicFields[i], false); 
        // Using setDisplay(false) hides the field and reclaims the empty space
    }

    // --- STEP 3: THE LOGIC (Re-apply visibility) ---
    
    // Always show Region/Country/City if we are creating? 
    // (Adjust logic based on your specific requirements)
    if (action == 'create') {
        g_form.setDisplay('region', true);
        g_form.setDisplay('country', true);
        g_form.setDisplay('city', true);
    }

    // Airport Specifics
    if (category == 'airport_location') {
        g_form.setDisplay('airport', true);
        
        if (type == 'terminal') {
            g_form.setDisplay('terminal_name', true);
        }
        else if (type == 'rack') {
            g_form.setDisplay('rack_id', true);
            g_form.setDisplay('room_details', true); // Maybe rack needs room too?
        }
    }
    
    // Town Office Specifics
    else if (category == 'town_office_location') {
        // Town offices might not need 'Airport' field
        
        if (type == 'floor') {
             g_form.setDisplay('floor_number', true);
        }
    }
    
    // Modify/Deactivate specific logic
    if (action == 'modify' || action == 'deactivate') {
        // Perhaps you only show a "Search for existing location" field here
        g_form.setDisplay('search_location', true);
    }
};

 

2. Create the onChange Scripts: Create 3 separate Client Scripts (one for Location Category, one for Location Action, one for Location Type).

For each of them, the script is simply:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading) {
      return;
   }

   // Call the main function we defined in the onLoad script
   // Ensure you use the exact same function name
   if (typeof window.updateLocationFields === 'function') {
       window.updateLocationFields();
   }
}

 

Regards,

Vishal