Back in the day, the only way we had to create cool dynamic filters was by having filters that read “User is javascript:gs.getUserID()”. These days, there’s a better way!

The Explanation
It’s possible now to use the “Dynamic” options to create URLs and filters that don’t reveal that they’re using JavaScript. This helps provide a better user experience, as well as make security teams happier, as they worry less about code injection in URLs if they don’t see JavaScript being passed directly.
In order to create a dynamic filter, we still have to take the steps of the old days, and then just go one step further.
In order to show how, I’ll run you through an example:
The Example
- Create a Script Include named “getHRUsers”
- In that Script Include, put this (and only this) as the script body:
function getHRUsers() { var users = new Array(); var userRole = new GlideAggregate("sys_user_has_role"); userRole.addQuery("role.name", "STARTSWITH", "hr_"); userRole.addAggregate("COUNT"); userRole.groupBy("user"); userRole.query(); while(userRole.next()) { users.push(String(userRole.user)); } return users; }
- In the left hand navigation, go to Dynamic Filter Options.
- Create a New Record, and fill it in like so:
- Label: an HR User
- Script: getHRUsers()
- Field Type: Reference
- Referenced table: User
- Order: 500
- Available for Filter: True
- Available for Default: True
That’s it! Now give it a try!
Categories: Developers, Tips and Tricks
Leave a Reply