- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2023 02:05 PM - edited 06-29-2023 10:50 PM
I have a Before Query Business Rule which I'm using to set an Active field on a record.
1)
2)
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.
However, when I use these conditions in a report, I see there are 66 records satisfying the condition
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?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2023 03:44 PM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2023 12:28 AM
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.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-28-2023 03:44 PM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2023 09:06 AM - edited 06-29-2023 09:08 AM
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
The article below gives a great explanation on how query business rules work
Hope this helps,
Shane
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2023 10:52 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2023 11:21 PM
Hi @Aditya Raute ,
If you uncheck Query check box it will work.
