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

Need this information to identify the issue

  • Can you observe the "Email Logs" under the related list of the received email and see whether relevant inbound action got "skipped".  If skipped, open that record and observe the error message that it is showing.  Most of the cases, you will be able to resolve the issue by seeing this.
  • Need to observe the Inbound action script and the conditions in case if the above one is not helpful for you.
  • Need the email subject line and body of the email

Thanks,

Narsing 

msai
Tera Contributor

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 

I am not seeing issues with the script.  Probably a try catch block would be enough to see whether you are getting any errors when the script runs on rejections.

As per the screen shot, it looks to me it is failing in the Inbound action conditions itself.  The from address is not matching with the condition and looks like the address is added to the CC. 

Can you check the condition / try to remove the condition and reprocess it for a testing purpose.  If that works, probably need to do something in the condition itself.

Narsing1_0-1709450609753.png

Thanks,

Narsing

 

msai
Tera Contributor

Hi All,

 

I need help here, I have P1 incident regarding this issue

Mark Manders
Mega Patron

You said it was the 'Update Approval Request' Inbound action, but I don't see it in your screenshot of skipped scripts. Or is the script part of the one your screenshot showed (966)? If that's the case, the reason is in your message: it's coming from the wrong email address. But since your email is added to the record, I am assuming at least one inbound action was triggered.

Did it run the update on the 'u_personal_data' table you have scripted if the subject contains 'reject'?

And if you have a P1, it means it is already on PROD? If so: did this ever work? If yes: what changed between then and now?


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