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.

3 REPLIES 3

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:

RakeshM49470519
Tera Guru

Hello @zee15 ,

Just to confirm the requirement — should the error message be displayed only when certain conditions are met (like based on field values), or is it meant to be a persistent alert/error message that appears on form load and never disappears regardless of user interaction?