You are currently viewing ServiceNow GlideForm(g_form) Class Functions Demo!!!

ServiceNow GlideForm(g_form) Class Functions Demo!!!

This article is dedicated to demonstrate all functions of ServiceNow GlideForm class. The GlideForm API provides methods to customize forms. The global object g_form is used to access GlideForm methods.

You must remember that these methods are only available at client side i.e. in client side scripting only.

How to add icon on ServiceNow field label?

To decorate a field label you can use addDecoration function of GlideForm Class

Syntax: addDecoration(String fieldName, String icon, String title)
Example: g_form.addDecoration('caller_id', 'icon-star', 'preferred member');

How to show error message on the top of form?

We use addErrorMessage() function to displays the specified error message at the top of the form.

Syntax: addErrorMessage(String message)
Example: g_form.addErrorMessage('This is an error');

How to display a floating form message at top?

In this case we use addFormMessage() message that displays a floating form message at the top of the form detail section.

Syntax: addFormMessage(String message, String type)
g_form.addFormMessage('info message','info');
g_form.addFormMessage('warning message','warning');
g_form.addFormMessage('error message','error');

How to add choice in the list using script?

This amazing thing can be done by using addOption() of GlideForm class. Using this will adds a choice to the end of a choice list field.

Syntax:  addOption(String fieldName, String choiceValue, String choiceLabel)
Example: g_form.addOption('impact', '4', '4 - Really Low');

How to add choice in the list using at a position?

This is done by addOption() function of Glideform class.

Syntax: addOption(String fieldName, String choiceValue, String choiceLabel, Number choiceIndex)
g_form.addOption('impact', '4', '4 - Really Low',1);
//adding '4- Really low' choice.

How to remove information & error messages and clear form?

This can be done by clearAllFormMessages() function of GlideForm class. It Removes informational and error messages added with g_form.addInfoMessage() and g_form.addErrorMessage().

clearMessages(); // clear message
clearAllFormMessages(); // clear all messages
clearFormMessages(String type); // clear message based on type - pass(error/info/warning)

How to clear choice values of field in servicenow?

This is done by using clearOptions function which removes all options from the drop down field.

clearOptions(String fieldName);
g_form.clearOptions('impact'); // Remove options of impact field

How to remove value of text field on form?

This is done by using clearValue() function of GlideForm class.

 clearValue(String fieldName);
function onLoad() {
g_form.clearValue('short_description'); // clear short desc on load

Leave a Reply