Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

How to add email addresses to notification dynamically

vidhya_mouli
Giga Sage

Email Script

 

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

    // Read sys_id from event parameter (account sys_id passed in BR)
    var acctSysId = event.parm1;
    
    // Validate account sys_id
    if (!acctSysId) {
        gs.info("SV001: Error: No account sys_id provided in event.parm1");
        return;
    }

    // Get the account record for contract name
    var accountGR = new GlideRecord('customer_account');
    var contractName = '';
    if (accountGR.get(acctSysId)) {
        contractName = accountGR.name.toString();
    } 

    // Add recipients
    var recipientCount = 0;
    var contactGR = new GlideRecord('customer_contact');
    contactGR.addQuery('account', acctSysId);
    contactGR.addQuery('u_email_notification', true);
    contactGR.addQuery('email', '!=', ''); // Ensure email is not empty
    contactGR.query();

    while (contactGR.next()) {
        if (contactGR.email && contactGR.email.toString().trim() !== '') {
            email.addAddress("to", contactGR.email.toString(), contactGR.name.toString());
            recipientCount++;
        }
    }

    // Check if we have recipients
    if (recipientCount === 0) {
        gs.info("SV001: Warning: No recipients found for account: " + acctSysId);
        return;
    }

    // Build portal link with proper base URL
    var portalSuffix = new sn_ex_emp_fd.FoundationNotificationUtil().getPortalSuffix();
    var linkUrl = '/' + portalSuffix + '?id=monitoring_reports';

    // Set subject
    email.setSubject("New monthly monitoring report is available for " + contractName);

    // Build HTML body for better formatting
    var body = '';
    body += '<p>Hello,</p>';
    body += '<p>A new monthly monitoring report has been added recently to the ' + contractName + '.</p>';
    body += '<p>Please visit our Customer Portal to download it.</p>';
    email.setBody(body);
	
    gs.info("SV001: Email prepared successfully for " + recipientCount + " recipients");
	gs.info("SV001: Body: \n" + body);

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

 

On notification: ${mail_script:get_recipient_contract_attachment}

I can see the logs and confirm everything works as expected in the mail script. However the email is not getting generated. I can see 2 active users email in the logs. How to fix this?

1 ACCEPTED SOLUTION

vidhya_mouli
Giga Sage

I was able to resolve it after going through few community posts. It seems addAddress does not work in mail script. So I did the following:
1. moved the script to a BR and passed sysid and email ids as parameters.
2. Generated body of the text dynamically. 

3. Who to send -> added param 2.

 

This worked.

View solution in original post

3 REPLIES 3

Sanjay34
Tera Contributor

Use the below format.

 


(function runMailScript(current, template, email, email_action, event) {
if (current.u_manager.email) {
email.addAddress("to", current.u_manager.email, "Manager");
}

 

  • current.requested_for → is the user record of the Requester.

  • .email → gets that user’s email address.

  • "Requester" → is just a label that appears in the email logs (for debugging).

 

Nehal Dhuri
Mega Sage

hello @vidhya_mouli 
as per servicenow doc you can use cc and bcc in addAddress fuction. 

Please hit like and mark my response as correct if that helps

vidhya_mouli
Giga Sage

I was able to resolve it after going through few community posts. It seems addAddress does not work in mail script. So I did the following:
1. moved the script to a BR and passed sysid and email ids as parameters.
2. Generated body of the text dynamically. 

3. Who to send -> added param 2.

 

This worked.