Auto ticket closure requirement for incidents and sctask

Thomas98
Tera Expert

Auto ticket closure requirement for incidents and sctask.

 

I have a business requirement. If a ticket is in the "Waiting customer" state for 10 business days be it incident to sctask.

For Sctask: Assigned to is mandatory field, but in most cases, it will be updated, but if its empty I'm ok to over ride this data policy. 

 

For incidents

Assigned to, resolution code and resolute notes are mandatory to resolve an incident.  It's ok to over ride the policy for assigned to, but for resolution code should be updated to "closed by caller" and resolution notes should be " No response from caller" 

 

what will be the best approach? If its business rule can someone please help me with the rule?

3 ACCEPTED SOLUTIONS

Ratnakar7
Mega Sage
Mega Sage

Hi @Thomas98 ,

 

You can create a Scheduled Job in ServiceNow that runs daily and checks for tickets that have been in the "Waiting customer" state for 10 business days.
Here are the steps you can follow:

  1. Create a new Scheduled Job 
  2. Set the Interval /trigger type to "Daily".
  3. In the "Script" field, enter the name of a Script Include (new AutoTicketClosure.run()) that you will create in the next step.
  4. Save the Scheduled Job.

Now, you need to create the Script Include that will contain the logic to close the tickets that meet the criteria. Here's an example of what the Script Include could look like:

 

var AutoTicketClosure = Class.create();
AutoTicketClosure.prototype = {
  initialize: function() {
    // Set the duration for which the ticket should be in "Waiting customer" state before closing it (in business days)
    this.waitingCustomerDuration = 10;
  },

  run: function() {
    var gr = new GlideRecord('incident');
    gr.addQuery('state', 'Waiting customer');
    gr.addQuery('sys_created_on', '<=', gs.daysAgo(this.waitingCustomerDuration));
    gr.query();

    while (gr.next()) {
      // Check if the incident has been assigned to someone
      if (gr.assigned_to.nil()) {
        // Do nothing if the incident has not been assigned to anyone
        continue;
      }

      // Check if the incident has resolution information
      if (gr.u_resolution_code.nil() || gr.u_resolution_notes.nil()) {
        // Set the resolution information if it is missing
        gr.u_resolution_code = 'Closed by caller';
        gr.u_resolution_notes = 'No response from caller';
        gr.update();
      }

      // Close the incident
      gr.setValue('state', 7); // 7 is the value for "Closed"
      gr.update();
    }

    var sctask = new GlideRecord('sc_task');
    sctask.addQuery('state', 'Waiting customer');
    sctask.addQuery('sys_created_on', '<=', gs.daysAgo(this.waitingCustomerDuration));
    sctask.query();

    while (sctask.next()) {
      // Check if the sctask has been assigned to someone
      if (sctask.assigned_to.nil()) {
        // Do nothing if the sctask has not been assigned to anyone
        continue;
      }

      // Close the sctask
      sctask.setValue('state', 4); // 4 is the value for "Closed Complete"
      sctask.update();
    }
  },

  type: 'AutoTicketClosure'
};

 

  

If my response was helpful in resolving the issue, please consider accepting it as a solution by clicking on the Accept solution button and giving it a thumbs up 👍. This will benefit others who may have a similar question in the future.

 

Thank you!

Ratnakar

 

View solution in original post

Hello Thomas,

 

I see you have a javascript Class definition in your code for a Scheduled Job.  I guess that is a Script Include? Anyway, I typically use the script in the scheduled job "Run this script" field.  I'm not familiar with your usage.

 

Add some debug to your script.  One example:

 

gs.info("AutoTicketClosure: Found " + gr.getRowCount() + " records to process");

after the line: "gr.query();" and before the while loop.  Add similar debug messages inside the loop. Like:

 

gs.info("AutoTicketClosure: Closing incident:  " + gr.number + ".");

 

just before "gr.update();"

 

then you can look in the Script Log Statements module and search message, starts with, AutoTicketClosure.

View solution in original post

Hi @Thomas98 

 

In you scheduled job try:

 

var acScript = new AutoTicketClosure();
acStript.run();

You can click the "Execute now" button to test. And if you add the gs.log() statements proposed, review the Scripts Log Statements to see those messages.

View solution in original post

16 REPLIES 16

I guess that works for you. Script logic looks OK if you get expected results for both tables. You may want to update sctask.close_notes with some value before the sctask.update().

Thanks will do! Will update and accept the resolution once testing is complete. Thank you for all your help. 

Its not working:( 

 

I have an incident that was opened on 15th of May 

State was changed to "on hold"  "awaiting caller" on 15th as well and since then the state was not updated at any point. 

 

My scheduled job ran at 9:12pm  edt and it did not update the the above incident. 

 

Can you take a look at my scheduled job screenshot. 

Thomas98_0-1684718315886.png

 

 

Hello Thomas,

 

I see you have a javascript Class definition in your code for a Scheduled Job.  I guess that is a Script Include? Anyway, I typically use the script in the scheduled job "Run this script" field.  I'm not familiar with your usage.

 

Add some debug to your script.  One example:

 

gs.info("AutoTicketClosure: Found " + gr.getRowCount() + " records to process");

after the line: "gr.query();" and before the while loop.  Add similar debug messages inside the loop. Like:

 

gs.info("AutoTicketClosure: Closing incident:  " + gr.number + ".");

 

just before "gr.update();"

 

then you can look in the Script Log Statements module and search message, starts with, AutoTicketClosure.

Hi @Thomas98 

 

In you scheduled job try:

 

var acScript = new AutoTicketClosure();
acStript.run();

You can click the "Execute now" button to test. And if you add the gs.log() statements proposed, review the Scripts Log Statements to see those messages.