Send Email Notifications
Send customized email notifications when specific conditions are met.
Table: incident When: after
#email #notification #after #insert #update #glideemailoutbound
Script Code
JavaScript
1(function executeRule(current, previous /*null when async*/) {
2
3 // Configuration: Define when to send email
4 var sendEmail = false;
5 var emailTo = '';
6 var emailSubject = '';
7 var emailBody = '';
8
9 // Example 1: Send email when priority becomes Critical
10 if (current.priority.changesTo('1')) {
11 sendEmail = true;
12 emailTo = current.assignment_group.manager.email.toString();
13 emailSubject = 'URGENT: Critical Incident Assigned - ' + current.number;
14 emailBody = 'A critical priority incident has been assigned to your team.\n\n' +
15 'Number: ' + current.number + '\n' +
16 'Short Description: ' + current.short_description + '\n' +
17 'Caller: ' + current.caller_id.getDisplayValue() + '\n' +
18 'Assigned to: ' + current.assignment_group.getDisplayValue() + '\n\n' +
19 'Please review immediately: ' + gs.getProperty('glide.servlet.uri') +
20 'incident.do?sys_id=' + current.sys_id;
21 }
22
23 // Example 2: Send email when incident is resolved
24 if (current.state.changesTo('6')) { // 6 = Resolved
25 sendEmail = true;
26 emailTo = current.caller_id.email.toString();
27 emailSubject = 'Your Incident Has Been Resolved - ' + current.number;
28 emailBody = 'Hello ' + current.caller_id.first_name + ',\n\n' +
29 'Your incident has been resolved.\n\n' +
30 'Resolution: ' + current.close_notes + '\n\n' +
31 'If you have any questions, please reply to this email.\n\n' +
32 'Thank you,\nIT Support Team';
33 }
34
35 // Send the email if conditions are met
36 if (sendEmail && emailTo) {
37 var mail = new GlideEmailOutbound();
38 mail.setSubject(emailSubject);
39 mail.setBody(emailBody);
40 mail.addAddress(emailTo);
41
42 // Optional: Add CC recipients
43 // mail.addAddress(email, 'cc');
44
45 // Optional: Set from address
46 // mail.setFrom('no-reply@company.com');
47
48 // Optional: Set reply-to
49 // mail.setReplyTo(current.assigned_to.email.toString());
50
51 mail.send();
52
53 gs.info('Email sent to ' + emailTo + ' for incident ' + current.number);
54 }
55
56})(current, previous);
How to Use
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
Related Scripts
Auto-assign Based on Category
Automatically assign tickets to the appropriate assignment group based on category.
Populate Fields on Insert
Automatically populate fields with default values or calculated values when a record is created.
Prevent Duplicate Records
Check for existing records and prevent duplicates based on specific field combinations.