How to use onclick in a button in email script

HrishabhKumar
Tera Contributor

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

3 REPLIES 3

Tanushree Maiti
Tera Patron

Hi @HrishabhKumar 

 

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/developer-forum/how-to-add-button-in-email-notification/td-p/31...

https://www.servicenow.com/community/itsm-forum/onclick-function-in-email-notification/m-p/503958

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

Hi @Tanushree Maiti 

I want to use onclick function and add some custom logic, I dont't want to navigate use to any record.

 

 

Hi @HrishabhKumar 

 

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 

 

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti