A Hidden Way to Show Form Messages in ServiceNow

I was recently doing some research on g_form methods that ServiceNow provides to display messages on the form. The main two that I was familiar with were “addInfoMessage” and “addErrorMessage”; but what if there were more? I have definitely seen other messages displayed that was neither of these two… After doing some digging for the “hidden” method I was rewarded. The new method I found was called “_addFormMessage”. Note that this is not documented and is prefixed with an underscore indicating that it was meant to be used as an internal ServiceNow method so it is subject to change although it has been there since at least Eureka if that tells you anything :). Enjoy!

g_form._addFormMessage('Testing a form message at the top');

Ok, so that’s fine and dandy, but what if at some point in the future ServiceNow did deprecate this command; here’s one way you could re-implement it yourself using jQuery. Note that in order to make the command accessible via “g_form” we need to create it as a function off of the GlideForm object:

String.prototype.capitalizeFirstLetter = function() {
	return this.charAt(0).toUpperCase() + this.slice(1);
};

GlideForm.prototype.setNewFormMessage = function (message) {
	var tableId = g_form.getTableName().toString().capitalizeFirstLetter();

	var htmlBlock = '<span class="section " id="section-test" data-header-only="false"><div id="' + tableId + '.section_header_spacer"></div><div class="outputmsg_container" style="display: block;" id="output_messages"><button class="btn btn-icon close icon-cross" onclick="GlideUI.get().clearOutputMessages(this); return false;"><span class="sr-only">Close Messages"</span></button><div class="outputmsg_div"><div class="outputmsg outputmsg_ notification notification-"><img class="outputmsg_image" src="images/outputmsg__24.gifx" alt=""><span class="outputmsg_text">' + message + '</span></div></div></div>';

	jQuery("[tab_caption=" + tableId + "]").after(htmlBlock);
};


var message = 'testing 123';
g_form.setNewFormMessage(message);

Leave a Reply

Your email address will not be published. Required fields are marked *