Please help with Business Rule Script conditions.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2024 06:24 PM
Hello,
Could someone please help review the code? It is not working. The purpose of the business rule is to allow users to see records that are assigned to their team, but not records assigned to other teams. Thank you.
When to Run: Before, Query
(function executeRule(current, previous /*null when async*/) {
if (!gs.hasRole("itil") && gs.getSession().isInteractive()) {
var u = gs.getUserID();
var qc = current.addQuery("requested_for", u).addOrCondition("opened_by", u).addOrCondition("watch_list", "CONTAINS", u).addOrCondition('assignment_group', '927934e41b6bf0104425cb35624bcbe9');
gs.info("query restricted to user: " + u);
current.addQuery('assignment_group', '927934e41b6bf0104425cb35624bcbe9');
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2024 10:55 PM
Hi @Maddysunil
It seems to working for the sc_req_item records. I tried to apply the same code to "sc_request" and "sc_task" tables, but it returns all the records for those tables. Any suggestions?
(function executeRule(current, previous /*null when async*/) { if (!gs.hasRole("itil") && gs.getSession().isInteractive()) { var u = gs.getUserID(); var qc = current.addQuery("requested_for", u).addOrCondition("opened_by", u).addOrCondition("watch_list", "CONTAINS", u).addOrCondition('assignment_group', '927934e41b6bf0104425cb35624bcbe9'); gs.info("query restricted to user: " + u); } })(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2024 11:07 PM
Hi Annie,
there is no field "requested_for" on sc_task table but it is dotwalked from parent. You need to use this:
var gr = new GlideRecord('sc_task');
gr.addQuery('request_item.request.requested_for', '318d28db1b66cd904e15ebdde54bcb0d');
gr.query();
if(gr.next()){
gs.print("Task " + gr.getDisplayValue());
}
so your query should be:
var qc = current.addQuery("request_item.request.requested_for", u).addOrCondition("opened_by", u).addOrCondition("watch_list", "CONTAINS", u).addOrCondition('assignment_group', '927934e41b6bf0104425cb35624bcbe9');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2024 04:45 AM
Try with the below code:
(function executeRule(current, previous /*null when async*/) {
if (!gs.hasRole("itil") && gs.getSession().isInteractive()) {
var u = gs.getUserID();
var gr = new GlideRecord(current.getTableName());
gr.addQuery("requested_for", u);
gr.addOrCondition("opened_by", u);
gr.addOrCondition("watch_list", "CONTAINS", u);
if (current.getTableName() === 'sc_task') {
gr.addOrCondition('assigned_to', u);
}
gr.addOrCondition('assignment_group', 'CONTAINS', u); // Adjust if necessary
gr.query();
if (gr.next()) {
current.addQuery("sys_id", gr.sys_id);
} else {
// Clear the current query to return no records if none match
current.setWorkflow(false);
}
gs.info("query restricted to user: " + u);
}
})(current, previous);
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2024 10:19 PM
@Annie10 ,
Step 1: Ensure your have "security_admin" role. Security admin role is required to modify the ACL.
Step 2: Click "Elevate role" from the user preferences. If you are not able to see "elevate role" then you don't have "Security admin" role.
Step 3: Open the link
Step 4: Add the code in "Script condition".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2024 10:52 PM
@Community Alums
I must have screwed up some where. It returned all the record.