The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Email based Approval with Approve/Reject button

Sujit Karn
Tera Contributor

//Create a policy to action on approve and reject button
Open or Create a policy: Navigate to System Policy -> Email > Inbound Action

Click on New button

Name: Approve/Reject
Target table: Incident
Action type: Record Action //you can use your required table an action type

Navigate below tab one by one and save your record

When to run
    Type: Reply
     //you can use your exe order, condition, roles or leave as it is
Description
    //Email inbound for incident table

Actions
//Email inbound code

(function runAction(/*GlideRecord*/ current, /*GlideRecord*/ event, /*EmailWrapper*/ email, /*ScopedEmailLogger*/ logger, /*EmailClassifier*/ classifier) {

	// Implement email action here
	if (current.state == "1") { //incident state New

		if (email.subject.indexof("approve") >= 0)
			current.state = "2"; //incident state In Progress
		current.update();

		if (email.subject.indexof("reject") >= 0)
			current.state = "8"; //incident state Cancelled
		curre.update();
	}

})(current, event, email, logger, classifier);

 

//Upload approval images

Upload or create images: Navigate to System UI -> Images
Click on New button
Select Category say General
Name:Approve_REQ.png //don't forget to add image file type like .jpg, .bmp, .gif, .png etc
Image: Click to add...

Repeat the steps to upload Reject_REQ.png image

 

//create 2 (approve and reject) Notifications Email Scripts
Open or Create a Notification: Navigate to System Notifications -> Email > Notifications Email Scripts

 

//Approve button code

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {

    // Initialize reusable variables
    //var mailPic = template.evaluate('<img src="Approve_REQ.png" width="150" height="70"/>');
	var mailPic = "/Approve_REQ.png";
    var mailResponse = "approve";
    var mailBody = "Request is approved";

    // Instantiate the Script Include class
    var mailGenerator = new ApproveRejectMail();

    // Generate the mailto link
    var mailLink = mailGenerator.generateMailLink(mailPic, mailResponse, mailBody);

    // Output the link to the email template
    template.print(mailLink);

})(current, template, email, email_action, event);

 

//Reject button code

(function runMailScript(/* GlideRecord */ current, /* TemplatePrinter */ template,
          /* Optional EmailOutbound */ email, /* Optional GlideRecord */ email_action,
          /* Optional GlideRecord */ event) {

    // Initialize reusable variables
    //var mailPic = template.evaluate('<img src="Reject_REQ.png" width="150" height="70"/>');
	var mailPic = "/Reject_REQ.png";
    var mailResponse = "reject";
    var mailBody = "Request is rejected";

    // Instantiate the Script Include class
    var mailGenerator = new ApproveRejectMail();

    // Generate the mailto link
    var mailLink = mailGenerator.generateMailLink(mailPic, mailResponse, mailBody);

    // Output the link to the email template
    template.print(mailLink);

})(current, template, email, email_action, event);

 

//create Notifications mail to add approve and reject mail script
Open or Create a Notification: Navigate to System Notifications -> Email > Notifications
Name: Approver Response
Table: Incident
Category: say Uncategorized //you can use your required table an category
Navigate below tab one by one and save your notification

 

When to send
   Send When: Record inserted or updated
   Updated:Checked
   Conditions:
                 Assignment group is Prod Config OR
                Assignment group changes to Prod Config //you can use your support group

 

Who receives
     Users: say System Administrator
     Users/Groups in fields: //define as required or leave as it is
     Groups: //define as required or leave as it is

 

What it will content
     Email template: //define as required or leave as it is
     Subject: ${number} has been assigned to your group <b>Prod Config</b> to review
     Message HTML:

                        Hello Administrator,

 

An incident request ${number} has been received for your review. Please have a look:  ${URI_REF} and provide your response by clicking on Approve or Reject button.

${mail_script:approve_REQ} ${mail_script:reject_REQ}


                       Thank you
                       ITSM Team

 

Preview of Notification mail

Approval_Preview.jpg

 

//create a script includes to work above configurations
Open or create Script Includes: Navigate to System UI -> Script Includes
Click on new button
Name: ApproveRejectMail

 

//ApproveRejectMail code

var ApproveRejectMail = Class.create();
ApproveRejectMail.prototype = {
    initialize: function() {
        this.instance = gs.getProperty("instance_name");
        this.link = "https://" + this.instance + ".service-now.com";
        this.emailAddress = this.instance + "@service-now.com"; //you can customize your email address
    },

    generateMailLink: function(mailPic, mailResponse, mailBody) {
        var wMark = email.watermark || "";
        var number = current.number;

        // Check if the current table is a sysapproval table
        if (current.getTableName().indexOf("sysapproval") !== -1 && current.sysapproval) {
            number = current.sysapproval.number;
        }

        // Safely encode email body and subject
        var subject = encodeURIComponent("Re:" + number + " " + mailResponse);
        var body = encodeURIComponent(mailBody + wMark);

        // Construct the mailto link with embedded image
        var mailLink = '<a href="mailto:' + this.emailAddress +
                       '?subject=' + subject +
                       '&body=' + body +
                       '"><img src="' + this.link + mailPic + '"></a>';

        return mailLink;
    },

    type: 'ApproveRejectMail'
};

 

Time to Verify Your Script
After an event that triggers the notification occurs, navigate to view the sent email and verify the content generated by your script.
Navigate to System Logs -> Emails
System Mailboxes -> Outbound > Outbox 

Thank you for your patience

0 REPLIES 0