Close Parent HR Case when all the child task get completed

ukti
Tera Contributor

Hi All,

 

I am working on New Hire Onboarding  and wanted to close the Parent case to close completed state , once all the child task( which can be HR Task + Request + SR request) , from all different tables get closed , then parent case should get auto closed. But Its not working I have written Business rule on HR Task table and after update BR as below. Can I get any help on this. ?

 

ukti_0-1702906057337.png

(function executeRule(current, previous /*null when async*/ ) {
    var hrtask = new GlideRecord('sn_hr_core_task');
    hrtask.addQuery('parent', current.parent);
    hrtask.addActiveQuery();
    hrtask.query();

    if (!hrtask.next()) {

        var reqUpdate = new GlideRecord('sc_request');
        reqUpdate.addQuery('parent', current.parent);
        reqUpdate.addActiveQuery();
        reqUpdate.query();

        if (!reqUpdate.next()) {

            var sirUpdate = new GlideRecord('sc_request');
            sirUpdate.addQuery('parent', current.parent);
            sirUpdate.addActiveQuery();
            sirUpdate.query();

            if (!sirUpdate.next()) {


                var hrcase = new GlideRecord('sn_hr_core_case');
                hrcase.addActiveQuery();
                hrcase.addQuery('sys_id', current.parent);
                hrcase.query();

                if (hrcase.next()) {
                    hrcase.state = '3';
                    hrcase.work_notes = "Case closed as all to-dos are completed";
                    hrcase.update();
                }
            }
        }
    }

})(current, previous);

 

1 ACCEPTED SOLUTION

ahefaz1
Mega Sage

Hi @ukti ,

 

I have modified your code to below and added some comments:

 

  var hrtask = new GlideRecord('sn_hr_core_task');
  hrtask.addQuery('parent', current.parent);
  hrtask.addActiveQuery();
  hrtask.query();
 
  if (hrtask.next()) {
      return; // because there is active hr task for the case
  }
 
  var reqUpdate = new GlideRecord('sc_request');
  reqUpdate.addQuery('parent', current.parent);
  reqUpdate.addActiveQuery();
  reqUpdate.query();
 
  if (reqUpdate.next()) {
      return; // because there is an active REQ for this hr case
  }
// Request closure and hr task closure has been checked, close the case
 
  var hrcase = new GlideRecord('sn_hr_core_case');
      hrcase.addActiveQuery();
      hrcase.addQuery('sys_id', current.parent);
      hrcase.query();
 
      if (hrcase.next()) {
          hrcase.state = '3';
          hrcase.work_notes = "Case closed as all to-dos are completed";
          hrcase.update();
      }

 

 
However, I wanna point out this is not scalable at all. HR onboarding can contain not just RITMs and SCRequests but many other types of tasks.
 
Please mark helpful if the post helped.
 
 

View solution in original post

4 REPLIES 4

Anil Lande
Kilo Patron

Hi,

Please make sure to add logs and track and debug your business rule.

Also make sure all tasks are closed and active=false (there is possibility of some tasks are closed but active is still 'true')

Also check records in respective tables by applying query to verify your query is correct to find completed records.

 

 

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Susan Britt
Mega Sage
Mega Sage

To make it more scalable, you can just read the task table (HR task, case, request, etc all extend from it and prevents you from having to add other task types if you expand to include finance request, etc) for open records where the parent = onboarding case.  However, you'll need to think through the timing of this running.  If running on update of any HR Task, then it could run and close the parent onboarding case before all Activity Sets and activities/tasks have even been created.  

ahefaz1
Mega Sage

Hi @ukti ,

 

I have modified your code to below and added some comments:

 

  var hrtask = new GlideRecord('sn_hr_core_task');
  hrtask.addQuery('parent', current.parent);
  hrtask.addActiveQuery();
  hrtask.query();
 
  if (hrtask.next()) {
      return; // because there is active hr task for the case
  }
 
  var reqUpdate = new GlideRecord('sc_request');
  reqUpdate.addQuery('parent', current.parent);
  reqUpdate.addActiveQuery();
  reqUpdate.query();
 
  if (reqUpdate.next()) {
      return; // because there is an active REQ for this hr case
  }
// Request closure and hr task closure has been checked, close the case
 
  var hrcase = new GlideRecord('sn_hr_core_case');
      hrcase.addActiveQuery();
      hrcase.addQuery('sys_id', current.parent);
      hrcase.query();
 
      if (hrcase.next()) {
          hrcase.state = '3';
          hrcase.work_notes = "Case closed as all to-dos are completed";
          hrcase.update();
      }

 

 
However, I wanna point out this is not scalable at all. HR onboarding can contain not just RITMs and SCRequests but many other types of tasks.
 
Please mark helpful if the post helped.