How to write a Query Business Rule?

Sid_Takali
Kilo Patron
Kilo Patron

Hi all, 

I have a requirement where I have to show a List view of incidents  which are assigned to my group only. 

I know this can be achieved by Before Query BR but I have never written a query BR.  Can anyone help me with it. 

Thanks!! 

1 ACCEPTED SOLUTION

Saurav11
Kilo Patron
Kilo Patron

Hello,

 

So your requirement is any user going to list view should see only his own assignment group incidents then write the below code in a before query BR:-

 

Saurav11_0-1666178779707.png

 

Code:-

 

(function executeRule(current, previous /*null when async*/) {

	current.addEncodedQuery("assignment_groupIN"+ gs.getUser().getMyGroups().toArray().join());

})(current, previous);

 

Please mark my answer as correct based on Impact.

View solution in original post

4 REPLIES 4

Sebastian L
Mega Sage

Hi Siddharam,

 

Well, yes it can be achieved by a query business rule, but a query business rule goes for all queries to the table - so it is not just for a list. It means no one will be able to see incidents assigned to other groups than their own - no matter where they look. I would rather think you can get your view by just adding a filter to a module by having "assignment group is (dynamic) one of my groups". 

 

If you requirement is for ALL queries to incident and NOT just for a list view, then here is a great guide for query business rule: https://www.servicenow.com/community/developer-blog/query-business-rules-a-definitive-guide/ba-p/227... 

Also please remember to include that the caller, opened_by or others can see an incident, so you just don't only create a rule about the assignment group, but think about all stakeholders that should be able to view the incident(s). 


Best regards,
Sebastian Laursen

Saurav11
Kilo Patron
Kilo Patron

Hello,

 

So your requirement is any user going to list view should see only his own assignment group incidents then write the below code in a before query BR:-

 

Saurav11_0-1666178779707.png

 

Code:-

 

(function executeRule(current, previous /*null when async*/) {

	current.addEncodedQuery("assignment_groupIN"+ gs.getUser().getMyGroups().toArray().join());

})(current, previous);

 

Please mark my answer as correct based on Impact.

Hello,

 

If my answer helped you can you also mark it as correct.

 

Thanks.

Abhijit4
Mega Sage

I think you shouldn't be using query BR for this requirement, if you use query BR then each and every user will be able to see only incidents which are assigned to their group which is not good process wise. There are many other users who needs to see incidents even if they are not part of group.

 

Instead you can plan to provide them new module with below filter ( you can make filter non-editable if required)

Abhijit4_0-1666180932324.png

 

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP