email approval by email

Bhaskar24
Tera Expert

Hi,

I have created one table Purchase Request, which is not extended from Task table.

I have created relationship of Purchase Request table with Approval table, mentioned query with in that is: "current.addQuery("document_id", parent.sys_id)".

I have created one email notification to approve/reject request by Cost Center Manager, If I am trying to approve request by email the status of my approval is not updating.

I have verified the details in System Mailboxes--->Outbound--->Skipped, in that email log message is showing as: "Skipping 'Update Approval Request', did not create or update sysapproval_approver".

Could you please help on this issue, As I am trying to resolve the issue, but i am not getting what is the cause.

Please find the attached image for Approval record of my request:

Approval Screen shot.PNG

Thanks & Regards,

Bhaskar Reddy

10 REPLIES 10

arnabwa
Giga Guru

Hi Bhaskar,



We would definitely like help you.


Taking the 1st step, Could you please post the script in "Update Approval Request"? I believe this will help us a lot.



Thanks,


Arnab


Hi Arnab,



Thanks for your reply.


Inbound Action: "Update Approval Request", we are using out of box only.



Script:


if (current.getTableName() == "sysapproval_approver") {


  if (validUser()) {


  gs.log("Parent ticket state--"+current.sysapproval.state,'Approval');


  if(current.sysapproval.state==-5||current.sysapproval.state==1){



  // if it's been cancelled, it's cancelled.


  var doit;


  if (current.state == 'cancelled')


  doit = false;



  if(current.state=='requested'){


  doit=true;


  current.comments = "reply from: " + email.from + "\n\n" + email.body_text;


  if (email.body.state != undefined)


  current.state = email.body.state;



  if (email.subject.indexOf("approve") >= 0){


  current.state = "approved";


  if(current.sysapproval.sys_class_name == 'change_request'){


  var change = new GlideRecord('change_request');


  change.get('sys_id', current.sysapproval.sys_id);





  if(current.group != ''){


  var groupApproval = new GlideRecord('sysapproval_group');


  groupApproval.addQuery('sys_id', current.group);


  groupApproval.addQuery('parent',change.sys_id);


  groupApproval.query();



  if(groupApproval.next()){


  var indApproval = new GlideRecord('sysapproval_approver');


  indApproval.addQuery('group', groupApproval.sys_id);


  indApproval.addQuery('parent', change.sys_id);


  indApproval.addQuery('sys_id', '!=', current.sys_id);


  indApproval.query();



  while(indApproval.next()){


  indApproval.state = 'not_required';


  indApproval.update();


  }


  groupApproval.approval = 'approved';


  groupApproval.update();


  }



  }


  change.comments=current.approver.name+' has provided the approval for this change.';


  change.update();


  }


  var comments="Approved \n"+ebody;}



  if (email.subject.indexOf("reject") >= 0){


  current.state = "rejected";}


  }




  if (doit){


  current.update();


  var table=current.sysapproval.sys_class_name;


  //if(email.subject.indexOf("CHG") >= 0){table='change_request';}


  if (email.subject.indexOf("reject") >= 0){


  var gr=new GlideRecord(table);


  gr.addQuery('sys_id',current.sysapproval.sys_id);


  gr.query();


  if(gr.next()){


  var ebody='';


  if(table=='change_request'){ebody=current.approver.name+' has Rejected the change. \n';}


  else{ebody='Rejected \n';}


  ebody+=email.body_text.substring(0,email.body_text.indexOf(email.body.ref)-4);


  gr.comments = ebody;


  gr.update();}


  }


  }


  else{gs.log("Approval for task ("+current.sysapproval.getDisplayValue()+") is already cancelled or rejected");}


  }


  else{gs.log("Approval for task ("+current.sysapproval.getDisplayValue()+") rejected because Parent ticket state does not match with approval record");}


  }




  else {


  gs.log("Approval for task ("+current.sysapproval.getDisplayValue()+") rejected because user sending email( "+email.from+") does not match the approver ("+current.approver.getDisplayValue()+")");


  }


}




function validUser() {


  if (current.approver == email.from_sys_id || gs.hasRole('admin'))


  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();


}


jose_valle
ServiceNow Employee
ServiceNow Employee

Hi Bhaskar,



You will see this message when the inbound action runs, but the script does not make use of "current" update the record. You may want to review the logic in inbound action to see if perhaps took a path in the code that does not actually update the record. Usually adding some logging to the inbound action script can help out in figuring out which path it takes.  



Are you using the out of the box "Update Approval Request" inbound action?


Hi Jose,



Thanks for your reply.


Inbound Action: "Update Approval Request", we are using out of box only.



Script:


if (current.getTableName() == "sysapproval_approver") {


  if (validUser()) {


  gs.log("Parent ticket state--"+current.sysapproval.state,'Approval');


  if(current.sysapproval.state==-5||current.sysapproval.state==1){



  // if it's been cancelled, it's cancelled.


  var doit;


  if (current.state == 'cancelled')


  doit = false;



  if(current.state=='requested'){


  doit=true;


  current.comments = "reply from: " + email.from + "\n\n" + email.body_text;


  if (email.body.state != undefined)


  current.state = email.body.state;



  if (email.subject.indexOf("approve") >= 0){


  current.state = "approved";


  if(current.sysapproval.sys_class_name == 'change_request'){


  var change = new GlideRecord('change_request');


  change.get('sys_id', current.sysapproval.sys_id);





  if(current.group != ''){


  var groupApproval = new GlideRecord('sysapproval_group');


  groupApproval.addQuery('sys_id', current.group);


  groupApproval.addQuery('parent',change.sys_id);


  groupApproval.query();



  if(groupApproval.next()){


  var indApproval = new GlideRecord('sysapproval_approver');


  indApproval.addQuery('group', groupApproval.sys_id);


  indApproval.addQuery('parent', change.sys_id);


  indApproval.addQuery('sys_id', '!=', current.sys_id);


  indApproval.query();



  while(indApproval.next()){


  indApproval.state = 'not_required';


  indApproval.update();


  }


  groupApproval.approval = 'approved';


  groupApproval.update();


  }



  }


  change.comments=current.approver.name+' has provided the approval for this change.';


  change.update();


  }


  var comments="Approved \n"+ebody;}



  if (email.subject.indexOf("reject") >= 0){


  current.state = "rejected";}


  }




  if (doit){


  current.update();


  var table=current.sysapproval.sys_class_name;


  //if(email.subject.indexOf("CHG") >= 0){table='change_request';}


  if (email.subject.indexOf("reject") >= 0){


  var gr=new GlideRecord(table);


  gr.addQuery('sys_id',current.sysapproval.sys_id);


  gr.query();


  if(gr.next()){


  var ebody='';


  if(table=='change_request'){ebody=current.approver.name+' has Rejected the change. \n';}


  else{ebody='Rejected \n';}


  ebody+=email.body_text.substring(0,email.body_text.indexOf(email.body.ref)-4);


  gr.comments = ebody;


  gr.update();}


  }


  }


  else{gs.log("Approval for task ("+current.sysapproval.getDisplayValue()+") is already cancelled or rejected");}


  }


  else{gs.log("Approval for task ("+current.sysapproval.getDisplayValue()+") rejected because Parent ticket state does not match with approval record");}


  }




  else {


  gs.log("Approval for task ("+current.sysapproval.getDisplayValue()+") rejected because user sending email( "+email.from+") does not match the approver ("+current.approver.getDisplayValue()+")");


  }


}




function validUser() {


  if (current.approver == email.from_sys_id || gs.hasRole('admin'))


  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();


}