Query business rule for user based location, country restriction
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2023 05:59 AM
I have created a query business rule to restrict viewing of incidents, catalog requests, and catalog tasks based on user location. I need users to only be able to view incidents, catalog requests, and tasks that were created by users with the same location and country code. US locations should be able to see all countries.
Please see the attached code. I could use some help troubleshooting.
This is a before query business rule.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2023 06:06 AM
Hi @Mattikus4901 ,
Please follow the thread :https://www.servicenow.com/community/itsm-forum/how-to-restrict-catalog-item-for-user-country/td-p/7...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2023 08:49 PM
HI @Mattikus4901 ,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2023 07:13 AM
Try this script.
(function executeRule(current, previous /*null when async*/) {
var userCountry = "";
var query = "";
var currentUser = gs.getUserID();
var userGR = new GlideRecord('sys_user');
if (userGR.get(currentUser)) {
userCountry = userGR.location.country + '';
}
var userCountrySm = userCountry.toLowerCase();
if(userCountrySm == 'us' || userCountrySm == 'united states' || userCountrySm == 'usa' || userCountrySm == 'united states of america'){
return;
}
var taskType = current.sys_class_name + '';
if (taskType == "incident") {
query = "ref_incident.caller_id.location.country=" + userCountry;
} else if(taskType == "sc_req_item"){
query = "ref_sc_req_item.requested_for.location.country="+userCountry;
} else if(taskType == "sc_request"){
query = "ref_sc_request.requested_for.location.country="+userCountry;
} else if(taskType == "sc_task"){
query = "ref_sc_task.request_item.requested_for.location.country="+userCountry;
} else {
query = "opened_by.location.country="+userCountry;
}
current.addEncodedQuery(query);
})(current, previous)
Please mark my answer helpful and accept as a solution if it helped 👍✔️
Anvesh