Is it possible to back-date a ticket when closing it? If so , how?

mitali1
Giga Contributor

Facilities would like to change the close date and back date it when closing. Is it a functionality currently available, if so, how?

2 REPLIES 2

awessel
Kilo Guru

I believe this could be done by adding a business rule that updates the closed field to the date desired. The important part would be that this BR runs after the OOB BR that sets the closed field.



With that said, I would push back against this requirement. I can't think of a good reason why we'd want to do this. What's the use case that's driving this requirement?


Aditya Telideva
ServiceNow Employee
ServiceNow Employee

Hi Mitali,



I found this question on the community where they were looking for a solution to close the ticket after 3 business days. As we all know, for example the "autoclose" for incident doesn't count business days, it just goes for "days". Which pretty much leaves people setting a high value enough so it would take care most of the short holidays and the customers won't be angry. This solution can be used on all kind of tickets.



EDIT: I also put this as an "idea" on the community, so go in and vote for it and see if we can get it into a future release: Be able to use Business hours/days to autoclose ticket



I will use the following stuff to get it to work:



  • Relative Durations. I use this to set the amount of business days I want it to wait until it closes. Using relative duration
  • DurationCalculator. To calculate the duration with a schedule. Using DurationCalculator to calculate a due date
  • Schedule Job. Then I'll put the code in a schedule job that runs how often you want it to run to check and close those tickets that are "due".
First step is to create a relative duration that fits the amount of days you want. You can easy find it under System Scheduler->Schedules->Relative Durations.As you can see there are a few examples that you can either use or look to understand how it's build up.find_real_file.pngBut lets create a new one that we want to be "3 business days". It doesn't need to be more than this:find_real_file.pngCopy the sys_id of this record, since you will be needing it later for the coding of the schedule job. Next step will just to get the sys_id of the schedule you want to use. Just head to the schedule record and copy that sys_id as well. Now we got all the nice things we need. So lets head to System Definition->Scheduled Job and press New. Choose "Automatically run a script of your choosing" and we get to the new record.Here you can set how often you want it to run, but perhaps once a day would be nice and let's pick a time when we don't think so many people are working.Remember to put all your code in an anonymous function so it variable etc. doesn't get messed up with other things that is running. If you don't do this, strange things can happen if you for example have two jobs running at the same time and both having a variable called for example gr. Summary of the code below.
  • Get the sys_id for the relative duration I want to use
  • Query all the records I want to go through. Active is true and got a resolve date is the ones I want here.
  • Loop through the records I get and check if its been more than 3 business days since it was set to resolve. If it is, close it.


  1. (function(){  
  2.   //Get the relative duration for 3 business days  
  3.   var relDur = 'f3ae5fc70f0132004cf365ba32050eb9';  
  4.   //get the incidents that we want to go through  
  5.   var encQue = 'active=true^resolved_atISNOTEMPTY';  
  6.  
  7.   var gr = new GlideRecord('incident');  
  8.   gr.addEncodedQuery(encQue);  
  9.   gr.query();  
  10.  
  11.   while(gr.next()){  
  12.   //Calculate and see if resolve date is more than 3 business days ago. And if, close the ticket.  
  13.   var dc = new DurationCalculator();  
  14.  
  15.   //Load the schedule into our calculation through the function below  
  16.   addSchedule(dc);  
  17.  
  18.   //Do the calculation and see if end date is before today  
  19.   dc.setStartDateTime(gr.resolved_at);  
  20.   if (!dc.calcRelativeDuration(relDur)) {  
  21.   gs.error("*** calcRelativeDuration failed for record {0}", gr.number);  
  22.   }  
  23.   if (dc.getEndDateTime() < gs.nowDateTime()){  
  24.   gr.setValue('state', 7);  
  25.   gr.update();  
  26.   }  
  27.   }  
  28.  
  29.   function addSchedule(durationCalculator) {  
  30.   //   Load the "8-5 weekdays" schedule into our duration calculator.  
  31.   var scheduleName = "8-5 weekdays";  
  32.   var grSched = new GlideRecord('cmn_schedule');  
  33.   grSched.addQuery('name', scheduleName);  
  34.   grSched.query();  
  35.   if (!grSched.next()) {  
  36.   gs.error("*** Could not find schedule {0}.", scheduleName);  
  37.   return;  
  38.   }  
  39.   return durationCalculator.setSchedule(grSched.getUniqueValue(), "GMT");  
  40.   }  
  41.  
  42. })();  




Aditya Telidevara