Trigger a alert mail to Assignment Group

Pavan Kumar28
Tera Contributor

Hi  All,

I have to generate alert mails to members of Assignment group if Ctask has “assigned to” field is empty in 2 days after Change request is approved. Can anyone please let me know the possible ways.

Best Regards,
Pavan. 

1 ACCEPTED SOLUTION

@Pavan Kumar28

I believe I answered based on your original question you posted.

You can always enhance the script I shared based on your requirement.

I hope I have provided a thorough answer to your question. I'm confident that with your developer skills, you can take it further from here.

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

9 REPLIES 9

Community Alums
Not applicable

Hi @Pavan Kumar28 ,

try this codeThe script checks the parent change_request for approval and identifies unassigned change_task records created more than two days ago. It sends an email notification to the members of the Assignment group..

 

(function executeNotification() {
    // Get the date and time from two days ago
    var twoDaysAgo = new GlideDateTime();
    twoDaysAgo.addDaysUTC(-2);

    // Query Change Tasks where "Assigned to" is empty and the Change Request is approved
    var taskGr = new GlideRecord('change_task');
    taskGr.addEncodedQuery('assigned_toISEMPTY^sys_created_on<=' + twoDaysAgo.getValue());
    taskGr.addJoinQuery('change_request', 'change_request', 'sys_id')
          .addCondition('state', '3'); // Ensure the parent Change Request is in "Approved" state

    taskGr.query();

    while (taskGr.next()) {
        var assignmentGroup = taskGr.assignment_group;

        if (assignmentGroup) {
            // Retrieve all members of the Assignment Group
            var groupMembers = new GlideRecord('sys_user_grmember');
            groupMembers.addQuery('group', assignmentGroup);
            groupMembers.query();

            while (groupMembers.next()) {
                // Send an email notification to each member
                gs.eventQueue('custom.change_task.unassigned_alert', taskGr, groupMembers.user, '');
            }
        }
    }
})();

 

@Pavan Kumar28 

Hope you are doing good.

Did my reply answer your question?

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

yuvarajkate
Giga Guru

First you need to create a Notification for this.

 

  • Navigate to System Notification > Email > Notifications.
  • Create a new notification tied to the custom event 'custom.ctask.notification'.
  • Set the email subject and body with the details of the Change Task and instructions for assignment

 

 

Secondly you will need a Schedule job for this. Go to System Definitions>Scheduled Jobs and use this Script.

Script for Schedule Job:

 

var gr = new GlideRecord('change_task'); 
gr.addQuery('assigned_to', '');                                                   // Check if 'Assigned To' is empty
gr.addQuery('state', 'approved');                                              // Ensure the Change Request is approved
gr.addQuery('sys_created_on', '<=', gs.daysAgo(2));              // Check if 2 days have passed
gr.query();

while (gr.next()) {
    var assignmentGroup = gr.assignment_group;
    if (assignmentGroup) {
        var groupMembers = new GlideRecord('sys_user_grmember');
        groupMembers.addQuery('group', assignmentGroup);
        groupMembers.query();

        while (groupMembers.next()) {
            gs.eventQueue('custom.ctask.notification', gr, groupMembers.user.email, '');
        }
    }
}

 

 

Please Mark this as Correct/Helpful if it helped you in any way.

Runjay Patel
Giga Sage

Hi @Pavan Kumar28 ,

 

Try below code in your schedule job.

var grTask = new GlideRecord('change_task'); // Table for Change Tasks
grTask.addQuery('state', '!=', '3'); // Ensure it's not closed (3 is an example state for Closed)
grTask.addNullQuery('assigned_to'); // Check if Assigned to is empty
grTask.addQuery('parent.state', '3'); // Change Request is in Approved state (replace '3' with actual Approved state value)
grTask.addQuery('sys_created_on', '<=', gs.daysAgoStart(2)); // Created at least 2 days ago
grTask.query();

while (grTask.next()) {
   
        gs.eventQueue('change_task.alert_email',  grTask,  '', '');
    }
}

 

Create event based notification and configure email body and recipients (Add change group).

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

vishwajeet5550
Mega Guru

Create a new Business Rule:

Name: "Track Change Request Approval"

Table: change_request

When: After

Condition: state is approved

Script: The Business Rule should create a record in a custom table or a field that tracks the time when the Change Request is approved, which can be later used to trigger the alert for the Ctask.

 

(function executeRule(current, previous /*null when async*/) {
    current.approved_timestamp = new GlideDateTime(); 
    current.update();
})(current, previous);

 

Ensure that you have a field (approved_timeStamp) in the

change_request table to store the approval time.