← Script library

Business Rules

Send Email Notifications

Send customized email notifications when specific conditions are met.

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

  // Configuration: Define when to send email
  var sendEmail = false;
  var emailTo = '';
  var emailSubject = '';
  var emailBody = '';

  // Example 1: Send email when priority becomes Critical
  if (current.priority.changesTo('1')) {
    sendEmail = true;
    emailTo = current.assignment_group.manager.email.toString();
    emailSubject = 'URGENT: Critical Incident Assigned - ' + current.number;
    emailBody = 'A critical priority incident has been assigned to your team.\n\n' +
                'Number: ' + current.number + '\n' +
                'Short Description: ' + current.short_description + '\n' +
                'Caller: ' + current.caller_id.getDisplayValue() + '\n' +
                'Assigned to: ' + current.assignment_group.getDisplayValue() + '\n\n' +
                'Please review immediately: ' + gs.getProperty('glide.servlet.uri') +
                'incident.do?sys_id=' + current.sys_id;
  }

  // Example 2: Send email when incident is resolved
  if (current.state.changesTo('6')) {  // 6 = Resolved
    sendEmail = true;
    emailTo = current.caller_id.email.toString();
    emailSubject = 'Your Incident Has Been Resolved - ' + current.number;
    emailBody = 'Hello ' + current.caller_id.first_name + ',\n\n' +
                'Your incident has been resolved.\n\n' +
                'Resolution: ' + current.close_notes + '\n\n' +
                'If you have any questions, please reply to this email.\n\n' +
                'Thank you,\nIT Support Team';
  }

  // Send the email if conditions are met
  if (sendEmail && emailTo) {
    var mail = new GlideEmailOutbound();
    mail.setSubject(emailSubject);
    mail.setBody(emailBody);
    mail.addAddress(emailTo);

    // Optional: Add CC recipients
    // mail.addAddress(email, 'cc');

    // Optional: Set from address
    // mail.setFrom('no-reply@company.com');

    // Optional: Set reply-to
    // mail.setReplyTo(current.assigned_to.email.toString());

    mail.send();

    gs.info('Email sent to ' + emailTo + ' for incident ' + current.number);
  }

})(current, previous);

How to use it

1. Create an after Business Rule on your table 2. Check the appropriate timing checkboxes (Insert, Update, etc.) 3. Customize the email conditions and content 4. Test with various scenarios

Adapt the table names, fields, and conditions to your instance. Test the behavior in a development environment before using it in production.