Dynamic encoded query on 'opened_by' field from Incident table in before business rule not working
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2023 04:43 AM
Hi everyone,
I am trying to create a business rule that runs before the incident table is loaded and queries only the records that are opened by the current active external user.
I've tried using an encoded query with a dynamic condition, but it doesn't seem to work. Other conditions, like if the external user is the caller or an assignment group they are a part of is assigned then it will work.
I have even tried running just the encoded query to see if the external user is the caller by itself, but it is still not working.
This is what i have:
restrictIncidentsForExternalUsers();
function restrictIncidentsForExternalUsers() {
// Get the current logged-in user's sys_id
var currentUserId = gs.getUserID();
// Query the sys_user table to check if the user has restricted access
var userGR = new GlideRecord('sys_user');
userGR.get(currentUserId);
if ((gs.hasRole("itil") || gs.hasRole("sn_incident_read")) && userGR.u_restricted_access== 1) {
//Only allow Restricted users to see Incident where they are Caller, in Watchlist, Opened by or Assignment group.Vendor is same as User.vendor
var queryString = 'assignment_groupDYNAMICd6435e965f510100a9ad2572f2b47744^NQcaller_idDYNAMIC90d1921e5f510100a9ad2572f2b477fe^NQopened_byDYNAMIC90d1921e5f510100a9ad2572f2b477fe^NQwatch_listDYNAMIC90d1921e5f510100a9ad2572f2b477fe';
current.addEncodedQuery(queryString);
current.query();
}
}
Can someone help me understand why this isn't working or if I should use a different kind of query type? Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2023 04:58 AM - edited 10-17-2023 06:22 AM
If this is a before Query Business Rule, then you don't want/need the current.query(); line. Your queryString is not really dynamic since you're hard-coding sys_ids. Change your if block within the GlideRecord query to this:
var grps = j2js(gs.getUser().getMyGroups().toArray());
var queryString = 'caller_id=' + currentUserId + '^NQopened_by=' + currentUserId + '^NQwatch_listLIKE' + currentUserId + '^NQassignment_groupIN' + grps;
current.addEncodedQuery(queryString);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-20-2023 02:33 AM
Hmm strange that this is still not querying watch_list or opened_by when I test. I will keep tweaking it though! Thanks for this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-17-2023 05:13 AM
Can you update the logic as below and Test?
var u = gs.getUserID();
if ((gs.hasRole("itil") || gs.hasRole("sn_incident_read")) && userGR.u_restricted_access== 1)
current.addQuery("opened_by", u).addOrCondition("caller_id", u)
}
Regards,
Shyamkumar