You need to sign in to do that
Don't have an account?
Approval through apex giving error
I am writing a trigger to approve a record submitted for approval using the standard 'Submit for approval' button. But when i do that the record is getting approved twice with the same date and time of approval. I was suubmitting the record for approval in the morning through apex and it was getting submitted twice. In the afternoon it was working fine, executing only one submission. This is happening with the same apex code without any modification. Can anyone please help me out? I am giving a sample for the code i have used below.
trigger triggerName on customObject (after insert, after update) { customObject cObj = Trigger.new[0]; ProcessInstance[] prInst = [Select Id from ProcessInstance where
TargetObjectId=:cObj.Id and Status='Pending']; if(prInst.size()>0){
ProcessInstanceWorkitem[] prInstWItem = [select Id,OriginalActorId from
ProcessInstanceWorkitem where ProcessInstanceId=:prInst[0].Id]; Approval.ProcessWorkitemRequest prWkItem =
new Approval.ProcessWorkitemRequest();
prWkItem.setWorkitemId(prInstWItem[0].Id);
prWkItem.setComments('Approving request.'); prWkItem.setAction('Approve'); Approval.ProcessResult prResult = Approval.process(prWkItem);
}
}
Thanks in advance for your help.