We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

GlideEmailOutbound()-Send Notifications using Script with email template

Supriya Mane
Tera Contributor

GlideEmailOutbound()-Send Notifications using Script with email template using business rule how we can configure

6 REPLIES 6

vaishali231
Kilo Sage

Hey  @Supriya Mane 

One important point to understand is that GlideEmailOutbound and the Notification framework are two different mechanisms in ServiceNow.

Many developers try to use GlideEmailOutbound() with an existing Email Template, but this is generally not the recommended approach because GlideEmailOutbound is designed to build and send an email programmatically, while Email Templates are intended to be rendered through the Notification engine.

 Approach 

If your requirement is:

Execute a Business Rule - Send an email using an existing Email Template

This approach provides several advantages:

  1. Separation of business logic and email content
  2. Reusable email templates
  3. Easier maintenance
  4. Supports localization
  5. Honors user notification preferences
  6. Email Preview functionality
  7. Better upgrade compatibility

Step 1 – Create an Email Template

Navigate to:

System Notification → Email → Email Templates

Example:

Template Name

Incident Created Template

 

Body

Hello ${caller_id.first_name},

 

Your incident has been created successfully.

 

Incident Number:

${number}

 

Short Description:

${short_description}

 

Priority:

${priority}

 

Thank you.

 

Step 2 – Create an Event

Navigate to:

System Policy → Events → Registry

Example

Name:

incident.created.mail

 

Table:

Incident

 

Step 3 – Create a Notification

Navigate to:

System Notification → Email → Notifications

Configure:

Table:

Incident

 

When to Send:

Event is fired

 

Event Name:

incident.created.mail

 

Recipients can be:

  1. Caller
  2. Assigned To
  3. Watch List
  4. Groups
  5. Users
  6. Event Parm1 / Parm2

Use your Email Template in the notification body or reference it according to your implementation pattern.

 

Step 4 – Fire the Event from the Business Rule

Business Rule

(function executeRule(current, previous) { gs.eventQueue( "incident.created.mail", current, current.caller_id, "" ); })(current, previous);

Once the Business Rule executes, ServiceNow automatically:

  1. queues the event
  2. processes the notification
  3. renders the email template
  4. sends the email

No email formatting is required in your script.

 

Using GlideEmailOutbound

If you decide to use GlideEmailOutbound, you must construct the email yourself.

Example:

(function executeRule(current, previous) { var email = new GlideEmailOutbound(); email.setSubject("Incident " + current.number); email.setFrom("servicenow@company.com"); email.addAddress("to", current.caller_id.email); email.setBody( "Hello " + current.caller_id.getDisplayValue() + "\n\nIncident Number : " + current.number + "\nShort Description : " + current.short_description ); email.save(); })(current, previous);

This works well for simple scenarios, but everything (subject, body, formatting, recipients, etc.) must be handled in code.

Can GlideEmailOutbound Use an Email Template?

This is one of the most common questions.

The answer is:

Not directly.

There is no supported API such as:

email.useTemplate("Incident Template");

 

or

email.setTemplate(...)

 

that allows GlideEmailOutbound to automatically render an Email Template.

Some developers attempt to retrieve records from the sys_email_template table and manually render them using internal or undocumented APIs. While this may work in certain releases, it is not officially recommended, can change between versions, and may introduce upgrade risks.

When Should GlideEmailOutbound Be Used?

GlideEmailOutbound is appropriate when you need to:

  1. Send fully custom emails from server-side scripts
  2. Generate dynamic content that doesn't fit the Notification framework
  3. Send emails from Background Scripts or Script Includes
  4. Build emails for custom integrations
  5. Send emails where no Notification record is required

When Should Notifications Be Used?

Notifications are the better choice when you need:

  1. Reusable email templates
  2. Event-driven emails
  3. Multiple recipient types
  4. HTML formatting
  5. Localization
  6. Email Preview
  7. Notification logs
  8. User notification preferences
  9. Easier maintenance by administrators

Final Recommendation

For production implementations, I recommend using Business Rule → Event → Notification  Email Template instead of calling GlideEmailOutbound directly.

This keeps business logic separate from presentation, makes email content reusable, allows administrators to modify templates without changing scripts, and fully leverages ServiceNow's notification framework.

Reserve GlideEmailOutbound for scenarios where you need to generate and send a completely custom email in code and do not require Notification features or Email Templates.

***********************************************************************************************************************************

If this response helps, please mark it as Accept as Solution and Helpful.

Doing so helps others in the community and encourages me to keep contributing.

Regards

Vaishali Singh

Servicenow Developer
Linkedin - https://www.linkedin.com/in/vaishali-singh-2273361bb









Ehab Pilloor
Mega Sage

Hi @Supriya Mane,

 

GlideEmailOutbound class implements the email object for scoped applications. It is identical to the email global object available in mail scripts. One can use the GlideEmailOutbound class to send an email without creating a notification record in the Instance. Some of the useful methods of GlideEmailOutbound class are mentioned below.

 

setReplyTo(String address)

This method sets the reply-to address for the email notification. The input type is of string.

 

setFrom(string address)

This method sets the from address for the email notification. The input type is a string.

 

addAddress(Sring type,String address)

This method adds the address to either the cc or bcc list of the email. The input type is a string.

 

setBody(String body Text)

This method adds the body of the email. The input type is a string.

 

setSubject(String subject)

This method sets the subject of the email. The input type is a string.

 

save

This method triggers the notification.

 

Example:

var mail = new GlideEmailOutbound();

mail.setReplyTo('agent@abc.com');

mail.setSubject('Testing outbound email'); 

mail.addAddress('cc', 'admin@example.com'); 

mail.setBody('Hello world!');

mail.save();

EhabPilloor_0-1783505525380.png

 

 

However, GlideEmailOutbound() class lacks the ability to send attachments or add templates to the outbound email. Because of this ServiceNow recommends sending notifications by events and altering the properties of the email using mail scripts.

 

Please refer to ServiceNow Docs for more details.

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards

 

Regards,

Ehab Pilloor