Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Slow performance Business Rule

Leonel Sandroni
Tera Guru

Hi there!

 

I've created a BR in order to update a value from demand tablo to an enhancement table. It's a simple query and no much more...

but for some reason it runs very slow

 

the business rule is an 'after' type with a particular field change and it should modify a field in another table with the same value so:

LeonelSandroni_0-1737639216811.png

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

	// it finds the related enhancement records
        var udd = new GlideRecord('rm_enhancement');
        udd.addQuery('enhancement', current.sys_id); 
        udd.query();

        // set the due date with the same value
        while (udd.next()) {
            udd.u_due_date = current.requested_by; 
            udd.update();
        }


})(current, previous);

 

I tested this BR in PDI and it works fast. 

I used the field 'enhancement' because it is the reference field from demand table to enhancement table and because I couldn't find a reference demand field from the enhancement table.

 

any suggestion?

1 ACCEPTED SOLUTION

Keep the if condition and update the below query condition 

udd.addQuery('sys_id', current.enhancement);
 
let me know if still not working. 


Thanks,
Rajashekhar Mushke
Rising star : 2022 - 2024
Community Leader -2018
Connect me on LinkedIn : Rajashekhar Mushke

View solution in original post

10 REPLIES 10


@Leonel Sandroni wrote:

Ok, in this way, just check and update the only record that I need!!

 

This is the solution. So...for the next time...When I need to update only one matching record I should use IF inestead WHILE. Thanks!!!


Not quite.

 

  • if (udd.next()): Processes only the first record returned by the query.
  • while (udd.next()): Processes all records returned by the query.

 

The code sample I provided is likely the most appropriate way to achieve your requirement. You can also use GlideRecord get: https://www.youtube.com/watch?v=0UNITC4CX7A

 

Pass one or two parameters to get a record by sys_id or any other field/value pair quickly and easily. #servicenowdyk

Ok, with IF...If there were more than one record matched it wouldn't work. I get it! I will try with your suggestion.

Bert_c1
Kilo Patron

Try adding:

 

udd.setWorkflow(false);

 

before the 

 

udd.update();

 

line, to prevent other BRs from running if your goal is to only set that field on the rm_enhancement table.

 

Or, put in an enhancement request for:

 

https://www.rfc-editor.org/info/rfc9564

 

That will speed things up.

Sheldon  Swift
ServiceNow Employee
ServiceNow Employee

Hi @Leonel Sandroni - What is your requirement?

 

There is no enhancement field in the rm_enhancement table. When this script runs, a message like 'Invalid query detected, please check logs for details [Unknown field enhancement in table rm_enhancement]' should appear in the system log, indicating the issue.

 

If you query a table using a field that does not exist (e.g., enhancement in this case), the platform ignores that condition when constructing the query. As a result, the query matches all records in the table. This means the while (udd.next()) loop will iterate over every record in the rm_enhancement table and update the u_due_date field for all records, which is why the script takes so long to execute.

 

It might run faster in your PDI if there are fewer records in the rm_enhancement table compared to some other instance.

Yes @Sheldon  Swift you're right!! it's checkin all the enhancement records because there is no enhancement field. 

 

Anyway, I don't know how to accomplish this requirement. I couldn't find a reference field to a demand table from the enhancement table. I need to modify due_date value in enhancement when the due_date in demands changes (with same value).