Difference between Before BR and Before query BR?

AjKadam
Tera Contributor

How we can differentiate when to use the Before Query BR and Before BR?

Exactly what are the real-time scenarios that we can decide in this type of situation we have to go with Before Query BR so it won't create performance issues to an instance or we can maintain the standard

1 ACCEPTED SOLUTION

Megha Padale
Giga Guru

Hi,

Adding to all, Before a record is saved to the database.Business rules execute after form submission and before the record update in the database. Example: Calculation of priority based on impact and urgency

imgbr9.PNG

Before Query Before query Business Rules execute before a query is sent to the database. When a user is not authorized to see all records in a list, the “Number of row removed by security constraints” message appears. Before query Business Rules act like ACL and prevents users from seeing certain records. When access is controlled through a before query Business Rule ,the “Number of row removed by security constraints” message is not displayed and the user does not know access is restricted.

br10.PNG

 

check below links:

https://www.learnnowlab.com/Business-Rules/

https://www.servicenowguru.com/scripting/business-rules-scripting/controlling-record-access-before-q...

If my answer helped you in any way, mark answer as helpfula nd correct.

Thanks and regards,

Megha.

View solution in original post

6 REPLIES 6

Namrata Khabale
Giga Guru

Hey Aj,

 

Before Query BR:

You can use a query BRule that executes before the database query is made to prevent from accessing certain records

Write BRule on ('incident') table --- When before-query

Example Script:-

This example prevents users from accessing incident records unless they have the ITIL role are listed in the caller or Opened by field . So, for ex, when self-service users open a list of incidents, they can only see the incidents they submitted.

if(!gs.hasRole("itil")&& gs.isInteractive()){
  var u = gs.getUserID();
  var qc = current.addQuery("caller_id", u).addOrCondition("opened_by", u).addOrCondition("watch_list","CONTAINS", u);
  gs.print("query restricted to user: "+ u);}

Or You can refer the Service Now Docs as well.

https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/script/useful_scripts/concept/c_E...

 

There is one OOTB before query BR on user table. it basically checks if logged in user is admin. if not then it will add query as active is true.

 

It is to restrict other users than admins to see inactive users.

This can be done with ACL also. but advantage of BR is that it will not show any message like, 10 records were hided because of security reasons.

 

Before BR: After the user submits the form but before any action is taken on the record in the database.

 Use to update information on the current object. For example, a business rule containing current.state=3; would set the State field on the current record to the state with a Value of 3.

 

Please mark it Correct or Helpful, if it works based on impact....!!!!

 

Best Regards,

Namrata.

DirkRedeker
Mega Sage

Hi

There is no DIFFERENCE, a "Before Query BR" is just a more special type of "BR".

Generally a "Before BR" runs BEFORE the Database operation happens, so does the "BEFORE QUERY".

In that specific type of "BEFORE QUERY BR", you can add e.g. Filterconditions for your query to narrow the data shown in the result. (so, to add the filterconditions is BEFORE the database operation - the Query).

For other "Before BR", it will be also ALWAYS specific, like a "BEFORE INSERT" or "BEFORE UPDATE"-

There is NO just "BEFORE BR" which is generally. You alway specify, the database operation, the BEFORE BR will run on.

 

Let me know, if that answers your questioin and mark my answer as correct and helpful.

BR

Dirk

 

Swapnil Soni1
Giga Guru

Hi,

You can use a query business rule that executes before a database query is made.

Use this query business rule to prevent users from accessing certain records.For filter the data and add some condition and querying the table we can use query business rule.

Before a query for a record or list of records is sent to the database.
Typically you should use query for before business rules.

Before BR

Since they run before the physical update, any changes you make to the record during the rule are recorded as part of the update. This makes before rules the ideal place to set values as part of the save cascade. On the other hand, at the time the before rule is running, the current set of changes hasn't yet written through to the database, and may not be, e.g. the update could be cancelled because of missing mandatory fields, other business rules, etc.

 

So just simple difference between them is for filtering the data you can use query BR and for all data you can use before BR.

Please mark correct or helpful.

Thanks

Tejas Tamboli
Giga Guru

Hello Aj,

Business Rule is server-side scripting that executes whenever a record is inserted, updated, deleted, displayed, or queried. The key thing to keep in mind while creating a business rule is that when and on what action it has to execute. You can run the business rule ‘on display’, ‘on before’ or ‘on after’ of an action (insert, delete, update) is performed.

 

Before Business Rule:-

 

Before Business Rule will, runs before the data saved in the database or in other words, we can say that the actions you give are saved first and then the form gets updated/inserted.

 

                                        When user click save or update the record

                                                                      Before

                                        Before Business Rule Script will get execute

                                                                      Then

                                      Record get to save in the database or in table

 

Example:-

 1. Set field values using scripting.

Sol:-

                current.u_email = gs.getUser().email;

 

 2. Abort the action by validating the field value.

Sol:-

                If(current.u_email == xyz@gmail.com)

                {

                                gs.addInfoMessage(“You are submitting the right value”);

                }

                Else {

                                current.setAbortAction(true);

                }

 

After Business Rule:-

 

After Business Rule runs when the record is saved in the database. We can use after business rule to react inserts/updates/deletes.

 

                                          When user click save or update the record

                                                                       Before

                                        Record get to save in the database or in table

                                                                       Then

                                          After Business Rule Script will get execute

 

Example:-

 1. Will create the problem ticket through the incident ticket, when user change the incident state field value as “On Hold”

Sol:-

             Var configurationitem = current.cmdb_ci;

             Var shortdesciption = current.short_desciption;

             Var assignmentgroup = current.assignment_group;

 

             Var probobj = new GlideRecord(‘problem’);

             probobj.cmdb_ci = configurationitem;

             probobj.short_desciption = shortdesciption;

             probobj.assignment_group = assignmentgroup;

 

             probobj.insert();

             gs.addInfoMessage(probobj.number + ”ticket has been created through incident record”);

 

Before Query Business Rule:-

 

Before Query Business Rules are just what they sound like - Business Rules that run before the query on a table occurs.  They offer an opportunity to add query terms that will limit the data returned, and can, therefore, act as security measures.  

You can visit below link for more info -

https://www.servicenowguru.com/scripting/business-rules-scripting/controlling-record-access-before-q...

https://docs.servicenow.com/bundle/newyork-application-development/page/script/business-rules/concep...

 

 

I hope this will help you.

 

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response useful to you and help others to find information faster.

Thanks,
Tejas