Inbound Email Action to reject a Request is not working. Whereas the Approval Inbound Actions works

msai
Tera Contributor

Hi All,

 

End users was able to approving the requests without any error, but users are trying to reject the request its not working and logs also not sent to ServiceNow, Kindly help here!

 

Regards,

Jaya 

9 REPLIES 9

Mark Manders
Mega Patron

Could you give some more information, like the inbound email, including triggers, how the setup is done, etc.?

The only possible answer to your limited information is: "there must be something wrong in the code of your inbound action. Correct that and it will work." But that doesn't help you.


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Hi Mark,

 

Inbound Email Script: Update Approval Request

 

/*global current, email, gs, GlideController, GlideRecord*/
/*eslint-disable eqeqeq*/
processApprovalEmail();

function processApprovalEmail() {
    "use strict";
    var errorMsg = "";
    var msgArray = [];
   
    if (current.getTableName() != "sysapproval_approver")
        return;
   
    var displayValue = getApprovalDisplayValue(current);
   
    if (!validUser()) {
        gs.log(getFailurePreamble() + "Sender email does not match approval assignee.");
        msgArray.push(displayValue);
        msgArray.push(current.approver.getDisplayValue());
        msgArray.push(current.approver.email);
        errorMsg = gs.getMessage("approvalInvalidUser", msgArray);
        createEmailEvent(errorMsg);
        return;
    }
   
    if (current.state == 'cancelled') {
        gs.log(getFailurePreamble() + "The approval has been canceled.");
        msgArray.push(displayValue);
        errorMsg = gs.getMessage("approvalCancelled", msgArray);
        createEmailEvent(errorMsg);
        return;
    }
   
    if (email.body.state != undefined)
        current.state = email.body.state;
   
    if (email.subject.indexOf("approve") >= 0)
        current.state = "approved";
   
    if (email.subject.indexOf("reject") >= 0){
        current.state = "rejected";
       
        var newhire = new GlideRecord("u_personal_data");
        newhire.addQuery("u_request_no", current.document_id.request);
        newhire.query();
        if (newhire.next()) {
            gs.log("New Hire Approval " + current.document_id.request);
            newhire.u_comments_related_to_rejection = email.body_text;
            newhire.update();
        }
       
    }
   
    if (current.state != "approved" && current.state != "rejected") {
        gs.log(getFailurePreamble() + "The subject is malformed. The approver probably did not click the approve or reject button on the email.");
        msgArray.push(displayValue);
        errorMsg = gs.getMessage("approvalFailed", msgArray);
        createEmailEvent(errorMsg);
        return;
    }
   
    current.comments = "reply from: " + email.from + "\n\n" + email.body_text;
    var controller = new GlideController();
    controller.putGlobal("approvalSource", "email");
    current.update();
    controller.removeGlobal("approvalSource");
   
    function validUser() {
        if (current.approver == email.from_sys_id)
            return true;
       
        // check if the email is from a delegate of the approver
        var g = new GlideRecord("sys_user_delegate");
        g.addQuery("user", current.approver.toString());
        g.addQuery("delegate", email.from_sys_id);
        g.addQuery("approvals", "true");
        g.addQuery("starts", "<=", gs.daysAgo(0));
        g.addQuery("ends", ">=", gs.daysAgo(0));
        g.query();
        return g.hasNext();
    }
   
    function createEmailEvent(msg) {
        gs.eventQueue("approval.email.errorMsg", current, email.from, msg);
    }
   
    function getFailurePreamble() {
        return 'Approval email from ' + email.from + ' for task "' + displayValue + '" assigned to "' + current.approver.getDisplayValue()
        + '" failed because: ';
    }
   
    function getApprovalDisplayValue(approval) {
        if (!gs.nil(approval.sysapproval))
            return approval.getDisplayValue();
        else {
            var target = new GlideRecord(approval.source_table);
            if (target.get(approval.document_id))
                return target.getDisplayValue();
        }
        gs.warn("Target for sysapproval_approver:" + approval.getUniqueValue() + " not found. Target=" + approval.source_table + ":" + approval.document_id);
        return "Unknown";
    }
   
}
 
 
This is the script.
and active the stop processing field 

Narsing1
Mega Sage

Are they updating comments when they Reject the request as the approval rejection needs comments as mandatory field.  Also, check whether any data policies enabled on this table.

 

Thanks,

Narsing

msai
Tera Contributor

Hi Narsing, there is no mandatory field for rejection and approval.