Custom Filter - UI Macro

Rooma1
Tera Contributor

Hi All,

 

I have a requirement to have a custom filter on 'Location' field on Incident table, so that users should be able to select the inactive locations as well.

Currently, only active locations are being shown to the users in the lookup. So i have created a UI Macro but that is not working.

 

Can someone help me where i am making mistake? Screenshot below:

 

Rooma1_0-1709988879865.png

 

 

Thanks,

Rooma

1 ACCEPTED SOLUTION

Aravind2799
Giga Expert

Hey @Rooma1 

Please find the solution 

Potential Issues:

  1. Missing GlideRecord Initialization: The script doesn't explicitly create or initialize a GlideRecord object (gr) before adding queries.
  2. Incorrect Field Name (Maybe): Depending on your specific table structure, the field name for marking a location as inactive might be different. Double-check the actual field name used in your Incident table.

 

var gr = new GlideRecord('incident_location'); // Replace with actual location table if different

// Use 'getDisplayValue' to handle multiple words in location names (optional)
var locationValue = g_form.getValue('location').getDisplayValue();

// Consider using 'OR' operator for better performance with many inactive locations
gr.addQuery('name','STARTSWITH',locationValue);
gr.addOrQuery('inactive','true'); // Replace 'inactive' with the actual field name

gr.query();

if (gr.hasNext()) {
  g_form.setValue('show all locations', true);
}

 

Explanation of Improvements:

  • Initializes a GlideRecord object (gr) with the appropriate table name (replace 'incident_location' if your location table has a different name).
  • Uses getDisplayValue on the location field value to handle cases where the location name might contain multiple words. This ensures the filter considers the entire displayed value.
  • Considers using the OR operator in the addOrQuery method for improved performance, especially if you have a large number of inactive locations.

Additional Considerations:

  • Make sure the UI Macro is associated with the 'Location' field on the Incident form.
  • If the issue persists, check the UI Macro properties to ensure it has the appropriate read access on the 'incident_location' table (or your actual location table).

By implementing these adjustments, your UI Macro script should effectively create a custom filter on the 'Location' field, allowing users to see both active and inactive locations in the lookup.

 

Thanks

Aravind Panchanathan

View solution in original post

4 REPLIES 4

Manoj89
Giga Sage

Hi,

 

What is not working, the script itself, or is it that just the inactive locations are not shown?

Aravind2799
Giga Expert

Hey @Rooma1 

Please find the solution 

Potential Issues:

  1. Missing GlideRecord Initialization: The script doesn't explicitly create or initialize a GlideRecord object (gr) before adding queries.
  2. Incorrect Field Name (Maybe): Depending on your specific table structure, the field name for marking a location as inactive might be different. Double-check the actual field name used in your Incident table.

 

var gr = new GlideRecord('incident_location'); // Replace with actual location table if different

// Use 'getDisplayValue' to handle multiple words in location names (optional)
var locationValue = g_form.getValue('location').getDisplayValue();

// Consider using 'OR' operator for better performance with many inactive locations
gr.addQuery('name','STARTSWITH',locationValue);
gr.addOrQuery('inactive','true'); // Replace 'inactive' with the actual field name

gr.query();

if (gr.hasNext()) {
  g_form.setValue('show all locations', true);
}

 

Explanation of Improvements:

  • Initializes a GlideRecord object (gr) with the appropriate table name (replace 'incident_location' if your location table has a different name).
  • Uses getDisplayValue on the location field value to handle cases where the location name might contain multiple words. This ensures the filter considers the entire displayed value.
  • Considers using the OR operator in the addOrQuery method for improved performance, especially if you have a large number of inactive locations.

Additional Considerations:

  • Make sure the UI Macro is associated with the 'Location' field on the Incident form.
  • If the issue persists, check the UI Macro properties to ensure it has the appropriate read access on the 'incident_location' table (or your actual location table).

By implementing these adjustments, your UI Macro script should effectively create a custom filter on the 'Location' field, allowing users to see both active and inactive locations in the lookup.

 

Thanks

Aravind Panchanathan

@Aravind2799 Thanks for the suggestion. I forgot to associate the UI Macro to the location dictionary that is why, UI Macro was not appearing next to the Location field.

 

Thanks,

Rooma

@Aravind2799 Can we put the logging statement in my UI Macro? My UI Macro isn't returning the inactive location if the filter gets removed in the Glide Window. 

 

Actually, I have created a dynamic reference qualifier which will return just the active location in the lookup and have done the dictionary override on location field on incident table because of which UI Macro is showing just the active locations.

 

Rooma1_0-1710483169338.png