I
nformation messages are a great way to provide UI feedback to end users as they interact with the various forms in your instance. This article describes the various methods you can use to display information messages to the users accessing your ServiceNow system.
Client-side UI Messages
The following methods are designed for use in client-side scripting (primarily client scripts and UI policies). As such, they are used on standard forms and on catalog forms and can run on load or submit of a form, or on change of a field value.
Function/Method | Description | Usage |
---|---|---|
addInfoMessage(message) | Displays an informational message at the top of the form with a blue information icon and a light-blue background. | g_form.addInfoMessage('Form Info Message Text'); |
addErrorMessage(message) | Displays an error message at the top of the form with a red error icon and a light-red background. | g_form.addErrorMessage('Form Error Message Text'); |
clearOutputMessages | Hides ALL form info and error messages. There is no way to remove form info and error messages individually. | GlideUI.get().clearOutputMessages(); |
showFieldMsg(input, message, type, [scrollForm]) | Displays either an informational or error message under the specified form field (either a control object or the name of the field). Type may be either "info" or "error." If the control or field is currently scrolled off the screen, it will be scrolled to. A global property (glide.ui.scroll_to_message_field) is available that controls automatic message scrolling when the form field is offscreen (scrolls the form to the control or field). Optional: Set scrollForm to false to prevent scrolling to the field message offscreen. Parameters: input - specifies the name of the field or control. message - the message to be displayed. type - error or info. scrollForm (optional) - true to scroll to message if offscreen, false to prevent this scrolling. | //Field info message g_form.showFieldMsg('priority','Priority is low.','info'); //Field error message g_form.showFieldMsg('impact','Priority is high!','error'); |
hideFieldMsg(input, [clearAll]) | Hides info and error messages for a single field. Parameters: input - specifies the name of the field. clearAll (optional) - boolean parameter indicating whether to clear all messages. If true, all messages for the field will be cleared; if false or empty, only the last message will be removed. | g_form.hideFieldMsg('impact', true); |
hideAllFieldMsgs([type]) | Hides all field info and error messages. Optional: Provide type to hide only "info" or "error" messages. | g_form.hideAllFieldMsgs(); |
flash(widgetName, color, count) | Flashes the specified color the specified number of times in the field. Parameters: widgetName - Specifies the element with (table name).(fieldname). color - RGB or CSS color count - How long the label will flash. use 2 for a 1-second flash use 0 for a 2-second flash use -2 for a 3-second flash use -4 for a 4-second flash | g_form.flash("incident.number", "#CC0000", -2); |
Server-side UI Messages
Although messages will always be displayed client-side, they are often initiated from a server-side script like a business rule, record producer script, or script include. Messages initiated from server-side scripts can appear at the top of any form or list and are typically triggered by a database action such as a record insert, update, or delete.
Function/Method | Description | Usage |
---|---|---|
addInfoMessage(message) | Displays an informational message for the current session with a blue information icon and a light-blue background. Can also include HTML in the message! Note that I've replaced the greater than and less than symbols with brackets in the HTML usage example to get it to display correctly here. You'll need to change the brackets back to standard HTML to get it to work in your instance. | gs.addInfoMessage('Session Info Message Text'); //Info message with HTML formatting //Create the html contents of the information message var link = '[a href="incident.do?sys_id=' + current.sys_id + '" class="breadcrumb" ]' + current.number + '[/a]'; var message = 'Incident ' + link + ' has been created. '; message += 'Thank you for your submission.'; //Add the information message gs.addInfoMessage(message); |
addErrorMessage(message) | Displays an error message for the current session with a red error icon and a light-red background. Can also include HTML in the message! | gs.addErrorMessage('Session Error Message Text'); |
flushMessages() | Clears any existing session info or error messages to prevent them from being displayed. | //Clear any session info or error messages gs.flushMessages(); |
UI Notifications
UI Notifications are an unusual class that can be used for refreshing elements such as the navigation pane or for displaying messages in response to changes in database tables. For more information about UI Notifications, check out the following articles:
Is there a way to detect if a field message is displaying or not?
It would be nice if there was a way to detect if there is a field message displayed on the page without searching document for a div with class=”outputmsg_error” or class=”outputmsg_warning”.
Hey Justin, unfortunately, there isn’t. Too bad there aren’t any good element IDs provided in this case.
Is there is any way to change color of Error Message at client side?
No simple way to change the background color, but you can change the text color by adding HTML to the message as shown in the example above.
Is there a table or doc anywhere that has all the possible Error and Warning messages listed?. It would be nice to be able to suppress the messages you don’t want or need to see.
Anything like that would need to be provided by ServiceNow, but I don’t think that will happen. There are very few messages that appear in the product that aren’t part of standard client scripts, business rules, or UI actions. All you need to do in most cases is examine the code in those places to determine where the error message originates from.
Is there a way to flash a catalog item field?
I answered my own question
label_IO:SYS_ID_OF_VARIABLE_HERE
^^^^So that’s my widgetname for catalog variables.
How we can remove or hide the server UI messages?
Clearing messages (server-side or client-side) is accomplished via the ‘GlideUI.get().clearOutputMessages();’ call shown above in the client-side section.
Hi Mark, it appears some of the CSS coding changed in Geneva. I am getting an error when attemping to apply CSS to the info message in your example. Could you tell me what the updated code would be for: g_form.addErrorMessage(‘[b]Form Error Message Text (Bold)[/b]’); ?
I have a note for a different CSS styling that I found, which may help with the styling…
Set field width:
Fuji: https://servicenowguru.wpengine.com/system-ui/field-styles-service-catalog-variables/
g_form.getControl(‘VARIABLE_NAME’).style.width = ‘150px’;
Geneva: https://community.servicenow.com/message/881984#881984
g_form.getControl(‘VARIABL
Looks like the support for this in client-side messages has been removed in the Geneva release. You can’t inject your own html anymore using the basic methods unfortunately. I’ve updated the documentation above to reflect this. If you’d like to see the behavior in Geneva you’ll need to contact ServiceNow support to see about an enhancement.
Mark, is there a simple way to make the showFieldMsg() messages permanent? All of mine go away after the field they’re attached to is changed.
For example, if I add one to an email address field then you type something in that field and blur it, the message goes away. I want it to stay there permanently.
The only ways to do this are to set up the field message in an ‘onChange’ script so that it’s evaluated each time the field changes (and re-displays accordingly) or to avoid field messages entirely and just use an annotation if the message is always supposed to be there.
Is there a way to extend the addInfoMessage duration? I was hoping it would take an additional parameter like the number of ms or seconds to keep it up without requiring interaction.
None that I’m aware of, but I haven’t had any opportunity to dig into the API. You might get another opinion on the SN community.
How to disable the close option in addInfoMessage. If we use addInfoMessage(“Hi How are you”). It will be displayed at the top of the form and user has an option to close this message.
My requirement is like to make the Message permanently at the top of the form.
Please anyone help me to achieve this.
This might change depending on what version of ServiceNow you’re running, but on the Helsinki version I’ve got as a sandbox you can use an ‘OnLoad’ client script with the following code to hide the close button in the info message container.