What constitutes a Query for a query business rule to execute?

mikeberry
Kilo Explorer

I have an After business rule on the rm_story table that works exactly as expected for Insert and Update events.

I wanted to have the rule run once on all records in the table after successful implementation so, (in dev) i added the 'Query' option as well.  I assumed i could just open a list view of the table and that would fire the business rule but it did not.  Second, i tried to query the table from a Background script:

var a = 0;
var st = '';

var strygrp = new GlideRecord('rm_story');
strygrp.query();
		
		while(strygrp.next())
			{
				a = a + 1;
				st = strygrp.state;
			}
gs.print(a);

after executing this script, it returned the number of records in the table (the 'a' variable) but the business rule did not execute.

Any thoughts on why it did not fire the business rule?  what constitutes a 'Query'?
to make sure the BR is not executing, i added a gs.log statement and verified the logged event when i updated a record then verified there was not a new log event after executing the script above - as a background script.

any help is appreciated!

6 REPLIES 6

Dylan Mann1
Giga Guru

Maybe I'm misinterpreting the question, but a query business rule isn't used to run scripts on each individual record. They work similar to ACLs where they restrict which records a user can see based on your script. 

In order for this business rule to fire you need to change your business rule to run Before, this way the records are filtered before querying the DB and the records you want hidden are protected. 

Here's a link to an example Before Query BR: https://docs.servicenow.com/bundle/kingston-application-development/page/script/business-rules/conce...

Let me know if this helped,

Dylan

i can't actually run it before because it cause a recursive condition.
basically the business rule looks at an updated or inserted story and queries for other stories with the same enhancement.
it compares the states of the story/ies and updates a custom field on the related enhancement if conditions are met.

Because of that query, it can't run before - the background script actually even prevented it.

(function executeRule(current, previous /*null when async*/) {
	
	// When a story's state changes, the enhancement is automatically updated.
	//'lowest' story state determines enhc state if there are multiple.
	
	var groupnum = 5;
	var st = '';
	var target = current.enhancement;
	var enhst = "";
	
	gs.log("run","MB_roi2");
	
	try
	{
		var strygrp = new GlideRecord('rm_story');
		strygrp.addQuery('enhancement', target);//all stories with same enhc
		strygrp.query();
		
		while(strygrp.next())
			{
			st = strygrp.state;
				st = Number(st);
			switch(st)
			{
				case -6:
				case 10:
				case 9:
				//Draft Ready for Story Review Pending LOE
				//set enhancement to:Identifying Requirements
				if(groupnum > 1)
					{
					groupnum = 1;
					enhst = "Identifying Requirements";
				}
				break;
				case 1:
				//Ready for Sprint Assignment
				//set enhancement to:Ready for Sprint Assignment
				if(groupnum > 2)
					{
					groupnum = 2;
					enhst = "Ready for Sprint Assignment";
				}
				break;
				case -2:
				case 2:
				case -8:
				case 11:
				case 6:
				//Not Started Work in Progress Testing Ready for Production Validate in Production
				//set enhancement to:In Development
				if(groupnum > 3)
					{
					groupnum = 3;
					enhst = "In Development";
				}
				break;
				case 3:
				//Complete
				//set enhancement to:Complete
				if(groupnum > 4)
					{
					groupnum = 4;
					enhst = "Complete";
				}
				break;
				default:
				//enhst = "none";
			}			
			
			st = '';
		}
		
	}
	catch(exc)
	{
		gs.log("story number=" + strygrp.number + ":trgtEnhc=" + strygrp.enhancement.number + ":" + "lowestState=" + lowestState + ":ERROR!:" + exc.message,"MBsy8");
	}
	
	try
	{
		var updaterec = new GlideRecord('rm_enhancement');
		updaterec.addQuery('sys_id', target);
		updaterec.query();		
		
		while(updaterec.next())
			{
			updaterec.u_roi_reporting_status = enhst;//this is a custom field
			updaterec.update();
			
		}
	}
	catch(exc)
	{
		gs.log("ERROR!:" + exc.message,"MBsy8");
	}
	
})(current, previous);

Wirasat
Tera Guru

This article may be related to the issue you are having.

https://www.servicenowguru.com/scripting/business-rules-scripting/fixing-before-query-business-rules-flaw/

Mrigank Gupta
Giga Expert

QUery Business Rule, is not about firing a new query.

Query business rule runs when the table is being queried. You can use "current.addQuery()" in it. This will be appended to the already existing query which result in further filtering of records. It is better in performance if you compare it with ACL.

 

You can achieve the requirement by normal after Business rule.

Can you tell exactly what needs to be done, so that I can help you out in achieving it.