How to add mail script in notification in servicenow

To add a mail script in a notification in ServiceNow, you can follow these steps:

Prerequisites:

  1. You have a basic understanding of ServiceNow and its notification system.
  2. You have the necessary permissions to create and manage scripts in your ServiceNow instance.

Step 1: Create a new script

  1. Log in to your ServiceNow instance and navigate to the Scripting module.
  2. Click on New and select Script.
  3. Give your script a name, such as "Send email notification".
  4. In the Script field, enter the following code:
    
    import com.snc.notification.email.Email;
    import com.snc.notification.email.EmailMessage;

// Define the email settings String fromAddress = 'your_email_address'; String toAddress = 'recipient_email_address'; String subject = 'Notification from ServiceNow'; String body = 'This is a notification from ServiceNow';

// Create an email message EmailMessage emailMessage = new EmailMessage(); emailMessage.setFrom(fromAddress); emailMessage.setTo(toAddress); emailMessage.setSubject(subject); emailMessage.setBody(body);

// Send the email Email.send(emailMessage);


Replace `your_email_address` and `recipient_email_address` with your actual email addresses.

**Step 2: Create a new notification**

1. Navigate to the **Notifications** module.
2. Click on **New** and select **Notification**.
3. Give your notification a name, such as "Send email notification".
4. In the **Trigger** field, select the trigger that will trigger the notification (e.g., "On update of a record").
5. In the **Script** field, select the script you created in Step 1.

**Step 3: Configure the notification**

1. In the **Notification** form, configure the notification settings as needed (e.g., set the notification level, add additional recipients).
2. Save the notification.

**Step 4: Test the notification**

1. Trigger the notification by updating the record that is associated with the trigger you selected in Step 2.
2. Verify that the email is sent to the recipient with the subject and body you specified in the script.

That's it! You have successfully added a mail script to a notification in ServiceNow.