Sent one email for multiple assessment

Mukul Sharma
Tera Contributor

Hi,

 

I need to send weekly notification to assessor with the link whatever the assessment is pending for them.

 

Table -> asmt_assessment_instance (Assessment Instances)

 

I am not getting the point how can I send only one email to suer when multiple assessment is assigned to assignee.

 

For Ex. User A has 10 assessment in pending then only one email should trigger to assessor and link of all 10 assessment need to share with user.

 

Thanks,

Mukul

1 ACCEPTED SOLUTION

Vishal Birajdar
Giga Sage

Hi @Mukul Sharma 

 

Please follow the below Steps : 

 

Step 1 : Create event registry (sysevent_register) table to trigger the notification 

It will be used in Scheduled job

 

VishalBirajdar_0-1698411900257.png

 

Step 2 : Create a Scheduled job to send a notification

 

/*1. Declare array to store assigned to values */
var assignedArray = [];
var unique = [];

/*2. Glide record on assesment table*/
var grAssessor = new GlideRecord('asmt_assessment_instance');
grAssessor.addQuery('state', 'pending'); //use backend value of pending
grAssessor.query();
while (grAssessor.next()) {
    /*3. Add the assigned to values in array */
    assignedArray.push(grAssessor.getValue('user'));
}

/* 
  4. Now we have array of assigned to but it may contain duplicate value
 so we need to remove duplicate from array
*/

for (i = 0; i < assignedArray.length; i++) {
    if (unique.indexOf(assignedArray[i]) === -1) {
        unique.push(assignedArray[i]);
    }
}


/*5. Loop through Unique array to send notification to user */
for (i = 0; i < unique.length; i++) {
   //gs.eventQueue('<event_name_created_in_step1>',object,'parm1','parm2'); 
   gs.eventQueue('asmt.assesment.instance',grAssessor,unique[i]);     //will send user in parm1
}

 

VishalBirajdar_1-1698412151858.png

 

Step 4 : Create email script to show the link (list of records assigned to particular user)

Name : link_to_assesment

function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {

        /* Get assigned to from event's parm1 */
		var assigned_to = event.parm1;
		
		template.print('<a href=' +  gs.getProperty('glide.servlet.uri') + 'asmt_assessment_instance_list.do?sysparm_query=state=pending^user=' + assigned_to + '>' + 'Link' + '</a>');

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

 

VishalBirajdar_2-1698412386577.png

 

Step 5 : Create Notification on "asmt_assessment_instance" table , with following configuration

 

When to send 

Send When : Event is fired

Event name :  Event created  in step 1 

 

VishalBirajdar_3-1698412644276.png

 

What it will contain :

Call mail script created in Step 4 

 

${mail_script:link_to_assesment}

 

VishalBirajdar_4-1698412739089.png

 

 

Hope this helps...!!!

 

 

 

 

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

View solution in original post

3 REPLIES 3

Vishal Birajdar
Giga Sage

Hi @Mukul Sharma 

 

Please follow the below Steps : 

 

Step 1 : Create event registry (sysevent_register) table to trigger the notification 

It will be used in Scheduled job

 

VishalBirajdar_0-1698411900257.png

 

Step 2 : Create a Scheduled job to send a notification

 

/*1. Declare array to store assigned to values */
var assignedArray = [];
var unique = [];

/*2. Glide record on assesment table*/
var grAssessor = new GlideRecord('asmt_assessment_instance');
grAssessor.addQuery('state', 'pending'); //use backend value of pending
grAssessor.query();
while (grAssessor.next()) {
    /*3. Add the assigned to values in array */
    assignedArray.push(grAssessor.getValue('user'));
}

/* 
  4. Now we have array of assigned to but it may contain duplicate value
 so we need to remove duplicate from array
*/

for (i = 0; i < assignedArray.length; i++) {
    if (unique.indexOf(assignedArray[i]) === -1) {
        unique.push(assignedArray[i]);
    }
}


/*5. Loop through Unique array to send notification to user */
for (i = 0; i < unique.length; i++) {
   //gs.eventQueue('<event_name_created_in_step1>',object,'parm1','parm2'); 
   gs.eventQueue('asmt.assesment.instance',grAssessor,unique[i]);     //will send user in parm1
}

 

VishalBirajdar_1-1698412151858.png

 

Step 4 : Create email script to show the link (list of records assigned to particular user)

Name : link_to_assesment

function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {

        /* Get assigned to from event's parm1 */
		var assigned_to = event.parm1;
		
		template.print('<a href=' +  gs.getProperty('glide.servlet.uri') + 'asmt_assessment_instance_list.do?sysparm_query=state=pending^user=' + assigned_to + '>' + 'Link' + '</a>');

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

 

VishalBirajdar_2-1698412386577.png

 

Step 5 : Create Notification on "asmt_assessment_instance" table , with following configuration

 

When to send 

Send When : Event is fired

Event name :  Event created  in step 1 

 

VishalBirajdar_3-1698412644276.png

 

What it will contain :

Call mail script created in Step 4 

 

${mail_script:link_to_assesment}

 

VishalBirajdar_4-1698412739089.png

 

 

Hope this helps...!!!

 

 

 

 

 

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Hi @Vishal Birajdar 

 

Provided solution works for me.

 

Thanks a lot for your quick and descriptive solution.

 

Thanks,

Mukul

 

 

Hello Vishal

Thank You, it works for me.!