Autoclose HR Cases Using A Business Rule

bill72
Tera Contributor

Hello ServiceNow community,

We would like to automatically set a HR case to a State of "Closed Complete" if the HR case has been sitting in the "Awaiting Acceptance" State for more then 2 business days without a response from the customer. Would the Advanced script in the Business Rule below work? I have customized the out of the box Business Rule for autoclosing Incidents to apply to HR Cases instead.

Table: HR General Case (sn_hr_core_case)

// This script automatically closes HR Cases that have been sitting in Awaiting Acceptance State
// and haven't been updated by the Customer in 2 business days.
// This number is a property in System Properties.
// To place a comment in the HR Case, uncomment the "gr.comments" line.

autoCloseHRCases();

function autoCloseHRCases() {
var ps = gs.getProperty('glide.ui.autoclose.time');
var pn = parseInt(ps);
var queryTime = new GlideDateTime();
queryTime.addDaysUTC(-pn);

if (pn > 2) {
var gr = new GlideRecord('sn_hr_core_case');
gr.addQuery('sn_hr_core_case_state', HR_Case_State.Awaiting Acceptance);
gr.addQuery('sys_updated_on', '<', queryTime);
gr.query();
while(gr.next()) {
gr.sn_hr_core_case_state = HR_Case_State.Closed Complete;
// gr.comments = 'HR Cases automatically set to Closed Complete state after ' + pn + ' days in the Awaiting Acceptance state.';
gr.active = false;
gr.closed_by = gr.resolved_by;
gr.update();
}
}
}

Does anyone have a working Business Rule to auto close HR Cases they can share? Thanks in advance for any assistance.

 

 

 

 

 

4 REPLIES 4

Jaspal Singh
Mega Patron
Mega Patron

Hi Bill,

 

We achieved the same by creating a glide.ui.autoclosehrcase.time property in sys_properties table & then creating a scheduled job with below snippet

 

find_real_file.png

autoClosehr_case();
//HR Case
function autoClosehr_case() {

var ps = gs.getProperty('glide.ui.autoclosehrcase.time');
var pn = parseInt(ps);
if (pn > 0) {

var gr = new GlideRecord('hr_case');
gr.addQuery('state', '20');
gr.addQuery('sys_updated_on', '<', gs.daysAgoStart(pn));
gr.query();
while (gr.next()) {
gr.state = '3';
gr.comments = 'HR Case automatically closed after ' + pn + ' days in the Awaiting User Info state.';
gr.active = false;
gr.update();
}
}
}

 

Thanks,

Jaspal Singh

 

Hit Helpful or Correct on the impact of response.

Sai Anna
ServiceNow Employee
ServiceNow Employee

Hi Bill,

 

Business Rule is not a good way to achieve this. As Business Rules only trigger when insert/update/delete operation occurs. you can place above script in Scheduled Job and run it everyday night. so every night this script executes timely and checks whether there are any cases in Awaiting Acceptance state more than the desired number of days and close them. Please let me know if you need help with the script. Looks like you are using Scoped HR. make sure scheduled job is also in the same scope.

 

 

 

Thanks,

Sai

Hit like, Helpful or Correct depending on the impact of the response

 

 

 

 

Hi Sai, 

 

I also have similar requirement as above , but i need to close the case for both the conditions i.e for  on-hold and awaiting acceptance with respective of  5 business days ,not including with Saturday and Sunday. currently we have a schedule for 5 days but its taking sat and Sunday. if possible could you please help me with the script.

Thanks in advance.

Rob Sestito
Mega Sage

You should be able to achieve this using the OOTB Workflow.

There is a workflow called: HRI Case User Acceptance - this has the workflow you would want to use and can modify. I changed the wait time to 3 business days, but 2 would be fine as well. I don't see why you would need to wrap this requirement into a BR or anything like that. Use what the system has to the best of it's ability before creating custom things.

 

find_real_file.png

From the Timer step, you can adjust the wait time: (but you need to 'check out' the workflow to edit)

find_real_file.png

 

I have an entire process built for the end user (requestor) to accept or reject the resolution to their case.

Let me know if you would like me to share the whole process with you.

Cheers,

-Rob