How To Use setValue() In A GlideRecord Query

What Is The setValue() Method? When you’re building out a GlideRecord query, you’ll frequently want to set a value, in your while loop. The setValue() method in a GlideRecord query is a method that allows …

business rule

What Is The setValue() Method?

When you’re building out a GlideRecord query, you’ll frequently want to set a value, in your while loop.

The setValue() method in a GlideRecord query is a method that allows you to update the value of a field in a GlideRecord object.

A GlideRecord is a class that is used to perform operations on a table, such as querying, inserting, updating and deleting records. So the setValue() method is just used to update the value of a field within a GlideRecord object by passing the field name and the new value as arguments.

Once the field is updated, the update() method is used to save the changes to the record in the database. We’ll cover this in the example below.

It is definitely most commonly done in a while loop, but there are certainly use cases of using it outside of one.

It’s also worth mentioning that because setValue() is a GlideRecord method, that it is a server side method and should not be used on the client. There are other methods for setting values, client side – this is not one of them.

This method can be useful for updating a specific field value for multiple records, for example, in an import process where you need to update a specific field for many records.

Go here if you want to better understand GlideRecord queries in ServiceNow,

Example Of The setValue() Method

The above is an example of using the setValue() method in a GlideRecord query.

The above is also an OOB example, written by ServiceNow and this is shipped with every instance.

So the syntax is:

object.setValue(fieldName, value);

So if we were to create our own example of using setValue(), it would look like this.

//create a new GlideRecord object for the 'incident' table
var gr = new GlideRecord('incident');

//query to retrieve all open incidents
gr.addQuery('active', true);
gr.query();

//loop through the query results
while(gr.next()) {
   //set the value of the 'short_description' field to 'Test Description' for all incidents
   gr.setValue('short_description', 'Test Description');

   //update the record in the database
   gr.update();
}

If we were to break this apart, line by line – this is what we’d have.

  1. The first line creates a new GlideRecord object for the ‘incident’ table.
  2. The second line adds a query to retrieve all open incidents by checking if the ‘active’ field is true.
  3. The third line runs the query to retrieve the records.
  4. The while loop iterates over the result set of the query.
  5. The fourth line uses the setValue() method to set the value of the ‘short_description’ field to ‘Test Description’ for the current incident in the loop.
  6. The fifth line uses the update() method to save the changes to the record in the database.

In this example, the script retrieves all open incidents, then updates the ‘short_description’ field of all incidents to ‘Test Description’ and saves the changes in the database.

This could be useful for example to update the description for all open incidents of a specific category with a specific message.

This is obviously just a super simple example, but we want to showcase that so you better understand it.



What Do You Think About This Article?

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x