How To Use orderBy() To Sort By A Field Value

Why Use The orderBy() Method? What is orderBy() and why would you ever use it? Great question, we’d love to address it below. There are a number of reasons why you might want to order …

orderBy() in a background script

Why Use The orderBy() Method?

What is orderBy() and why would you ever use it?

Great question, we’d love to address it below.

There are a number of reasons why you might want to order records by a field name in ServiceNow.

One reason is to make it easier for users to find specific records. For example, if you are displaying a list of incidents to a user, you might want to sort the records by the priority field so that the most important incidents are displayed at the top of the list.

Another reason to order records by a field name is to make it easier to analyze or report on the data. For example, if you are creating a report that shows the number of incidents by priority, you might want to sort the records by the priority field so that the data is easier to understand and analyze.

If you don’t specify an orderBy() field, then the GlideRecord record set will just be the data, but in an unknown or undetermined order.

Finally, ordering records by a field name can also make it easier to perform tasks such as updating or deleting records in bulk, as you can more easily identify which records need to be processed.

You can use this idea and apply it to your own instance.

Overall, ordering records by a field name can help to improve the usability and effectiveness of your ServiceNow application.

You’ll need to be an admin user in ServiceNow to run this script below.

How To Use orderBy() In A Script

In ServiceNow, you can use the orderBy() method of the GlideRecord class to specify the order in which records should be returned by a query.

Here is an example of how you might use the orderBy() method:

var gr = new GlideRecord('incident');
gr.addQuery('active', 'true');
gr.orderBy('priority');
gr.query();
while (gr.next()) {
   // process the record
}

Let’s walk through each line of code above, so we can show you what’s going on here.

This example creates a new GlideRecord for the incident table and specifies that only active records should be included in the results. The orderBy() method is then used to specify that the results should be sorted by the priority field. The query() method is called to execute the query, and the next() method is used to iterate through the resulting records.

If you were to order by the priority, as shown above for 50 records, you’d get the below printed out.

Background Script Response

How To Use orderBy DESC

You can pass a field and a parameter in, as shown below, to order by a descending value.

By default, the orderBy() method will sort the records in ascending order. If you want to sort the records in descending order, you can pass the DESC keyword as a second parameter to the orderBy() method.

For example:

gr.orderBy('priority', 'DESC');

This will sort the records by the priority field in descending order.

You can also use the orderByDesc() method to sort the records in descending order. This method is equivalent to calling orderBy() with the DESC keyword as the second parameter.

Are there other ways you’ve used orderBy() in ServiceNow?



What Do You Think About This Article?

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