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

Chuck Tomasi
Tera Patron

That seems logical and should work. You'll need to create the property, of course. The only question I have that I cannot verify at the moment is whether hr_case extends task. If so, then your use of HR_Case.CLOSED should work in taskStateUtil.


Hey Chuck,



Thanks for replying to this!



Are you able to elaborate on the property that I would need to create? And to your question whether hr_case extends task. What exactly do you mean by that? Are you referring to the Task that can be added to a Case? If so, then yes we also use Tasks that branch out from our Cases.



Thanks,


-Rob


Hi Robert,



I did a little digging and found that (HR Case 2.0) the table sn_hr_core_case is extended from the Task table so it does use the TaskStateUtil stuff. Theoretically, it should recognize your constant HR_Case_State.CLOSED).



What I might recommend, instead of following the incident autoclose process (which uses a scheduled job to trigger a business rule, oddly enough), is just to write the scheduled job yourself. I created a solution about a year ago to do this without writing any script. You can do it all with the condition field and set field values (like a scriptless business rule) and trigger a notification if you like. No code - easier to write, easier to maintain.



Scriptless scheduled jobs


Hey Chuck,



Thanks for the recommendation and input. I followed your link you provided and watched your video, great stuff! With that in mind, am I supposed to go to ServiceNow Share and download the file? If so - the download keeps failing. It only downloads it as an XML file. Or was this downloan meant for older versions under Istanbul?



Thanks again - I am going to work on creating a Scriptless Scheduled Job.



chat soon,


-Rob