How To Save A Record Using g_form.save() On The Client

What Is g_form.save() There are so many use cases for this to be implemented. The scenario is that you’re working with a form, in some client scripting capacity – client script, UI Action, etc. You …

g_form.save() client script or ui action

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

What Is g_form.save()

There are so many use cases for this to be implemented.

The scenario is that you’re working with a form, in some client scripting capacity – client script, UI Action, etc.

You want to automatically save the current record, with all of the field data being immediately saved. You can accomplish this in a single line of code.

Here comes g_form.save();

As soon as this line is hit in a script, then the form will immediately be saved and will be saved to the server. The form will then reload.

We’ve covered 2 examples below, one simple and one more complex with a callback function, that alerts the user of what happened.

You can take these scripts and use them as templates.

How To Use g_form.save() – Script Examples

Let’s cover 2 ways to use g_form.save().

In ServiceNow, you can use the g_form object to access and manipulate various aspects of a form in a client script. The save() method is a method of the g_form object that allows you to save the current form.

Here is an example of how you can use the save() method in a client script in ServiceNow:

function onSubmit() {
   // make some changes to the form
   g_form.setValue('short_description', 'Updated short description');

   // save the form
   g_form.save();
}

In this example, the onSubmit() function is called when the user submits the form. The function makes a change to the short_description field on the form using the setValue() method of the g_form object, and then calls the save() method to save the changes to the form.

Keep in mind that the save() method will only save the changes to the form if the form is valid. If there are any validation errors on the form, the save() method will not save the changes and the form will not be submitted.

The above is a simple example and should suffice.

If you want to make it a little more complex, take a look at the below script.

You can also pass a callback function as an argument to the save() method to specify code to be executed after the form is saved. For example:

function onSubmit() {
   // make some changes to the form
   g_form.setValue('short_description', 'Updated short description');

   // save the form and execute a callback function after the save is complete
   g_form.save(function() {
      alert('Form saved successfully!');
   });
}

In this example, the callback function will display an alert message after the form is saved successfully.

Both do the same thing, one just adds an alert message to the form.

Alternatively, you can add an info message with g_form.addInfoMessage(), which is a better user experience than an alert box, typically.

Why Use g_form.save()

There are several scenarios where you might want to use the g_form.save() method in ServiceNow:

  1. When you want to save changes to a form that the user has made: You can use the g_form.save() method to save changes that the user has made to a form, such as updating field values or adding new records.
  2. When you want to submit a form: The g_form.save() method can be used to submit a form if the form is valid. This is useful if you want to submit a form programmatically or if you want to add additional processing to the form submission process.
  3. When you want to execute code after a form is saved: As mentioned earlier, you can pass a callback function as an argument to the g_form.save() method to specify code to be executed after the form is saved. This can be useful if you want to perform additional actions after the form is saved, such as displaying a message to the user or redirecting the user to another page.
  4. When you want to save a form in the background: The g_form.save() method can be used to save a form in the background without requiring user interaction. This can be useful if you want to save a form automatically when certain conditions are met, such as when a field value is changed or when a timer expires.

Overall, the g_form.save() method is a useful tool for saving and submitting forms in ServiceNow, and it can be used in a variety of scenarios to meet the specific needs of your application.



What Do You Think About This Article?

0 0 votes
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback

[…] 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(). […]

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