Filter conditions not working in a Before Query business rule

Aditya Raute
Mega Guru

I have a Before Query Business Rule which I'm using to set an Active field on a record.

1) 

AdityaRaute_5-1687985978580.png

 

 

 

2) 

AdityaRaute_1-1687985244032.png

 

 

In the Advanced tab, there is only a log statement.

 

 

Now, I enabled 'Debug business rules' and refreshed the list. When the list loads, the debugger states that Filter conditions were not met hence BR was skipped. Needless to say, no records were marked as Inactive.

AdityaRaute_6-1687986226253.png

 

 

However, when I use these conditions in a report, I see there are 66 records satisfying the condition

AdityaRaute_4-1687985773412.png

 

 

What exactly is going wrong here? I'm completely baffled. 

 

Is there any other approach I can use here? I've not tested for Insert or Update because these operations are less important for this BR. Also, can I use function fields for this?

2 ACCEPTED SOLUTIONS

jonathanurie2
Tera Expert

Hey Aditya,

 

The issue here is "before" Business Rules only run before the transaction types listed on the right side of the Business Rule form (insert, update, query, delete). So, the system will only evaluate the conditions and perform the associated actions (i.e. flip the active field to false) when the record is inserted, updated, or queried according to your business rule you've written. 

 

(incidentally, your script works great — I replicated it in a dev instance and it worked perfectly for me when I deactivated an owner, then updated their dashboard records. The issue is that you're not triggering the Business Rule by just loading up the list or Tableau Dashboard records).

 

Option 1

If you're okay with a slight delay in updating the active flag on these Tableau Dashboard records, you might consider a scheduled job. 

 

1. In the Application Navigator, navigate to System Definition > Scheduled Jobs 

2. Create a new job that runs at an interval that makes sense (daily, hourly, etc.)

3. Create a script that finds all the records in the Valid Tableau Dashboards table you've got with inactive owners

 

var gr = new GlideRecord("x_idfcf_idfc_valid_tableau_dashboards"); //I think that's your table name, but double check this 🙂 
gr.addQuery("dashboard_owner.active", "false");
gr.query();
while (gr.next()) {
	gr.active = false;
	gr.update(); 
}

 

Option 2

If you don't want a delay, then you'll need to deactivate the Tableau Dashboard records when Users are marked as inactive. For example, you could:

  • Create a Business Rule on the sys_user table that flips Tableau Dashboard records to false when a user is deactivated. 
  • Add some logic to whatever process deactivates users (e.g. Active Directory import) that also deactivates Tableau Dashboards associated with the user being deactivated.  

 

 

Hope that helps!

 

View solution in original post

PavanK960672992
Mega Patron

Hi @Aditya Raute ,

Query business rules are not used to set the values based on some conditions.

If you need to set the value then check insert or update use before business rule.

If you need to set value for all the existing records then use background script or fix script to set value.

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

View solution in original post

9 REPLIES 9

jonathanurie2
Tera Expert

Hey Aditya,

 

The issue here is "before" Business Rules only run before the transaction types listed on the right side of the Business Rule form (insert, update, query, delete). So, the system will only evaluate the conditions and perform the associated actions (i.e. flip the active field to false) when the record is inserted, updated, or queried according to your business rule you've written. 

 

(incidentally, your script works great — I replicated it in a dev instance and it worked perfectly for me when I deactivated an owner, then updated their dashboard records. The issue is that you're not triggering the Business Rule by just loading up the list or Tableau Dashboard records).

 

Option 1

If you're okay with a slight delay in updating the active flag on these Tableau Dashboard records, you might consider a scheduled job. 

 

1. In the Application Navigator, navigate to System Definition > Scheduled Jobs 

2. Create a new job that runs at an interval that makes sense (daily, hourly, etc.)

3. Create a script that finds all the records in the Valid Tableau Dashboards table you've got with inactive owners

 

var gr = new GlideRecord("x_idfcf_idfc_valid_tableau_dashboards"); //I think that's your table name, but double check this 🙂 
gr.addQuery("dashboard_owner.active", "false");
gr.query();
while (gr.next()) {
	gr.active = false;
	gr.update(); 
}

 

Option 2

If you don't want a delay, then you'll need to deactivate the Tableau Dashboard records when Users are marked as inactive. For example, you could:

  • Create a Business Rule on the sys_user table that flips Tableau Dashboard records to false when a user is deactivated. 
  • Add some logic to whatever process deactivates users (e.g. Active Directory import) that also deactivates Tableau Dashboards associated with the user being deactivated.  

 

 

Hope that helps!

 

Shane41
ServiceNow Employee

Hi Aditya,

You shouldn't apply filter conditions to a query business rule as they will be ignored. A query business rule is generally used to force a filter to records for a particular user i.e. user with software role should only see incidents with software category

To achieve this you will need to set advanced to true on the business rule and in the script section, add the query you want to apply, see example below that applies the active = true filter to any incident record that an itil user (based on condition role=itil) interacts with

 

Shane41_0-1688054696799.png

The article below gives a great explanation on how query business rules work

https://www.servicenow.com/community/developer-blog/query-business-rules-a-definitive-guide/ba-p/227...

Hope this helps,

Shane

 

Hi Shane,

 

This is not what I'm trying to achieve. I want to set the 'active' field of a particular set of records (ones with Inactive Dashboard Owner) as 'false'. I'm unable to achieve this.
Since we are advised not to use current.update() in scripts, I'm not sure how I can achieve this.

 

 

Parag Kanoje
Tera Contributor

Hi @Aditya Raute ,

If you uncheck Query check box it will work.