Please help with Business Rule Script conditions.

Annie10
Tera Contributor

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

Annie10_0-1715995405484.png

(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);

 

26 REPLIES 26

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);

eviljack
Tera Contributor

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');

@Annie10 

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

Community Alums
Not applicable

@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.

Sai149_0-1716009355667.png

 

Step 3: Open the link 

 

https://yourInstanceName.service-now.com/sys_security_acl.do?sys_id=d9694407c3923000c111113e5bba8ffd...

 

Step 4:  Add the code in "Script condition".

 

Sai149_1-1716009499212.png

 

@Community Alums 

I must have screwed up some where.  It returned all the record.

Annie10_0-1716011526244.png