How to Display g_form.addErrorMessage() Permanently in ServiceNow.

zee15
Tera Contributor

Hi,

I have a requirement where an error message added using g_form.addErrorMessage() needs to be displayed permanently on the form.

Currently, I am adding the error message through a client script. The message appears initially, but after some time it automatically disappears. I understand that this is the out-of-box (OOB) behavior in ServiceNow.

Is there a way to override this behavior and make the error message persist on the form? If so, I would appreciate any guidance or best practices to achieve this.

Thanks in advance for your help.

10 REPLIES 10

Naveen20
ServiceNow Employee

You may use a UI Macro or form section instead

If the message is tied to a specific condition (like a field validation or record state), consider rendering a persistent visual element rather than relying on the transient notification system:

 
 
javascript
// Show/hide a dedicated field or HTML field on the form
g_form.setDisplay('u_error_banner', true);
g_form.setValue('u_error_banner', 
    '<div style="color:#c00;font-weight:bold;padding:8px;border:1px solid #c00;border-radius:4px;">' +
    'Your persistent message here</div>');

 create a macro/HTML variable or a dedicated field on the form specifically for this purpose. This is the cleanest approach for production use since you're not fighting the platform's OOB behavior.

Tanushree Maiti
Kilo Patron

Hi @zee15 

 

To make an error message persist permanently on a ServiceNow form, you can use g_form.showFieldMsg() instead of g_form.addErrorMessage()

g_form.showFieldMsg('<field_name>','Your message','error');

 

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin:

Hi @Tanushree Maiti I know we can use g_form.showFieldMsg() but we have a specific requirement. we need g_form.addErrorMessage() only

Hi @zee15 

 

To maintain a message after a form submission or update, you can use a server-side onDisplay Business Rule to trigger a message that renders when the form finishes loading.

You can use the GlideSystem (gs) API to generate the message:

gs.addInfoMessage('message to be displayed');

 

Refer: https://developer.servicenow.com/dev.do#!/reference/api/zurich/server_legacy/c_GlideSystemAPI#r_GS-a...

 

Please mark this response as Helpful & Accept it as solution if it assisted you with your question.
Regards
Tanushree Maiti
ServiceNow Technical Architect
Linkedin: