Email Notification on click of button can we run any server side?

abhishekraman
Tera Contributor
In a ServiceNow email notification, can we add a button that, when clicked, executes a server-side script?
 
Requirements- 🔁 Renewal Workflow via ServiceNow
The email must include an option to extend the variable value  for another 15, 30, or 90 days. When Clicking on 15 days, 30 days or  90 Days . Is it possible to call server side code ?  
 
Target- To update RITM variables value on click of Email button 15, 30, or 90 days.
 
6 REPLIES 6

adityahubli
Tera Guru

Hello @abhishekraman ,

  

I tried your useCase . For this i created one email notification,flow designer flow and Scripted Rest api and its updating RITM Variable .  Also Created one renewal_days variable in catalog item .

 

Steps :

 

  • Created renewal_days variable in the catalog item

  • Developed a Scripted REST API to update RITM variables

  • Configured email notification with action links (15 / 30 / 90 days)

  • Linked email buttons to the Scripted REST API

  • Created Flow Designer flow attached to the catalog item

  • Flow triggers on RITM update for post-processing

  • Tested the complete flow using REST API Explorer

 

 

 

Please Refer this Scripted rest api script :

(function process(request, response) {

    // var ritmSysId = request.queryParams.ritm_sys_id;
    // var days = parseInt(request.queryParams.days, 10);

	var ritmSysId = request.queryParams.ritm_sys_id[0];
	var days = parseInt(request.queryParams.days[0], 10);

    gs.info('=== Extend Renewal API Called ===');
    gs.info('URL: ' + request.url);
    gs.info('ritmSysId: ' + ritmSysId);
    gs.info('days: ' + days);

    // 1. Validate parameters
    if (!ritmSysId || !days) {
        response.setStatus(400);
        response.setBody({
            status: "error",
            message: "Missing parameters",
            received: {
                ritm_sys_id: ritmSysId,
                days: days
            }
        });
        return;
    }

    if ([15, 30, 90].indexOf(days) === -1) {
        response.setStatus(403);
        response.setBody({
            status: "error",
            message: "Invalid extension value. Allowed values: 15, 30, 90"
        });
        return;
    }

    // 2. Fetch RITM
    var ritm = new GlideRecord('sc_req_item');

    // Log the query
    gs.info('Querying sc_req_item with sys_id: ' + ritmSysId);

  
    gs.info('RITM found: ' + ritm.number + ' (sys_id: ' + ritm.sys_id + ')');

    // 3. Check if already extended
    var currentRenewalDays = ritm.variables.renewal_days;
    // if (currentRenewalDays && currentRenewalDays != '') {
	if (currentRenewalDays) {
        response.setStatus(200);
        response.setBody({
            status: "info",
            message: "Renewal already extended by " + currentRenewalDays + " days",
            ritm: ritm.number.toString()
        });
        return;
    }

    // 4. Update variable
	var data='';
    try {
        if (ritm.get(ritmSysId)) {
            ritm.variables.renewal_days = days;
           data=ritm.update();
			
        }

        gs.info('Successfully updated RITM ' + ritm.number + ' with renewal_days: ' + days);
    } catch (e) {
        gs.error('Error updating RITM: ' + e.message);
        response.setStatus(500);
        response.setBody({
            status: "error",
            message: "Failed to update RITM: " + e.message
        });
        return;
    }
	

    // 5. Success response
 var gd=new GlideRecord('sc_req_item')
 if(gd.get(data))
 {
	var num=gd.number
 }
	response.setStatus(200);
    response.setBody({
        status: "success",
        message: "Renewal extended by " + days + " days",
        ritm: num,
        ritm_sys_id: ritm.sys_id
    });
    

})(request, response);

 

Email Notification Body :

<p>Hello,</p>

<p>
Your request requires a renewal extension.  
Please choose one of the options below:
</p>

<p>
<a style="
padding:10px 16px;
background-color:#4CAF50;
color:#ffffff;
text-decoration:none;
border-radius:4px;
display:inline-block;"
href="https://devxxxxx.service-now.com/api/1795019/testingbtn/extend?ritm_sys_id=${sys_id}&days=15">
Extend by 15 Days
</a>
</p>

<p>
<a style="
padding:10px 16px;
background-color:#2196F3;
color:#ffffff;
text-decoration:none;
border-radius:4px;
display:inline-block;"
href="https://devxxxxx.service-now.com/api/1795019/testingbtn/extend?ritm_sys_id=${sys_id}&days=30">
Extend by 30 Days
</a>
</p>

<p>
<a style="
padding:10px 16px;
background-color:#FF9800;
color:#ffffff;
text-decoration:none;
border-radius:4px;
display:inline-block;"
href="https://devxxxxxx.service-now.com/api/1795019/testingbtn/extend?ritm_sys_id=${sys_id}&days=90">
Extend by 90 Days
</a>
</p>

<p>
<b>Note:</b> This action can be performed only once.
</p>

<p>Thank you,<br>
ServiceNow Team</p>

 

Please refer following Screenshots:

adityahubli_0-1768457539729.png    

adityahubli_1-1768457595619.png

 

 

adityahubli_2-1768457682548.png

 

adityahubli_3-1768457735850.png

 

adityahubli_4-1768457773548.png

 

adityahubli_5-1768457881225.png

 

adityahubli_7-1768457985463.png

 

adityahubli_8-1768458011689.png

 

 

If this helps you then mark it as helpful and accept as solution.

Regards,

Aditya,

Technical Consultant

 

 

 

 

 

Hello @abhishekraman ,

 

Did my response helps you ? If yes then mark it as helpful and accept as solution.

Regards,

Aditya,

technical consultant