Auto-Close Resolved Cases

Rob Sestito
Mega Sage

Hello SN Comm,

So I have had help with this in the past, and believe I got a lot accomplished.. However, I believe I missed an important piece and overlooked what I should have really done.

The OOB Incident Autoclose looks like this:

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 = IncidentState.CLOSED;

//   gr.comments = 'Incident automatically closed after ' + pn + ' days in the Resolved state.';

gr.active = false;

gr.closed_by = gr.resolved_by;

gr.update();

}

}

}

I was informed to correct this by adjusting accordingly.. as I need this to happen to HR Cases.. we do not use Incidents..

so I adjusted like so:

autoCloseHR_Cases();

function autoCloseHR_Cases() {

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('hr_case');

gr.addQuery('hr_case_state', HR_Case_State.RESOLVED);

gr.addQuery('sys_updated_on', '<', queryTime);

gr.query();

while(gr.next()) {

gr.hr_case_state = HR_Case.CLOSED;

//   gr.comments = 'Incident automatically closed after ' + pn + ' days in the Resolved state.';

gr.active = false;

gr.closed_by = gr.resolved_by;

gr.update();

}

}

}

Does this seem correct or completely wrong?

Anyone able to help thank you!!

-Rob

1 ACCEPTED SOLUTION

HI Robert,



If you are going to employ a schedule (to skip over weekends) and wait a certain number of days after then you'll need a scripted solution. The scriptless solution was designed to work with simple use cases of "if these records are found, then set these values on them." You've stated a more complex set of query parameters.



At this time I'm not available to create that solution for you. Apologies.



If you do not currently have talent in house to develop this, I recommend engaging with professional services from ServiceNow or one of our partners.


PartnerNow | Sales, Services, Technology Partners | ServiceNow


View solution in original post

25 REPLIES 25

I was just going to say, it's in the description of the update set as well.



***IMPORTANT*** AFTER COMMITTING THIS UPDATE SET, Navigate to System Definition> Fix Scripts and run Update Scheduled Jobs


Now Chuck, I must keep asking.. And I hope that is okay!



Along with the Business Rule we went over, how I was looking to change the Incident Auto-close to HR Case Auto-Close. I also have a Scheduled Job created, which is: auto-close resolved HR cases, with the script as:



// Close incidents after 5 business days


var days = -5;




// Business days exclude Saturday and Sunday


var day = new GlideDateTime(gs.nowDateTime());


var weekday = day.getDayOfWeek();




days = days - 2;


day.addDays(days);




// Find resolved incidents


gs.log('SJ Auto-close hr cases looking for cases resolved before ' + day.getDisplayValue());


var hr_case = new GlideRecord('hr_case');


hr_case.addQuery('state', '20');


hr_case.addQuery('sys_updated_on', '<=', day);


hr_case.query();


while (hr_case.next()) {


gs.log('SJ Auto-close cases found resolved case ' + hr_case.number + '. Was updated on ' + hr_case.sys_updated_on);


hr_case.state = '3';


hr_case.update();


}



Does that change anything we just discussed? (and did)



Thanks,


-Rob


Hi Robert,



You can replace the HR Case Auto Close scheduled job with one you build using the scriptless solution you just added. Something as simple as:



Table: HR Case


Condition: State | is | (whatever) AND Updated | at or before | 2 | days ago



Action:


Set field values:


State | is | Closed



I just thought of something... you may need to run the scheduled job as someone who has application admin rights to the HR app. Normally scheduled jobs run as "system". Since HR is protected with application admin rights, this may not work. Test first, then let me know and I can guide you through the "Run as" feature.


I have admin to everything within our Instances and security admin - are you speaking to that or is there something deeper I'd have to look into?



Thanks,


-Rob


Yes, HR is a protected application with something added fairly recently called "Application Administration". Think of it like "Elevated access" (e.g. security_admin) only for an entire application. It's how we protect sensitive information like HR stuff from even people with "admin" rights. You effectively set an application specific 'admin' person to be the admin of that app - not necessarily someone with the admin role, but they DO need admin role to add on.



Application administration