What Is The g_form() Object?

What Is g_form()? Fundamentally, g_form is a global JavaScript object that provides a number of methods for interacting with and manipulating forms in the ServiceNow platform. It essentially just allows ServiceNow developers to interact with …


Buy The "ServiceNow Developer's Manual" Now

We've packed over a decade of ServiceNow Development advice, into a single e-book.

Buy It Now
Stripe Climate Badge

The Snowball will contribute 5% of your purchase to remove CO₂ from the atmosphere.

What Is g_form()?

Fundamentally, g_form is a global JavaScript object that provides a number of methods for interacting with and manipulating forms in the ServiceNow platform.

It essentially just allows ServiceNow developers to interact with forms, client side.

Want to make a field read only, if a user enters a certain value? Want to hide a field, if a certain field hasn’t been populated yet? Want to make a field mandatory, if an incident is a certain type?

You can perform all of these, and much more, using the g_form object in client scripts.

Let’s review some examples.

Some of the things that can be done using the g_form object include:

  • Getting and setting field values
  • Enabling and disabling fields
  • Showing and hiding fields
  • Making fields read-only
  • Setting the value of a field to be the same as another field
  • Setting the values of multiple fields at once
  • Adding and removing sections from a form
  • Adding and removing choice lists from a field

These are just some examples of the functionality provided by the g_form object.

It is widely used in ServiceNow client script and UI policies.

g_form() is a very common object to use in ServiceNow. And each time a form loads, it’s almost a guarantee that some client script is utilizing it to make decisions.

Remember that this is a client side object, and cannot be accessed on the server (because this wouldn’t make much sense).

ServiceNow admins just getting started might copy/paste this into a business rule. We will cover why that won’t work, and doesn’t make any sense to even try, in this post.

How Is g_form Used?

It really couldn’t be easier for ServiceNow admins to use the g_form object and access methods.

You really just need to make sure that you fundamentally understand the difference between client side and server side. g_form is a global object that is only accessible, on the client.

You can even save a record using g_form. Take a look at our post on how to save a record, using g_form.save().

To use g_form() in a client script, you’ll use the following syntax:

g_form.setDisabled('fieldname', true);

Once you have the g_form() object, you decide on a method, and then pass in whatever parameters you need.

Here is a table that covers some common methods of the g_form object in ServiceNow.

Take a look at the “Coding example” tab, to better understand how it may be used.

Method nameDescriptionCoding example
setValue(fieldName, value)Sets the value of the specified fieldg_form.setValue("priority", "high");
getValue(fieldName)Gets the current value of the specified fieldvar priorityValue = g_form.getValue("priority");
setReadOnly(fieldName, boolean)Makes the specified field read-only or editableg_form.setReadOnly("priority", true);
setDisabled(fieldName, boolean)Enables or disables the specified fieldg_form.setDisabled("priority", true);
setVisible(fieldName, boolean)Shows or hides the specified fieldg_form.setVisible("priority", true);
addOption(fieldName, value, label)Adds an option to the specified field choice listg_form.addOption("priority", "high", "High");
clearOptions(fieldName)Clears all options from the specified field choice listg_form.clearOptions("priority");
setMandatory(fieldName, boolean)Makes the specified field mandatory or optionalg_form.setMandatory("priority", true);
showSection(sectionName)Shows the specified sectiong_form.showSection("priority_info");
isVisible(fieldName)Returns true if the field specified is visiblevar isVisible = g_form.isVisible("priority");
getLabelOf(fieldName)Returns the label of the field specifiedvar label = g_form.getLabelOf("priority");

Keep in mind that this is not an exhaustive list of all the methods that the g_form object provides, but it covers most commonly used ones in a client script.

Also, some methods have different options available for example showSection have options to show multiple sections at once.

It’s recommended to check the official ServiceNow documentation for a complete list of methods, and a detailed explanation of their usage and options available.

Using g_form() In An Example Script – Make Category Editable If The State Is New

This use case may never come up, but I just want to prove a point here.

We’ll even store the g_form() value in a JavaScript variable, which we’ll then refer to later in the script.

We’re providing this example because it will showcase how to USE the g_form object, not because someone will cut/paste this script and actually use it.

function onLoad() {
    // Get the current value of the "state" field
    var state = g_form.getValue('state');
    // Check if the state is "New"
    if (state === 'New') {
        // Make the "category" field editable
        g_form.setReadOnly('category', false);
    }
}

Let’s review what this client side g_form() script does.

This script would typically be associated with the form, for example by using the “onLoad” client script. It uses the getValue method of the g_form object to get the current value of the “state” field, and then checks the value of the state using a simple if statement. If the value of the state is ‘New’, it uses the setReadOnly method to make the “category” field editable.

It’s worth noting that making a field editable/non-editable using g_form.setReadOnly(field,value) where value is a boolean. Also, to make a field mandatory/optional use setMandatory(field,value) where value is a boolean.

Please note that this is just an example and you should adjust it to match your actual use case and the specific requirements of your system.

We’re not fans of just using cut/paste code from the internet, without truly understanding it.

Summarizing g_form()

As you’ve seen, g_form() is an easy and powerful tool for ServiceNow Admins to know.

Try to make sure that you don’t get intimidated by coding in ServiceNow.

Let’s finish off with just a final review of what it can really do, and why you need to learn it if you’re going to be administering ServiceNow.

In ServiceNow, the g_form object is a global JavaScript object that provides a number of methods for interacting with and manipulating forms.

It allows developers to easily access and modify the fields, sections, and other elements of a form. It provides a convenient way to interact with forms in ServiceNow and perform various actions on the fields and sections in the form.

The g_form object can be used in client scripts and UI policies.

Using the g_form object, developers can perform tasks such as setting and getting field values, enabling or disabling fields, showing or hiding fields, making fields read-only, setting the value of a field to be the same as another field, setting the values of multiple fields at once, adding and removing sections from a form, and adding and removing choice lists from a field.

It’s a really powerful tool that greatly improves the ease of form manipulation, and helps you do your job as a ServiceNow admin.



What Do You Think About This Article?

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Product Image

Sandra just purchased A ServiceNow Developer’s Manual 17 Hours ago from Garland TX.

Buy Now
0
Would love your thoughts, please comment.x
()
x