Skip to main content

ServiceNow g_form API & Methods

· 3 min read
Kaustubh Kulkarni

The g_form API in ServiceNow provides developers with a way to interact with form elements and controls on ServiceNow forms. With the g_form API, developers can read and write data to form fields, control the visibility and behavior of form elements, and perform a variety of other tasks on forms.

In this section, we'll explore the g_form API and its various methods, along with examples of how to use them.

Getting the g_form Object To get started with the g_form API, we first need to get the g_form object for the current form. We can do this by calling the getFormElement() method, which returns a reference to the form's HTML element.

// Get the g_form object for the current form
var gForm = new GlideForm('incident');

Once we have the g_form object, we can perform a variety of operations on it using the available methods.

Setting Field Values One of the most common tasks in working with forms is setting the value of a field. We can do this using the setValue() method.

// Set the value of the short_description field to "New incident"
gForm.setValue('short_description', 'New incident');

In this example, we're setting the value of the short_description field to "New incident". We use the setValue() method and pass the field name and value as arguments.

Getting Field Values We can also retrieve the value of a field using the getValue() method.

// Get the value of the short_description field
var shortDesc = gForm.getValue('short_description');
gs.log(shortDesc);

In this example, we're retrieving the value of the short_description field and logging it to the console. We use the getValue() method and pass the field name as an argument.

Controlling Field Visibility We can control the visibility of a field using the setVisible() method.

// Hide the work_notes field
gForm.setVisible('work_notes', false);

In this example, we're hiding the work_notes field. We use the setVisible() method and pass the field name and a boolean value indicating whether the field should be visible or not.

Enabling and Disabling Fields We can enable or disable a field using the setReadOnly() method.

// Disable the impact field
gForm.setReadOnly('impact', true);

In this example, we're disabling the impact field. We use the setReadOnly() method and pass the field name and a boolean value indicating whether the field should be read-only or not.

Submitting the Form We can submit the form using the submit() method.

gForm.submit();

In this example, we're submitting the form. We use the submit() method to submit the form.

The g_form API provides a powerful set of tools for developers to interact with forms in ServiceNow. With the g_form API, developers can set and retrieve field values, control field visibility and behavior, and perform a variety of other tasks on forms. By understanding the g_form API and its various methods, developers can build more powerful and flexible ServiceNow applications.