Email Notification Trigger issue

DharmaT
Tera Contributor

I want to trigger an email notification weekly to assigned person if there is any pending task  in the Bin which comes from the table  "sn_grc_task ", created the scheduled job while executing able to see the logs thats triggering the job and event but the email is not triggering not able to see in the email logs, created the notification and event in the sys_user table. please help me to fix ...

1 ACCEPTED SOLUTION

@DharmaT 

why did you create event in global scope when the sn_grc_task table is in other scope?

Changes below

1) Event should be on sn_grc_task table and in "GRC: Profiles" scope

2) Notification should be on sn_grc_task and and scope "GRC: Profiles"

a) Event Parm1 contains Recipient - True

3) scheduled job script

(function executeRule() {
    var gr = new GlideRecord('sn_grc_task');
    gr.addEncodedQuery('stateNOT IN3,4,7^assigned_toISNOTEMPTY');
    gr.query();

    var userTasks = {};

    while (gr.next()) {
        var userId = gr.assigned_to.sys_id.toString();
        if (!userTasks[userId]) {
            userTasks[userId] = [];
        }
        userTasks[userId].push(gr.getDisplayValue('number') + ' - ' + gr.short_description);
    }

    for (var user in userTasks) {
        var taskList = userTasks[user].join('\n');
        var taskGR = new GlideRecord('sn_grc_task'); // Use task record as base
        taskGR.addQuery('assigned_to', user);
        taskGR.query();
        if (taskGR.next()) {
            gs.eventQueue(
                'IRM_Pending_tasks_reminder', 
                taskGR, // Pass TASK record (not user)
                taskGR.assigned_to.toString(), // Proper recipient reference
                taskList // Task data parameter
            );
            gs.info("Queued reminder for: " + taskGR.assigned_to.name);
        }
    }
})();

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

7 REPLIES 7

DharmaT
Tera Contributor
(function executeRule() {
   
    var gr = new GlideRecord('sn_grc_task');
    gr.addEncodedQuery('stateNOT IN3,4,7^assigned_toISNOTEMPTY');
    gr.query();
 
    var userTasks = {};
 
    while (gr.next()) {
        var userId = gr.assigned_to.sys_id.toString();
        if (!userTasks[userId]) {
            userTasks[userId] = [];
        }
        userTasks[userId].push(gr.number + ' - ' + gr.short_description);
    }
 
    for (var user in userTasks) {
        var taskList = userTasks[user].join('\n');
        var userGR = new GlideRecord('sys_user');
        if (userGR.get(user)) {
            gs.eventQueue('IRM_Pending_tasks_reminder', userGR, taskList, 'd9e4196833061a103cd40939bd5c7b84');
                        gs.info("Sending reminder to user: " +userGR.name);

        }
    }
})();
 
 
 
@Chaitanya ILCR  @Robert H @Abhijit4  Hi all , this is the script, yes, I have created the event in the Global scope only, please check the script is it correct or not  and let me know do I have to change anything, and I want to send to the assigned person the notification . let me know what correctly, I have to select in the  who willl recieve in the notification ..

Hi @DharmaT 

 

You had bit of inefficiencies in your code so I have modified your whole logic to achieve your required output.

 

Here a working updated script that I created for Incident table for testing purpose:

var gr = new GlideAggregate('incident');
    gr.addEncodedQuery('stateNOT IN3,4,7^assigned_toISNOTEMPTY');
	gr.groupBy("assigned_to");
    gr.query();
    while (gr.next()) {
		var jsonTicketList=[];
		var i=0;
        var userId = gr.assigned_to.sys_id.toString();
        var grUserIncidents = new GlideRecord('incident');
    grUserIncidents.addEncodedQuery('stateNOT IN3,4,7');
	grUserIncidents.addQuery("assigned_to",userId);
    grUserIncidents.query();
	while(grUserIncidents.next()){
		jsonTicketList[i++]=grUserIncidents.getValue("number")+"- "+grUserIncidents.getValue("short_description");
	}
	gs.info("User Id: "+gr.assigned_to.getDisplayValue()+"\nTask Details:\n"+jsonTicketList);
	//gs.eventQueue('IRM_Pending_tasks_reminder', userGR,gr.assigned_to, jsonTicketList);
                        // gs.info("Sending reminder to user: " +userGR.name);
    }

 Output:

Abhijit4_0-1746174417996.png

Here is updated script for you to use:

 

var grGRCTask = new GlideAggregate('sn_grc_task');
    grGRCTask.addEncodedQuery('stateNOT IN3,4,7^assigned_toISNOTEMPTY');
	grGRCTask.groupBy("assigned_to");
    grGRCTask.query();
    while (gr.next()) {
		var jsonTicketList=[];
		var i=0;
        var userId = grGRCTask.assigned_to.sys_id.toString();
        var grUserIncidents = new GlideRecord('sn_grc_task');
    grUserIncidents.addEncodedQuery('stateNOT IN3,4,7');
	grUserIncidents.addQuery("assigned_to",userId);
    grUserIncidents.query();
	while(grUserIncidents.next()){
		jsonTicketList[i++]=grUserIncidents.getValue("number")+"- "+grUserIncidents.getValue("short_description");
	}
	var userGR=grGRCTask.assigned_to.getRefRecord();
	gs.info("User Id: "+grGRCTask.assigned_to.getDisplayValue()+"\nTask Details:\n"+jsonTicketList);
	gs.eventQueue('IRM_Pending_tasks_reminder', userGR,grGRCTask.assigned_to.toString(), jsonTicketList);
                        // gs.info("Sending reminder to user: " +userGR.name);
    }

Who will receive should be parm1 user and you can use parm2 in email body to show ticket details for users.

 

If my response helped please mark it as correct.

 

Regards,

Abhijit 

ServiceNow MVP

ServiceNow Buddy Owner: snbuddy.com

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

@DharmaT 

why did you create event in global scope when the sn_grc_task table is in other scope?

Changes below

1) Event should be on sn_grc_task table and in "GRC: Profiles" scope

2) Notification should be on sn_grc_task and and scope "GRC: Profiles"

a) Event Parm1 contains Recipient - True

3) scheduled job script

(function executeRule() {
    var gr = new GlideRecord('sn_grc_task');
    gr.addEncodedQuery('stateNOT IN3,4,7^assigned_toISNOTEMPTY');
    gr.query();

    var userTasks = {};

    while (gr.next()) {
        var userId = gr.assigned_to.sys_id.toString();
        if (!userTasks[userId]) {
            userTasks[userId] = [];
        }
        userTasks[userId].push(gr.getDisplayValue('number') + ' - ' + gr.short_description);
    }

    for (var user in userTasks) {
        var taskList = userTasks[user].join('\n');
        var taskGR = new GlideRecord('sn_grc_task'); // Use task record as base
        taskGR.addQuery('assigned_to', user);
        taskGR.query();
        if (taskGR.next()) {
            gs.eventQueue(
                'IRM_Pending_tasks_reminder', 
                taskGR, // Pass TASK record (not user)
                taskGR.assigned_to.toString(), // Proper recipient reference
                taskList // Task data parameter
            );
            gs.info("Queued reminder for: " + taskGR.assigned_to.name);
        }
    }
})();

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