CC to respective notification not working as expected

SAM321
Tera Contributor

Hi experts am trying to trigger respective notification which are selected in list collector variable in catalog item  .when RITM is   is created on submit  notification triggers. Now another one more variable is there like CCGROUP, which is reference to  group table. The Notification triggering is working but the cc is not working as expected. Below is my BR script and Notification email script. Thanks in advance.

BR script:

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

   
    var selectedNotificationSysIds = current.variables.template.toString().split(',');
    gs.info('Selected Notification SysIds: ' + selectedNotificationSysIds);

 
    var firstNotificationSysId = 'debeea69839c1e146eb8c3b6feaad310';  
    var secondNotificationSysId = 'c4cc0a2a838c16106eb8c3b6feaad329';

    firstNotificationSysId = firstNotificationSysId.trim();
    secondNotificationSysId = secondNotificationSysId.trim();

   
    var customEmailBody = current.variables.email_content || '';  

    // Retrieve the group for CC
    var groupSysId = current.variables.cc_group;  
    gs.info('cccccc....'+groupSysId);

   
    if (selectedNotificationSysIds.indexOf(firstNotificationSysId) !== -1) {
        gs.info('First Notification is selected. Executing the first part of the script.');

        var uniqueApprovers = [];
        var incidentQuery = new GlideRecord('incident');
        incidentQuery.addEncodedQuery('active=true^stateIN1,2,3^assigned_toISNOTEMPTY');
        incidentQuery.query();

        while (incidentQuery.next()) {
            var approver = incidentQuery.assigned_to.toString();
            if (uniqueApprovers.indexOf(approver) === -1) {
                uniqueApprovers.push(approver);
               
                gs.eventQueue('incident.schedule.reminder', incidentQuery,approver ,customEmailBody);
            }
        }
    }

   
    if (selectedNotificationSysIds.indexOf(secondNotificationSysId) !== -1) {
        gs.info('Second Notification is selected. Executing the second part of the script.');

        var uniqueManagers = [];
        var managerQuery = new GlideRecord('sys_user');
        managerQuery.addEncodedQuery('active=true^managerISNOTEMPTY');
        managerQuery.query();

        while (managerQuery.next()) {
            var managerSysId = managerQuery.manager.sys_id.toString();
            if (uniqueManagers.indexOf(managerSysId) === -1) {
                uniqueManagers.push(managerSysId);
                gs.eventQueue('custom.manager.incident.report', managerQuery, managerSysId, customEmailBody);
            }
        }
    }

})(current, previous);

Email script:
(function runMailScript(current, template, email, email_action, event) {
    // Retrieve custom email body and group sys_ids from event parameters
     var customEmailBody = event.parm2;
     var groupSysIds = event.parm3.split(',');

    // Get the approver's email address
    var approver = new GlideRecord('sys_user');
    if (approver.get(current.assigned_to)) {
        email.addAddress('to', approver.email);

        // Add group members to CC
        groupSysIds.forEach(function(groupSysId) {
            var groupGR = new GlideRecord('sys_user_grmember');
            groupGR.addQuery('group.sys_id', groupSysId);
            groupGR.query();

            while (groupGR.next()) {
                var emailAddress = groupGR.user.email.toString();
                email.addAddress('cc', emailAddress);
            }
        });
    } else {
        return;
    }
   
    var pendingIncidents = [];
    var incidentGr = new GlideRecord('incident');
    incidentGr.addEncodedQuery('active=true^stateIN1,2,3^assigned_to=' + approver.sys_id);
    incidentGr.query();

    var slNo = 1;
    while (incidentGr.next()) {
        var lastAssignedDate = new GlideDateTime(incidentGr.sys_created_on); // Default to creation date if no assignment found
        var auditGr = new GlideRecord('sys_audit');
        auditGr.addQuery('documentkey', incidentGr.sys_id);
        auditGr.addQuery('fieldname', 'assigned_to');
        auditGr.orderByDesc('sys_created_on');
        auditGr.query();
        if (auditGr.next()) {
            lastAssignedDate = new GlideDateTime(auditGr.sys_created_on);
        }

        var currentDate = new GlideDateTime();
        var pendingDaysFromAssignment = Math.floor((currentDate.getNumericValue() - lastAssignedDate.getNumericValue()) / (1000 * 60 * 60 * 24));
        var openedDate = new GlideDateTime(incidentGr.sys_created_on);
        var pendingDaysFromOpened = Math.floor((currentDate.getNumericValue() - openedDate.getNumericValue()) / (1000 * 60 * 60 * 24));

        if (pendingDaysFromAssignment >= 4) {
            var incidentNumber = incidentGr.number.getDisplayValue();
            var shortDescription = incidentGr.short_description.getDisplayValue();
            var createdDate = new GlideDateTime(incidentGr.sys_created_on);
            var state = incidentGr.state.getDisplayValue();

            pendingIncidents.push({
                slNo: slNo++,
                incidentNumber: incidentNumber,
                shortDescription: shortDescription,
                created: createdDate.getDisplayValue().split(' ')[0],
                lastAssignedOn: lastAssignedDate.getDisplayValue().split(' ')[0],
                pendingDaysFromOpened: pendingDaysFromOpened,
                pendingDaysFromAssignment: pendingDaysFromAssignment,
                state: state
            });
        }
    }

    if (pendingIncidents.length === 0) {
        return;
    }

    // Include custom email body in the email template
    template.print(customEmailBody);

    template.print('<p>Dear ' + approver.name + ',</p>');
    template.print('<p>Below, you will find the incidents awaiting your attention .</p>');
  template.print('<table style="border-collapse: collapse; width: 100%;">');
    template.print('<tr style="background-color: #f2f2f2;">');
    template.print('<th style="border: 2px solid #dddddd; padding: 10px; text-align: left;">No.</th>');
    template.print('<th style="border: 2px solid #dddddd; padding: 10px; text-align: left;">Number and Created Date</th>');
    template.print('<th style="border: 2px solid #dddddd; padding: 10px; text-align: left;">Description</th>');
    template.print('<th style="border: 2px solid #dddddd; padding: 10px; text-align: left;">Last Assigned On</th>');
    template.print('<th style="border: 2px solid #dddddd; padding: 10px; text-align: left;">Pending Days from Creation</th>');
    template.print('<th style="border: 2px solid #dddddd; padding: 10px; text-align: left;">Pending Days from Assignment</th>');
    template.print('<th style="border: 2px solid #dddddd; padding: 10px; text-align: left;">State</th>');
    template.print('</tr>');

    pendingIncidents.forEach(function(incident, index) {
        var rowColor = index % 2 === 0 ? '#ffffff' : '#f2f2f2';
        template.print('<tr style="background-color: ' + rowColor + ';">');
        template.print('<td style="border: 2px solid #dddddd; padding: 10px; text-align: left;">' + incident.slNo + '</td>');
        template.print('<td style="border: 2px solid #dddddd; padding: 10px; text-align: left;">' + incident.incidentNumber + '<br>' + incident.created + '</td>');
        template.print('<td style="border: 2px solid #dddddd; padding: 10px; text-align: left;">' + incident.shortDescription + '</td>');
        template.print('<td style="border: 2px solid #dddddd; padding: 10px; text-align: left;">' + incident.lastAssignedOn + '</td>');
        template.print('<td style="border: 2px solid #dddddd; padding: 10px; text-align: left;">' + incident.pendingDaysFromOpened + ' days</td>');
        template.print('<td style="border: 2px solid #dddddd; padding: 10px; text-align: left;">' + incident.pendingDaysFromAssignment + ' days</td>');
        template.print('<td style="border: 2px solid #dddddd; padding: 10px; text-align: left;">' + incident.state + '</td>');
        template.print('</tr>');
    });

    template.print('</table>');
    template.print('<p>Thank you.</p>');
})(current, template, email, email_action, event);


2 REPLIES 2

Mark Manders
Mega Patron

In the BR script, you're setting `groupSysId` from the `cc_group` variable, but this seems to be an individual group rather than a list of group sys_ids. Ensure this variable is being handled as a single `sys_id`, not a list and in the email script, you are splitting `event.parm3` to get `groupSysIds`. However, it appears that `groupSysId` is not being passed as a list of comma-separated sys_ids but as a single `sys_id`. This could cause the email script to iterate over an empty or incorrect list.

 

Business Rule

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

var selectedNotificationSysIds = current.variables.template.toString().split(',');
gs.info('Selected Notification SysIds: ' + selectedNotificationSysIds);

var firstNotificationSysId = 'debeea69839c1e146eb8c3b6feaad310';
var secondNotificationSysId = 'c4cc0a2a838c16106eb8c3b6feaad329';

var customEmailBody = current.variables.email_content || '';

// Retrieve the group for CC
var groupSysId = current.variables.cc_group.getValue(); // Ensure this gets a single sys_id
gs.info('Selected CC Group SysId: ' + groupSysId);

// Pass groupSysId as a single value (not as an array)
if (selectedNotificationSysIds.indexOf(firstNotificationSysId) !== -1) {
gs.eventQueue('incident.schedule.reminder', current, groupSysId, customEmailBody);
}

if (selectedNotificationSysIds.indexOf(secondNotificationSysId) !== -1) {
gs.eventQueue('custom.manager.incident.report', current, groupSysId, customEmailBody);
}

})(current, previous);


Update the email script to handle a single `groupSysId` properly instead of splitting it (don't forget to put your own pending logic and template processing in).

(function runMailScript(current, template, email, email_action, event) {
var customEmailBody = event.parm2;
var groupSysId = event.parm3; // Use a single sys_id, not a list

// Get the approver's email address
var approver = new GlideRecord('sys_user');
if (approver.get(current.assigned_to)) {
email.addAddress('to', approver.email);

// Add group members to CC
var groupGR = new GlideRecord('sys_user_grmember');
groupGR.addQuery('group.sys_id', groupSysId); // Query using a single sys_id
groupGR.query();

while (groupGR.next()) {
var emailAddress = groupGR.user.email.toString();
email.addAddress('cc', emailAddress);
}
}

// Handle pending incidents logic here...

template.print(customEmailBody);

// Other template processing here...
})(current, template, email, email_action, event);

 


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

@Mark Manders  I tried by changing script as you given, But in cc according to sysid of group there is only 5 memebers... but when i trigger this script, in cc more than 20 members are there . The event parm3 (sysid) is not passing to mail script I think?