In ServiceNow, `g_form` is client side object used within client scripts to interact with form fields and manage form functions on the client side. It offers a range of methods to manipulate form elements, validate user input, and dynamically control form behavior.
Here’s a list of common g_form methods and their descriptions:
1. Field Manipulation–
1) g_form.getValue(‘fieldName’)
It retrieves the value of a field on the form and we can use it later according to our need
example: var short_description= g_form.getValue(‘short_description‘);
2) g_form.setValue(‘fieldName’, value)
It sets the value of a field on the form.
example: g_form.setValue(‘short_description‘, ‘Your Text’);
3) g_form.clearValue(‘fieldName’)
Remove value from specific field on the form.
Example: g_form.clearValue(‘state’);
2. Field Visibility and Read-Only Behavior
1) g_form.setVisible(‘fieldName’, true/false)
It controls whether a field is visible on the form or not. Displays the field if true. Hides the field if false. If the field is hidden, the space is left blank. This method cannot hide mandatory fields with no value
Example: g_form.setVisible(‘description’, false); // Hides the field
g_form.setDisplay(fieldName, isVisible)
Displays the field if true. Hides the field if false. This method cannot hide mandatory fields with no value. If the field is hidden, the space is used to display other items
g_form.setDisplay(‘description’, false); // Hides the field
g_form.setReadOnly(fieldName, isReadOnly)
It makes a field read-only or editable.
g_form.setReadOnly(‘description’, true); // Makes the field read-only
g_form.setMandatory(fieldName, isMandatory)
Makes a field mandatory (requiring a value) or not.
g_form.setMandatory(‘description’, true); // Makes the field mandatory
g_form.isMandatory(fieldName)
Checks if a field is mandatory. Returns true or false accordingly.
g_form.setDisabled(FieldName, Value)
Grays out field and makes it unavailable
g_form.setDisabled(‘short_description’,true);
g_form.hideRelatedList(ListName)
This method will hide particular related list on form
g_form.hideRelatedList(‘incident_list’);
g_form.showRelatedList(ListName)
This method will show particular related list on form
g_form.showRelatedList(‘incident_list’);
g_form.hideRelatedLists()
This method will hide all related lists on form
g_form.hideRelatedLists();
g_form.showRelatedLists()
This method will show all related lists on form
g_form.showRelatedLists();
3. Field Visibility / Styling
g_form.addDecoration(fieldName, icon,title)
Adds an icon on a field’s label.Adding the same item twice is prevented; however, you can add the same icon with a different title.
g_form.addDecoration(‘caller_id’, ‘icon-star’, ‘Mark as Favorite’, ‘color-green’);
g_form.flash(fieldName, color, time)
This method is used to Flashes the specified color for a specified duration of time in the specified field.
g_form.flash(‘caller_id’, ‘#FFFACD’, 0);
4. Field Action and State
g_form.showFieldMsg(fieldName, message, type, scrollForm)
Displays a message next to a field (information, warning, or error message).
g_form.showFieldMsg(‘description’, ‘This field is important’, ‘info’);
g_form.hideFieldMsg(fieldName)
Hides any message that was shown for a field.
g_form.hideFieldMsg(‘incident_description’);
g_form.hideAllFieldMsgs(type)
Hides all messages that were shown for a field.
Type parameter is optional
g_form.hideAllFieldMsgs();
5. Attachments enabling –
g_form.disableAttachments()
Prevents the user attachment icon being hide
g_form.disableAttachments ();
g_form.enableAttachments()
Allows customer file attachments to be added. Shows the paper clip icon. g_form.enableAttachments ();
6. Field and Form Validations
g_form.addErrorMessage(message)
Adds a global error message to the form (not tied to a specific field).
g_form.addErrorMessage(‘This is a global error message’);
g_form.addInfoMessage(message)
Adds a global informational message to the form.
g_form.addInfoMessage(‘This is an informational message’);
g_form.clearMessages ()
Removes all informational and error messages from the top of the form.
g_form.clearMessages ();
g_form. showFieldMsg(FieldName, Message)
This method will display Informational or Error or Warn message under the specified form field (either a control object or the name of the field)
g_form.showErrorBox(FieldName, Message)
It is an alternate method that does not require the type parameter.
g_form.showFieldMsg(‘cmdb_ci’,’Atleast select one service app’,’info’);
7. Field Types and Options
g_form.getControl(fieldName)
Retrieves the DOM element (control) associated with a field on the form.
g_form.getOption(fieldName, value)
Retrieves the option label associated with a specific value for choice-type fields.
The option value Returns null if the field is not found or the option is not found
var label = g_form.getOption(‘state’, ‘1’); // Retrieves the label for the value ‘1’
g_form.addOption(fieldName, value, label)
Adds a new option to a choice field.
g_form.addOption(‘state’, ‘3’, ‘New State’);
g_form.removeOption(fieldName, value)
Removes an option from a choice field.
g_form.removeOption(‘state’, ‘3’);
g_form.clearOptions(fieldName)
Removes all options from the choice list.
g_form.clearOptions(‘category’);
8. Reference Fields
g_form.getReference(fieldName, callback)
Retrieves the reference record for a reference-type field.
function Warning: This requires a call to the server so using this function will require additional time and may introduce latency to your page
var ref = g_form.getReference(‘assigned_to’, assignedTo);
9. Form Control
g_form.save()
Saves the current form.
g_form.submit()
Submits the form (typically used with client scripts that handle form submissions).
10. Sections
g_form.setSectionDisplay(sectionName, display)
Shows or hides a section Works in both tab and flat modes. This method is available starting with the Fuji release
g_form.isSectionVisible(sectionName)
Returns true if the section is visible Returns false if the section is not visible or does not exist. This method is available starting with the Fuji release
g_form.getSectionNames()
Returns all section names, whether visible or not, in an array This method is available starting with the Fuji release
11. Special Use Cases
g_form.getTableName()
Retrieves the name of the current table that the form is based on.
g_form.getActionName()
Returns the most recent action name or, for a client script, the sys_id of the UI Action clicked Note: not available to Wizard Client Scripts
g_form.isNewRecord()
Returns true if the record has never been saved Returns false if the record has been saved
These are the most commonly used g_form methods in client scripts for manipulating forms in ServiceNow.