How to call a email template on click of button in Email Notification Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2023 03:48 AM
Hi All,
I have a requirement that in template we have approve button and clicking on that button one mail should open and in that mail's body part need to call a email template or need to show data in table format.
please check below line of code there is 'body=test', where we want to call an email tempelate which contains html code
template.print('<div style="padding-bottom:32px"><a href="mailto:?subject=test&body=test"><button >Agree</button>');
Quick response is much appreciated.
Best Regards,
Riddhi Patel

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2023 03:58 AM
Hi there,
You can call an email template in the body of the email by using the following method:
- Create an email template in ServiceNow, that contains the HTML code you want to include in the email body.
- In the template.print() function, replace the 'body=test' with a reference to the email template. For example, you can use the sys_id of the email template:
template.print('<div style="padding-bottom:32px"><a href="mailto:?subject=test&body='+email_template_sys_id+'"><button >Agree</button>');
- Now, when the user clicks on the approve button, the email will open and the body of the email will be populated with the contents of the email template.
Alternatively, you can use the g_form.getEmailBody() function to get the body of the email template and pass it to the body of the mail to link.
var emailBody = g_form.getEmailBody('email_template_sys_id');
template.print('<div style="padding-bottom:32px"><a href="mailto:?subject=test&body='+emailBody+'"><button >Agree</button>');
Also, you can use the g_form.addInfoMessage() function to show data in table format within the email body.
var message = '<table>';
message += '<tr><th>Name</th><th>Age</th></tr>';
message += '<tr><td>John Doe</td><td>30</td></tr>';
message += '<tr><td>Jane Doe</td><td>25</td></tr>';
message += '</table>';
g_form.addInfoMessage(message);
template.print('<div style="padding-bottom:32px"><a href="mailto:?subject=test&body='+g_form.getInfoMessages()+'"><button >Agree</button>');
Note: It's important to ensure that the email template and the email body contain appropriate variables and are tested before implementation.
Please mark my answer as helpful and correct, if it really helps you.
Regards,
Zoltán
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2023 03:59 AM
Hi @Riddhi227 ,
Please refer the link, I hope you find this helpful:
https://www.servicenow.com/community/developer-forum/buttons-on-email-notification/m-p/1737003