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

Kalaiarasan Pus
Giga Sage

Please add the following lines to the inbound action and try



current.setForceUpdate(true);


current.update();


Hi Kalaiarasan,



Thanks for your reply.


May I know where I can add


current.setForceUpdate(true);


current.update();


in my script.


Before that, can you add the logs to your script and see where the script is failing exactly?


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


}


So if you see the code, its getting driven by sysapproval field which will work for task based tables. Since your table is not extended from task, the code might not work for you.



I would suggest to add logs to the script and check where the script is exactly failing and add the code to use document ID and table name field if it is a non task based approval.