Need to trigger a notification when all the tasks associated with case closed complete?

Ramu6
Tera Contributor

Hi All,

I have a requirement that , basically when HR case is created , they manually create HR tasks, i want to trigger a notification that it checks all the tasks associated with case as closed complete then the notification will trigger to health group? and that includes body as 

Hi <HR service name>

Please note that the tasks you have requested on <date of tasks submitted> for <Employee Name> have now been marked as complete.

 

Please help to resolve this

 

Thanks

Ramu

2 ACCEPTED SOLUTIONS

@Ramu6 

then do this

1) have notification on HR Task, with your subject, body etc. I hope you know how to do this

Condition as State Changes to Close

2) Use advanced condition in notification and check if this task is the last one; if yes then set answer=true

var gr = new GlideRecord("sn_hr_core_task");
gr.addActiveQuery();
gr.addQuery("parent", current.parent);
gr.setLimit(1);
gr.query();
answer = !gr.hasNext();

AnkurBawiskar_0-1697629607150.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

@Ramu6 

something like this. please enhance

(function runMailScript(current, template, email, email_action, event) {

	// Add your code here

	var gr = new GlideRecord("sn_hr_core_task");
	gr.addQuery("parent", current.parent);
	gr.query();
	while (gr.next()) {
		template.print("Please note that the task you have requested on " + new GlideDateTime(gr.sys_created_on).getDate() + "for " + gr.parent.subject_person.name + " have now been marked as complete.");
	}


})(current, template, email, email_action, event);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

12 REPLIES 12

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Ramu6 ,

 

You can create a Business Rule or a Workflow that triggers when an HR case is created or when HR tasks are marked as complete. Here's a step-by-step guide for creating a Business Rule to handle this scenario:

 

### Step 1: Create a Business Rule

 

1. Go to **System Definition > Business Rules** in your ServiceNow instance.

 

2. Click on **New** to create a new Business Rule.

 

3. Provide a name for the Business Rule, such as "Notify Health Group for Completed HR Tasks."

 

4. Set the **Table** to the HR Task table (usually `sn_hr_core_task`).

 

5. Define the condition when this Business Rule should run. For example:

   

   active == true && state == '3' && hr_case_linked == true

   

   This condition checks if the task is active, completed (state 3), and linked to an HR case.

 

6. In the **Advanced** tab, add the following script to send the notification:

  

   (function executeRule(current, previous /*null when async*/) {

       // Check if all tasks related to the HR case are closed

       var hrCase = new GlideRecord('sn_hr_core_case');

       if (hrCase.get(current.parent)) {

           var tasks = new GlideRecord('sn_hr_core_task');

           tasks.addQuery('parent', hrCase.sys_id);

           tasks.addQuery('state', '!=', '3'); // Check for tasks not in the closed state

           tasks.query();

           if (!tasks.hasNext()) {

               // All tasks are closed, send notification to Health Group

               var notification = new GlideRecord('sysevent_email_action');

               notification.addQuery('name', 'Health Group HR Task Completed');

               notification.query();

               if (notification.next()) {

                   gs.eventQueue('incident.event', hrCase, notification.sys_id);

               }

           }

       }

   })(current, previous);

   

 

7. Save the Business Rule.

 

### Step 2: Create the Notification

 

1. Go to **Self-Service > Notifications**.

 

2. Click on **New** to create a new Notification.

 

3. Set the **Notification Name** to "Health Group HR Task Completed."

 

4. In the **Message** field, add the notification body template you provided:

   ```

   Hi <HR service name>,

 

   Please note that the tasks you have requested on ${date_of_tasks_submitted} for ${employee_name} have now been marked as complete.

   ```

 

5. Save the Notification.

 

With these steps, when HR tasks associated with a case are marked as complete, and all tasks related to the HR case are closed, the notification will be triggered to the Health Group with the specified body template. Make sure to customize the script and notification template placeholders according to your field names and requirements.

 

Thanks,

Danish

 

@Danish Bhairag2 thanks for the code

i have tried this but not working 

Ankur Bawiskar
Tera Patron
Tera Patron

@Ramu6 

So basically you want 1 email and that should be sent when the last HR task is closed?

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Ankur Bawiskar 

thanks for the response

Yes, i want only one notification to be  trigger when the all tasks of the HR case marked as complete