How to sort tickets by created date with scripting

Ashwini Jadhao
Giga Guru

Hi,

I want to add filter condition to sort tickets by created date means sort it from z-a. 

Could you please help me to achieve this by script include. This script include I have to call in filter condition.

 

Thank you

2 ACCEPTED SOLUTIONS

Amit Gujarathi
Giga Sage
Giga Sage

HI @Ashwini Jadhao ,
I trust you are doing great.

  1. Go to the "System Definition" module in ServiceNow and click on "Script Includes."
  2. Click on "New" to create a new Script Include.
  3. Provide a name for the Script Include, such as "TicketSortUtils."
  4. In the script section, you can use the following code:

 

var TicketSortUtils = Class.create();
TicketSortUtils.prototype = {
  initialize: function() {},

  sortByCreatedDateDescending: function(query) {
    query.orderByDesc('sys_created_on');
  },

  type: 'TicketSortUtils'
};

 

  1. Save the Script Include.

Now, you can use this Script Include in the filter condition of your ticket queries to sort them by created date in descending order.

Here's an example of how you can use this Script Include in a filter condition:

 

var sortUtils = new TicketSortUtils();
sortUtils.sortByCreatedDateDescending(current);

 


Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



View solution in original post

Teju Dhoran
Kilo Sage
Kilo Sage

Hi,

Not sure why you want to call it from Script Include.

If your code is running on Server Side then you can use it like below:

var rec = new GlideRecord('table_name');
    rec.orderBy('sys_created_on');  // if want to sort A-Z
    // rec.orderByDesc('sys_created_on');  // if wanto to sort Z-A
    rec.query();
    while (rec.next()) {
        return rec.column_name;
    }

 

 

View solution in original post

3 REPLIES 3

Amit Gujarathi
Giga Sage
Giga Sage

HI @Ashwini Jadhao ,
I trust you are doing great.

  1. Go to the "System Definition" module in ServiceNow and click on "Script Includes."
  2. Click on "New" to create a new Script Include.
  3. Provide a name for the Script Include, such as "TicketSortUtils."
  4. In the script section, you can use the following code:

 

var TicketSortUtils = Class.create();
TicketSortUtils.prototype = {
  initialize: function() {},

  sortByCreatedDateDescending: function(query) {
    query.orderByDesc('sys_created_on');
  },

  type: 'TicketSortUtils'
};

 

  1. Save the Script Include.

Now, you can use this Script Include in the filter condition of your ticket queries to sort them by created date in descending order.

Here's an example of how you can use this Script Include in a filter condition:

 

var sortUtils = new TicketSortUtils();
sortUtils.sortByCreatedDateDescending(current);

 


Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Teju Dhoran
Kilo Sage
Kilo Sage

Hi,

Not sure why you want to call it from Script Include.

If your code is running on Server Side then you can use it like below:

var rec = new GlideRecord('table_name');
    rec.orderBy('sys_created_on');  // if want to sort A-Z
    // rec.orderByDesc('sys_created_on');  // if wanto to sort Z-A
    rec.query();
    while (rec.next()) {
        return rec.column_name;
    }

 

 

Ashwini Jadhao
Giga Guru

Thank you Teju and Amit for your help!

I am preferring the  2nd way to use it directly in my business rule, not using script include.