How to add button in email notification

anvitha ash
Tera Contributor

Hello,

 

I need to add a button in email notification triggered to user, button called "Close Request" when user click on the button it should update the RITM record state to "Closed Completed" 

 

How can I achieve this. Could someone please let me on this 

 

Thanks in advance 😃 

1 ACCEPTED SOLUTION

Hello @anvitha ash 

Sorry for delay in response.

After going through entire overnight discussion do find updated things for your requirement.


Over email notification you need two buttons so the HTML source code as below:

 

<table style="border-collapse: collapse; width: 100%;" border="0">
<tbody>
<tr>
<td style="width: 100%; height: 52px; padding-top: 16px;"><span id="closerequest" class="btn" style="border-radius: 4px; border: 1px solid #4f52bd; padding: 6px 16px; color: #4f52bd;">${mailto:mailto.btn.closeRequest}</span><span id="createtaskforrequest" class="btn inverse" style="margin-left: 16px; border-radius: 4px; border: 1px solid #4f52bd; padding: 6px 16px; background-color: #4f52bd; color: #fff;">${mailto:mailto.btn.createtaskforrequest}</span></td>
</tr>
</tbody>

 

 

Then you will need two email template which will showcase details over your button and when click it will open a mailbox window so user will send email to ServiceNow instance.

Email template is created under table = sysevent_email_template

 

First will be for Close Request which will be as below:

VirajHudlikar_0-1737804470972.png

 

Second will be Create Task which will be as below:

VirajHudlikar_1-1737804502434.png

 

 

Email Preview will be as below:

VirajHudlikar_0-1737805174038.png

 

Now when any button is click it will open an outlook window to reply back to our instance which will look as below:

VirajHudlikar_2-1737804810270.png

VirajHudlikar_3-1737804868222.png

 

 

Now to when this email comes in ServiceNow we need inbound action which I have already provided above in reply but to save scroll time here we go again with same.

 

Inbound action named as Create Task on table sc_task with type as reply and condition as mail.subject.indexOf('Create Task') > -1 and script will be as below

 

 var gr = new GlideRecord('sc_task');
    gr.initialize();
    gr.setValue('parent', email.instance);
    gr.setValue('short_description', 'New Task for RITM ' + email.instance);
    gr.insert();

 

 

Inbound Action with name as Close Request, for table sc_req_item with type as Reply and condition something as email.subject.indexOf('Close Request') > -1 which checks whether email subject is having word as Close Request and then in script field you will need to put code to update ritm to close when email is process so it will be as below. 

 

 var gr = new GlideRecord('sc_req_item');
    if (gr.get(email.instance)) {
        gr.setValue('state', 3); // 3 is the value for "Closed Complete"
        gr.update();
    }

 

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

View solution in original post

35 REPLIES 35

Viraj Hudlikar
Giga Sage

Hello @anvitha ash 

 

To add a "Close Request" button in an email notification that updates the RITM record state to "Closed Completed" when clicked, you can use a combination of email scripts and inbound email actions in ServiceNow.

 

First, create an email script that generates the text with a link to a custom URL. This URL will trigger an inbound email action to update the RITM state.

email script as below:

 

(function() {
    var instanceName = gs.getProperty('instance_name');
    var emailLink = instanceName + '.service-now.com/api/now/v1/table/sc_req_item/' + current.sys_id + '?sysparm_action=update&state=3'; // 3 is the value for "Closed Complete"
    template.print('<a href="' + emailLink + '" style="padding: 10px 20px; background-color: #4CAF50; color: white; text-decoration: none; border-radius: 5px;">Close Request</a>');
})();

 

Then you add the email script to the notification you will be sending to user and in message body you need to put as below 

 

${mail_script:Close Request Button}

 

Last will be Create an Inbound Email Action with name as Close Request, for table sc_req_item with type as Reply and condition something as email.subject.indexOf('Close Request') > -1 which checks whether email subject is having word as Close Request and then in script field you will need to put code to update ritm to close when email is process so it will be as below. 

 

(function runAction(email, email_action, event) {
    var gr = new GlideRecord('sc_req_item');
    if (gr.get(email.instance)) {
        gr.setValue('state', 3); // 3 is the value for "Closed Complete"
        gr.update();
    }
})(email, email_action, event);

 

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

Hello @Viraj Hudlikar thanks for your response 

 

In the email Script i have added . Screenshot 

 

IMG_20250124_203426.jpg

 

I also need to create one more button "Create Task" on click it should create a task .

There should be two buttons 

1. Close request 

2. Create Task

 

Close request: close the RITM 

Create Task: to create another task with in the RITM 

Hello @anvitha ash 

 

Same process almost.
For create task button email script will be as below 

(function() {
    var instanceName = gs.getProperty('instance_name');
    var emailLink = instanceName + '.service-now.com/api/now/v1/table/sc_task?sysparm_action=insert&parent=' + current.sys_id + '&short_description=New Task for RITM ' + current.number;
    template.print('<a href="' + emailLink + '" style="padding: 10px 20px; background-color: #007BFF; color: white; text-decoration: none; border-radius: 5px;">Create Task</a>');
})();

 

similarly add the email script to notification we did earlier

${mail_script:Create Task Button}


then inbound action named as Create Task on table sc_task with type as reply and condition as mail.subject.indexOf('Create Task') > -1 and script will be as below

(function runAction(email, email_action, event) {
    var gr = new GlideRecord('sc_task');
    gr.initialize();
    gr.setValue('parent', email.instance);
    gr.setValue('short_description', 'New Task for RITM ' + email.instance);
    gr.insert();
})(email, email_action, event);

 

If my response has helped you hit helpful button and if your concern is solved do mark my response as correct.

 

Thanks & Regards
Viraj Hudlikar.

@anvitha ash - also one thing I just noticed in your image do copy only code in your email script function rather than copying entire function line which I gave as I skip syntax 😉

 

In short remove line 4 & 8 from image you shared.