HR Case is not getting closed automatically
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2023 03:02 AM
HR LE Case is not getting automatically closed even all the respective HR tasks are complete.
Have configured the same in HR service too, still its not working(screenshot below). Can someone pls provide any script/how to do configuration here?
@Ankur Bawiskar @Sandeep Rajput @Community Alums
- Labels:
-
Human Resources Service Delivery

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2023 03:55 AM
@Ram33843094 Auto closure of cases are usually taken care of a scheduled job 'Auto close case'. This job runs daily and checks for the cases which are in Ready (state=10) or Work in progress (state=18) cases. Please check if your case (which is a candidate for auto closure) have either of the states.
Here is the source code of this scheduled job.
//deals with cases with fulfillment type service activity and lifecycle event only
//get all cases with auto close case config set to true
var TABLE_HR_CASE = 'sn_hr_core_case';
var TABLE_TASK = 'task';
var TABLE_HR_TASK = 'sn_hr_core_task';
var TABLE_ACTIVITY_SET_CONTEXT = 'sn_hr_le_activity_set_context';
var TABLE_WORKFLOW_CONTEXT = 'wf_context';
var CLOSE_ALL = 'all';
var CLOSE_MANDATORY = 'mandatory';
var grCase = new GlideRecord(TABLE_HR_CASE);
var pCount = 0; //number of cases processed
var cCount = 0; //number of cases closed
//cache that holds processed cases
var processed = {};
grCase.addEncodedQuery('hr_service.auto_close_case=true^active=true^state=10^ORstate=18');
grCase.orderByDesc('number');
grCase.query();
while (grCase.next())
processCase(grCase, false);
gs.info('Auto close case: Total number of cases processed - '+pCount);
gs.info('Auto close case: Total number of cases closed - '+cCount);
/*
* Process child cases before parent.
* Following postorder traversal of a tree
*/
function processCase(grCase, isChild) {
var caseId = grCase.sys_id;
if (processed[caseId]) {
delete processed[caseId];
return;
}
pCount++;
var closeWhen = grCase.hr_service.close_when;
var closeOffset = grCase.hr_service.auto_close_offset;
//process child cases first
var grChild = new GlideRecord(TABLE_HR_CASE);
grChild.addEncodedQuery('parent='+caseId+'^hr_service.auto_close_case=true^state=10^ORstate=18');
grChild.orderByDesc('number');
grChild.query();
while(grChild.next())
processCase(grChild,true);
if (isChild)
processed[caseId] = true;
//check if all the activity set contexts are completed in case of LE
if (grCase.hr_service.le_type) {
if (new sn_hr_core.hr_CaseUtils()._getActivitySetContextCases(caseId))
return;
}
//check for child tasks
var count = 0;
// latestDateTime stores the updated time of the task which is completed last, or the date when the case was created
var latestDateTime = new GlideDateTime(grCase.sys_created_on);
var grTask = new GlideRecord(TABLE_TASK);
grTask.addQuery('parent',caseId);
grTask.query();
while(grTask.next()) {
count++;
if (grTask.active) {
if (closeWhen == CLOSE_ALL || !this.isOptional(grTask))
return;
continue;
} else {
//skip updating latestDateTime if task is optional
if (closeWhen == CLOSE_MANDATORY && this.isOptional(grTask))
continue;
if (!latestDateTime)
latestDateTime = new GlideDateTime(grTask.closed_at);
else {
var taskClosureDateTime = new GlideDateTime(grTask.closed_at);
if(latestDateTime.compareTo(taskClosureDateTime) == -1)
latestDateTime = taskClosureDateTime;
}
}
}
//do not update the case if doesn't have any child tasks at all
if (count == 0)
return;
if (!latestDateTime)
this.closeCase(grCase);
else {
var today = new GlideDateTime();
var diffInDays = GlideDateTime.subtract(latestDateTime,today).getDayPart();
if (diffInDays >= closeOffset)
this.closeCase(grCase);
}
}
/*
* close a case given its number
*/
function closeCase(grCase) {
cCount++;
grCase.setValue('state',3);
var updateCloseNotes = new sn_hr_core.updateCloseNotes();
updateCloseNotes.update(grCase);
}
/*
*check if an HR Task is optional
*/
function isOptional(grTask) {
if (grTask.sys_class_name!=TABLE_HR_TASK)
return false;
var grHRTask = new GlideRecord(TABLE_HR_TASK);
grHRTask.get(grTask.sys_id);
if (grHRTask.optional)
return true;
else
return false;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2023 04:03 AM
Yes, All the HR cases`will be in Ready state.
But do you mean to say that HR cases will be closed only when scheduled job runs at a particular time in a day?
OR
HR Case can also be closed right away when all its respective tasks are complete? ..If not can we achieve this?(as this is my requirement)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2023 05:31 AM
@Ram33843094 The cases will be closed by the scheduled job only. For closing them right away, you need to create a business rule on HR Task which will check if the all the HR Tasks related to a parent case are closed or not. You can close the parent case on the basis of it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-08-2023 05:34 AM
Thank you!! Do you have any script with respect to that, where HR Case should get closed if all the respective HR tasks and req items are complete.