Auto closure of a case record after 7 business days from the date of resolved using scheduled jobs

nikhitha24
Tera Guru

Hi All,

 

I'm trying to auto close my case records after 7 business days from the date of resolved using schedule jobs.

 

Can you help me how i can achieve this.

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@nikhitha24 

for handling business days you will have to consider using schedule in your calculation

remember there might be already 1 OOB auto case closure functionality

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar,

 

I have tried below script , but some how it is not working as expected in the schedule job

one more thing which i observed was when i run the same script in the background it's was working

Can you please check the script and let me know where exactly i was missing something.

Thanks in Advance.

 

function subDaysInSchedule(scheduleName, startDate, daysBack) {
var limitDaysBack = 100
//Get a schedule by name to calculate the check
var schedRec = new GlideRecord("cmn_schedule");
schedRec.get("name", scheduleName);
//Get date/time in correct format for duration calculation
var endDate = new GlideDateTime();
var startDateBeg = new GlideDateTime();
startDateBeg.setValue(startDate);
startDateBeg = startDateBeg.getLocalDate();
endDate.setValue(startDateBeg);

usrTZ = startDateBeg.getUserTimeZone();

var dc = new DurationCalculator();
dc.setTimeZone(usrTZ);
dc.setSchedule(schedRec.sys_id);

// Max 100 Days, just if loop does not find a slot in the schedule
var limitHoursBack = limitDaysBack * 8; // 9-Hours = 1-Business Day
for (var i = 0; i < limitHoursBack; i++) {
//Substract hour by hour
endDate.addSeconds(-3600); //1hour = 60sec * 60min

//Calculate Duration
var duration = dc.calcScheduleDuration(endDate, startDateBeg);

//If we went enough hours back, quit
if (duration / 32400 >= daysBack) { // 32400 Seconds = 9-Hours = 1-Business Day
break;
}
}

return endDate;
}
gs.info(new GlideDateTime() + '')
var daysAgo = subDaysInSchedule('8-5 weekdays', new GlideDateTime() + '', '7');
var date = daysAgo.getDate() + '';

var gr = new GlideRecord('sn_customerservice_case');

gr.addEncodedQuery("auto_close=true^u_account_type=abcd^resolved_at<=javascript&colon;gs.dateGenerate('"+ date +"','23:59:59')^state=6")

gr.query();
gs.info(gr.getEncodedQuery())
while(gr.next()){
gr.state =3;
gr.comments ="This case was auto closed.";
gr.closed_by = 'System Administrator';
gr.update();
}

AndersBGS
Tera Patron
Tera Patron

Hi @nikhitha24 ,

 

Please see the OOTB flow in flow designer that just need to be activated: https://docs.servicenow.com/bundle/vancouver-customer-service-management/page/product/customer-servi...

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.

 

Best regards

Anders

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Anders

Rising star 2024
MVP 2025
linkedIn: https://www.linkedin.com/in/andersskovbjerg/

ChiranjeeviR
Kilo Sage
 
Auto closure of a case record after 7 business days 
Follow below steps 
Turning auto-closure off
a. Turning auto-closure off
b. Navigate to System Properties > System.
c. Locate the property Number of days (integer) after which Resolved incidents are automatically closed.
d. Set the Value to 0.
 
Changing the number of days to wait before closing the incident
a. Navigate to System Properties > System.
b. Locate the property Number of days (integer) after which Resolved incidents are automatically closed.
c. Set the Value to be the number of days to wait before closing a resolved incident.
For example, if you want to wait a week before closing the incident, set the value to 7 days.
 
Changing the frequency at which the system checks on the resolved incidents to close them
a. Navigate to System Scheduler > Scheduled Jobs > Today's Scheduled Jobs.
Locate the record named Autoclose Incidents.
b. This job runs every hour. If there are any new incidents that have gone past the 1 day in Resolved state in that last hour, the incidents are updated.
c. Update the Repeat time to change this to run at a different frequency. If there is a time of the day you would like it to run, change the Trigger Type to be Daily and set a specific time.
 
Set a specific user as the Updated by on the incident record
a. Navigate to System Scheduler > Scheduled Jobs > Today's Scheduled Jobs.
b. Locate the record named Autoclose Incidents.
c. In the Job content, add the following line above the scScriptName:
fcRunAs=itil
(The underlined text must be a valid user_id.)
This ensures all incidents closed as a result of this script are recorded as being updated by ITIL users. You can set this to be any user in your system. 
 
Create Business rule
Navigate to System Definition > Business Rules.
Locate the record named Incident autoclose.
In the Advanced section, the script field contains the code that runs on every incident closed using this feature. This is where additional changes can be made to the incident record. For example, add a comment in the comments section of the incident documenting the auto-closure. 
 
 
autoCloseIncidents();
 
function autoCloseIncidents() {
var ps = gs.getProperty('glide.ui.autoclose.time');
var pn = parseInt(ps);
var queryTime = new GlideDateTime();
queryTime.addDaysUTC(-pn);
 
if (pn > 0) {
var gr = new GlideRecord('incident');
gr.addQuery('incident_state', IncidentState.RESOLVED);
gr.addQuery('sys_updated_on','<', queryTime);
gr.query();
while(gr.next())
{
gr.incident_state= '7';
gr.active=false;
gr.closed_by=gr.resolved_by;
gr.update();
 
}
}
}

ChiranjeeviR_0-1700134718552.png

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.👍🇮🇳

Thanks & Regards,
Chiranjeevi
ServiceNow Developer | | ITSM | | ServiceNow Discovery | | Event Management | | Service Mapping | | CMDB

Please mark as Correct Answer/Helpful, if applicable.