Module and Business Rule

Vengeful
Mega Sage

We have a module My Risks visible to role sn_risk.user and below is the filter condition.

Vengeful_0-1739780757150.png

 

 

We receive requirement that when they click the All in the filter, no record should be displayed.

Vengeful_1-1739781022797.png

So I created a business rule before-query to preview this

(function executeRule(current, previous /*null when async*/) {
    // Only apply restriction if the user is NOT a risk manager and is using the "All" filter
    if (gs.getUser() && !gs.hasRole('sn_risk.global_manager')) {
        var queryString = gs.action.getGlideURI().toString();
        // Apply restriction ONLY when the user is NOT using "My Risks" filter
        if (queryString.indexOf('sysparm_query=') !== -1 && queryString.indexOf('owned_byDYNAMIC') === -1 && queryString.indexOf('ownerDYNAMIC') === -1) {
            var gr = current.addQuery('owned_by', gs.getUserID());
            gr.addOrCondition('owner', gs.getUserID()); // Ensures correct OR condition
        }
    }
})(current, previous);

 

However, I created another module ATK Site Risk visible to role sn_risk.atk_site_risk and condition is below:

Vengeful_2-1739781216337.png

When I impersonated a user with role sn_risk.atk_site_risk, open the ATK Site Risks, there are no records.

Vengeful_3-1739781262332.png

I tried to deactivate the business rule and there are records. But I need the business rule to prevent the All filter

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Vengeful 

why not use sysparm_fixed_query as URL arguments from module, with this they cannot click All

Restrict filters and breadcrumbs with fixed queries 

you will see lot of OOB modules for this

Copy your query and give it after this

&sysparm_fixed_query=YOUR QUERY

AnkurBawiskar_0-1739782749335.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Vengeful 

determining the URL in query business rule is somewhat trickier

why are you checking the URL?

simply try this

(function executeRule(current, previous /*null when async*/ ) {
    // Only apply restriction if the user is NOT a risk manager and is using the "All" filter
    if (!gs.hasRole('sn_risk.global_manager')) {
        // Apply restriction ONLY when the user is NOT using "My Risks" filter
        var gr = current.addQuery('owned_by', gs.getUserID());
        gr.addOrCondition('owner', gs.getUserID()); // Ensures correct OR condition
    }
})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@Vengeful 

why not use sysparm_fixed_query as URL arguments from module, with this they cannot click All

Restrict filters and breadcrumbs with fixed queries 

you will see lot of OOB modules for this

Copy your query and give it after this

&sysparm_fixed_query=YOUR QUERY

AnkurBawiskar_0-1739782749335.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader