How to use onclick in a button in email script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I'm using the following code in my email script.
=============================================================
function myFunc(){
gs.addInfoMessage('Approved');
}
template.print('<button onClick="myFunc()"> Approve request </button>');
=============================================================
I'm able to add this button in notification using ${mail_script:<script_name>}, but when I click on the button, nothing happens.
Let me know how can I use onClick function in email script?
Thanks and regards
Hrishabh Kumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
Try with this sample code:
(function runMailScript(current, template, email, email_action, event) { var buttonStyle = "background-color: #4CAF50; color: white; padding: 10px 20px; text-decoration: none; border-radius: 5px; display: inline-block;"; var url = "https://" + gs.getProperty('instance_name') + "://" + current.getTableName() + ".do?sys_id=" + current.sys_id; template.print('<a href="' + url + '" style="' + buttonStyle + '">View Record</a>'); })(current, template, email, email_action, event);
Refer:
Approve/Reject button in Approval notification
https://www.servicenow.com/community/itsm-forum/onclick-function-in-email-notification/m-p/503958
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I want to use onclick function and add some custom logic, I dont't want to navigate use to any record.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
In ServiceNow email scripts, button elements cannot use the onClick attribute because most email clients, such as Outlook and Gmail, disable JavaScript and event handlers for security purposes.
Servicenow recommendation is to use mailTo:
Refer KB: KB0816015 No Restriction on Email Recipient List(to,cc,bcc) in $mailto script
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti