Gs.addErrorMessage gives error message multiple times

Community Alums
Not applicable

Hi Team,

 

I have a query I have written a before update BR, and I am comparing the date field. but I am getting the error message multiple times if the condition meets?

 

 

Ankur20_0-1710752283813.png

 

 

Ankur20_2-1710752372739.png

 

 

2 ACCEPTED SOLUTIONS

mhegde1
Kilo Sage

Hi @Community Alums ,

 

If multiple records are updating at a time then yes above error msg will show those many times.

 

thanks,

View solution in original post

Robbie
Kilo Patron
Kilo Patron

Hi @Ankur20,

 

A query business rule can legitimately be called multiple times meaning the message will appear (and be duplicated) each and everytime, however, a good way to make sure the cache is clear is to leverage the below syntax:

 

gs.flushMessages()

 

Call this before your gs.addErrorMessage() call.

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Helpful.

 

Thanks, Robbie

View solution in original post

5 REPLIES 5

mhegde1
Kilo Sage

Hi @Community Alums ,

 

If multiple records are updating at a time then yes above error msg will show those many times.

 

thanks,

Robbie
Kilo Patron
Kilo Patron

Hi @Community Alums,

 

A query business rule can legitimately be called multiple times, however, a good way to make sure the cache is clear is to leverage the below syntax:

 

gs.flushMessages()

 

Call this before your gs.addErrorMessage() call.

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Helpful.

 

Thanks, Robbie

Deepak Shaerma
Kilo Sage

Hi @Community Alums 

Here’s a revised version of your script with corrections:

 

 

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

    // Corrected variable declaration syntax and method name

    var bed = new GlideDateTime(current.u_billing_effective_date); // Ensure the field name is correct

    var fx_account_date = new GlideDateTime(current.account.u_fx_account_active_date); // Ensure the field name is correct



    // Fixed comparison logic by using GlideDateTime comparison methods

    if (bed.before(fx_account_date)) {

        gs.addErrorMessage(“The Billing Effective Date may not predate the FX Account Active Date.”);

    }

})(current, previous);

 


Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help us a lot.
Thanks & Regards 
Deepak Sharma

Amit Pandey
Kilo Sage

Hi @Community Alums 

 

It's likely because the Business Rule is running for each record updates. To prevent this, ensure that your condition is specific enough to only trigger the desired behavior. You can use following function-

flushMessages: function() {
     gs.flushMessages();
}

It clears all messages stored in the GlideSystem message queue in ServiceNow, ensuring only the most relevant messages are displayed to the user.

 

Regards,

Amit