Update Approval Request : did not create or update sysapproval_approver using current - from the logs

Ramel
Mega Guru

Hi Community,

I am trying to implement an approval via email but the OOB inbound action does not seem to be working. In the logs I can see this message - Update Approval Request : did not create or update sysapproval_approver using current, then after sending the approval via email, it is being received and I can see it from the approval logs but the script in the OOB inbound action does not do anything. The approval state remains requested but based on the script in the OOB Update Approval Request inbound action once 'approve' it should change the state to approved or reject when rejected.

Anyone had encountered the same issue? Need your advise. Thank you.


Regards,

Ramel

1 ACCEPTED SOLUTION

Arkesh Kumar
Giga Guru

#Inbound actions to record action (written on change_task, record_action , reply)


condtion : email.subject.indexOf("autoclose") >= 0 // checks email subject for autoclose keyword

Action script:

if (current.getTableName() == "change_task") {
if (validUser()) {
current.work_notes = "reply from: " + email.from + "\n\n" + email.body_text;

// if it's been cancelled, or completed already it won't update
var doit = true;
if (current.state == '3' || current.state == '4' || current.state == '5' || current.state == '6' || current.state == '7')
doit = false;

if (email.subject.indexOf("Closed Complete") >= 0) // checks email subject for closed complete keyword
{
current.close_notes = email.body_text;
current.state = '3';
}
if (email.subject.indexOf("Closed Incomplete") >= 0) // checks email subject for closed Incomplete keyword
{
current.close_notes = email.body_text;
current.state = '4';
}
if (email.subject.indexOf("Closed Cancelled") >= 0) // checks email subject for closed cancelled keyword
{
current.close_notes = email.body_text;
current.state = '5'; 
}
if (email.subject.indexOf("Closed Complete with Issues") >= 0) // checks email subject for closed complete with issues keyword
{
current.close_notes = email.body_text;
current.state = '6';
}
if (email.subject.indexOf("Closed and Failed") >= 0) // checks email subject for Closed and Failed keyword
{
current.close_notes = email.body_text;
current.state = '7';
}

if (doit) {
current.update();
} else {
gs.log("Update for Change Task ("+current.number + ") failed. Details: Invalid state. Task can only be updated if state is not already completed or cancelled");
}
} else {
gs.log("Update for Change Task ("+current.number + ") failed. Details: invalid user. Task can only be updated by the assignee, not " +email.from );
}
}

function validUser() {

var user = email.from_sys_id;
var ourUser = gs.getUser();
ourUser = ourUser.getUserByID(user);
ourUser.isMemberOf(current.assignment_group);

if(ourUser)
{
return true;
}

else if (current.assigned_to == email.from_sys_id)
{
return true;
}
}

 

Now go to templates under system notification and create records as below

find_real_file.png

 

Go to notification and link template

find_real_file.png

{$mailto:<template_name>} is the format to link template to notification

 

 Change above data as per your convenience. Presently it is in change_task table. and do action based on email subject from user who is the member of the assignment group of change task.

Thank you,

Arkesh

View solution in original post

28 REPLIES 28

Inbound email action is OOB - it is the one called Update Approval Request.

Email log shows the below for that specific inbound action

find_real_file.png

Can you please share received email subject and inbound action script?

 

Thanks,

Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Update Approval Request - OOB Inbound Action Script:

 

/*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;
}

//Added for 'No Longer Required' approval state
if (current.state == 'not_required') {
gs.log(getFailurePreamble() + "The approval is no longer required.");
msgArray.push(displayValue);
errorMsg = gs.getMessage("approvalNotRequired", msgArray);
createEmailEvent(errorMsg);
return;
}

if (email.body.state != undefined)
current.state = email.body.state;

//if (email.subject.indexOf("approve") >= 0)
if (email.subject.indexOf("approve") >= 0) //|| email.body.indexOf("approve") >= 0)
current.state = "approved";

//if (email.subject.indexOf("reject") >= 0)
if (email.subject.indexOf("reject") >= 0) //|| email.body.indexOf("reject") >= 0)
current.state = "rejected";

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() {
return new ApprovalDelegationUtil().isMyApproval(current);
}

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";
}

}

 

RECEIVED EMAIL SUBJECT:

find_real_file.png

Hi,

 

Above script is works fine

Can you check in email log is received mail getting ignored?

If yes then create a user in servicenow and add it to assignment group/approval group.

 

And also check if user email id is same as approver email id.

 

thank you,

Arkesh

Hi Arkesh,

Mail is received but the email log says: Update Approval Request : did not create or update sysapproval_approver using current 

I am the approver and the email ID matches my email in the sys_user record.

Regards,

Ramel