RITMs and SC task should be visible to only Requested for, Approver and Group
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2024 04:56 AM
Hi ServiceNow experts,
I want to hide particular RITMs and SC tasks created from the specific catalog items (abc1,abc2,abc3,abc4,abc5) using the Query Business rule.
I am able to achieve the Group functionality, below is my script. but i am not able to achieve the requester for and approver functionality
---------------------------------------------------------------------------------------------------------------------
I tried below script but is not working, kindly help me
(function executeRule(current, previous /*null when async*/) {
// Check if the current catalog item is one of the specified items and the user has ITIL role
if (!checkGroup() && !checkITILRole())
current.addEncodedQuery("cat_item!=44799bec976ce590eda7f0e3f153af6c"); /
function checkGroup() {
var grpMember = new GlideRecord('sys_user_grmember');
grpMember.addQuery('user', gs.getUserID());
grpMember.addQuery('group', '4ecc58ad1b66501020b6848dcc4bcb21');
grpMember.query();
return grpMember.hasNext();
}
function checkITILRole() {
var userRoles = gs.getUser().getRoles();
return userRoles.indexOf('itil') != -1;
}
function checkRequestedForITILRole() {
var requestedForUser = new GlideRecord('sc_req_item');
requestedForUser.addQuery('sys_id', current.getValue('requested_for'));
requestedForUser.query();
if (requestedForUser.next()) {
var requestedForUserRoles = requestedForUser.getValue('requested_for').getRoles();
return requestedForUserRoles.indexOf('itil') != -1;
}
return false;
}
if (!checkRequestedForITILRole())
current.addEncodedQuery("requested_for!=itil");
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-07-2024 09:38 AM
Hi prudhv167,
You are better off doing that via an ACL. In that ACL you can include whatever criteria are needed. From what I can see you are looking to apply this so that for certain RITMs and their tasks only the three groups can see anything on them.
Before doing anything else, best practice is do not embed sys_ids in scripts as they may not be the same across all your instances. The simplest alternative is to set up a property that will have the names of the items. With that, you can easily retrieve the items (gs.getProperty()). That also gives you the flexibility to add items to the property without needing to make any code changes.
You will need to do three checks to make this work. The first one is to iterate through the list of items to see if the one in question is restricted. If not, return true. From there, you need to compare the current user to requested_for, current user is in whatever assignment group (sys_user_grmember) or if the user is an approver (sysaaproval_approver). If any of them are a match, return true, otherwise return false.
Here's what the script should look like. I haven't tested it so there are probably some fat fingers in ti.
answer = false;
var foundUser = false;
var theItems = gs.getProperty(<restricted items>);
theItemsSplit = theItems.split("~"); //replace ~ with whatever delimiter you use between item names
var theItem = current.cat_item.getDisplayValue();
for (var i = 0; i < theItemsSPlit.length; i++) {
if (theItemsSplit[i] == theItem) {
answer = true;
foundUser = true;
}
}
var thisUser = gs.getUserID().toString();
if (foundUser == false) {
if (thisUser == current.requested_for.toString()) {
answer = true;
foundUser = true;
}
}
if (foundUser == false) {
var theUserGroups = new GlideRecord("sys_user_grmember");
theUserGroups.addEncodedQuery("user=" + thisUser + "^group=" + current.assignment_group.toString());
theUserGroups.query();
if (theUserGroups.next()) {
answer = true;
foundUser = true
}
}
if (foundUser == false) {
var theApprovers = new GlideRecord("sysapproval_approver);
theApprovers.addEncodedQuery("approver=" + thisUser + "^sysapproval=" current.sys_id.toString());
theApprovers.query();
if (theApprovers.next()) {
answer = true;
}
:{)
Helpful and Correct tags are appreciated and help others to find information faster
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2024 04:59 AM
It is not working.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-10-2024 06:45 AM
Can you provide some details as to what you've done and what result you are getting?
:{)
Helpful and Correct tags are appreciated and help others to find information faster