The CreatorCon Call for Content is officially open! Get started here.

Create BR on Email table

sparkles
Tera Contributor

Hi,

 

I need to create a business rule that add comment like (email sent) to the ticket every time fulfiller send an email using the email option or reply all from the ticket itself.  

 

Thanks

S

1 ACCEPTED SOLUTION

Karthiga S
Kilo Sage

Hi @sparkles 

 

you can create a business rule in ServiceNow to add a comment to a ticket every time an email is sent by a fulfiller. 

 

1. Navigate to System Definition > Business Rules in your ServiceNow instance.
2. Click on "New" to create a new business rule.
3. Fill in the necessary fields:
- Name: Give your business rule a name, e.g., "Add Comment on Email Sent".
- Table: Select the table you want the business rule to apply to, e.g., "Incident".
- When to run: Select "after" and "insert".
4. In the "Advanced" tab, write your script. Here's a sample script:

(function executeRule(current, previous /*null when async*/) {
// Check if the update is an outbound email
if (current.type == 'send') {
// Add a comment to the related record
var gr = new GlideRecord(current.target_table);
if (gr.get(current.target_sys_id)) {
gr.comments = 'Email sent by ' + current.user_id.getDisplayValue();
gr.update();
}
}
})(current, previous);

This script checks if the update is an outbound email, and if so, it adds a comment to the related record indicating that an email was sent.

5. Click on "Submit" to save your business rule.

 

Please mark it Correct and Hit Like if you find this helpful!

 

Regards,

Karthiga

View solution in original post

4 REPLIES 4

Karthiga S
Kilo Sage

Hi @sparkles 

 

you can create a business rule in ServiceNow to add a comment to a ticket every time an email is sent by a fulfiller. 

 

1. Navigate to System Definition > Business Rules in your ServiceNow instance.
2. Click on "New" to create a new business rule.
3. Fill in the necessary fields:
- Name: Give your business rule a name, e.g., "Add Comment on Email Sent".
- Table: Select the table you want the business rule to apply to, e.g., "Incident".
- When to run: Select "after" and "insert".
4. In the "Advanced" tab, write your script. Here's a sample script:

(function executeRule(current, previous /*null when async*/) {
// Check if the update is an outbound email
if (current.type == 'send') {
// Add a comment to the related record
var gr = new GlideRecord(current.target_table);
if (gr.get(current.target_sys_id)) {
gr.comments = 'Email sent by ' + current.user_id.getDisplayValue();
gr.update();
}
}
})(current, previous);

This script checks if the update is an outbound email, and if so, it adds a comment to the related record indicating that an email was sent.

5. Click on "Submit" to save your business rule.

 

Please mark it Correct and Hit Like if you find this helpful!

 

Regards,

Karthiga

Thanks Karthiga for your quick reply. However it didn't work. My table is a custom table not out of box so I am not sure if it has any impact.

Hi Karthiga,

I found the issue with the script, the current.type == 'sent' in our table.

After fixing it it works!

Thanks for your help

S

Great. Thanks, @sparkles