Clear error message (gs.addErrorMessage();) after X seconds in business rule.

Raghu35
Kilo Contributor

Clear error message (gs.addErrorMessage();) after 5 seconds in business rule. How can I do that. I've used gs.sleep(); but it didn't worked for me. It is pausing the entire script.

3 REPLIES 3

Mark Skinner
Mega Guru

HI

 

gs.addErrorMessage and gs.addInfoMessage cannot be scripted to remove the message. It is up to user input to remove them (clicking the X) 

 

gs.sleep() is used to pause the script for a desired amount of time and cannot be used to remove messages.

 

So unfortunately the only way for the messages to be removed if via the user.

 

if this has been helpful please mark as so. If this answered your question please mark as correct  

I've used gs.sleep();  along with gs.flushMessages();

The SN Nerd
Giga Sage
Giga Sage

Business rules are executed synchronously on the server (unless async is used).
The API gs.addErrorMessage() queues a message to be displayed to the end user in their web browser/device.
Alas, adding a 5 seconds sleep will do nothing but add 5 seconds to the Business Rule execution time.

You need to add an onLoad Client Script to do this after your Business Rule returns.
You will need your condition to mimic the Business Rule conditions, otherwise, all messages will be cleared after 5 seconds.

For example, if your Business rule condition is 'State' is "In Progress" in the Incident [incident] table, your onLoad Client Script for Incident would have this code:

if (g_form.getValue('state') == 2) { // IN PROGRESS
setTimeout(g_form.clearMessages, 5000); // 5 SECONDS
}

https://www.w3schools.com/jsref/met_win_settimeout.asp


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022