You need to sign in to do that
Don't have an account?

Recalling Approval Process
I am trying to execute the following code for recalling an approval process an ends up getting the following error
execution of AfterUpdate caused by: System.DmlException: Process failed. First exception on row 0; first error: INVALID_OPERATION, Illegal transition type:
Approval.ProcessWorkItemRequest pwr = new Approval.ProcessWorkItemRequest();
List<ProcessInstance> procins = new List<ProcessInstance>([select Id from ProcessInstance where Status = 'Pending' and TargetObjectId = :objectId]);
// Retrieve the process work instance id associated to the process instance
List<ProcessInstanceWorkitem> workitem = new List<ProcessInstanceWorkitem>([select Id from ProcessInstanceWorkitem where ProcessInstanceId = :procins[0].id]);
if ((workitem != null) && (workitem.size() > 0))
{
pwr.SetComments(statusToUpdate);
pwr.setWorkItemId(workitem[0].id);
pwr.setAction('Remove');
// Execute process request
Approval.ProcessResult pr = Approval.process(pwr);
}
gbalakri - Try changing this line:
pwr.setAction('Remove');
to this:
pwr.setAction('Removed');
See the docs: http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content%2Fapex_ProcessWorkitemRequest.htm|SkinName=webhelp
All Answers
Hey Girish,
What is "statusToUpdate"? If it's display-text, you need to enclose it in single-quotes.
Only other thought is make sure the Allow submitters to recall approval requests option is selected for the approval process and the code is executed by a sysadmin.
List<ProcessInstanceWorkitem> workitem = new List<ProcessInstanceWorkitem>([select Id, ProcessInstance.TargetObjectId from ProcessInstanceWorkitem where ProcessInstance.TargetObjectId = : objectId and Status = 'Pending']);
Approval.ProcessWorkItemRequest pwr = new Approval.ProcessWorkItemRequest();
if (workitem.size()==1)
{
pwr.setWorkitemId(workitem.id);
pwr.setComments('statusToUpdate');
pwr.setAction('Remove');
Approval.ProcessResult pr = Approval.process(pwr);
}
else{
//result for 2 or more returned?
//result for 0 returned?
}
Hi,
I need to know if Allow submitters to recall approval requests option can be configured via API ? because I do the process from API. can u tell me the detail how to set this option ? thanx..
regards,
edwin
I am attemping the same thing and I used your example. However, I get an error on this line:
pwr.setWorkitemId(workitem.id);
error: Initial term of field expression must be concrete SObject: LIST: SOBJECT: ProcessInstanceWorkitem
What does this mean?
gbalakri - Try changing this line:
pwr.setAction('Remove');
to this:
pwr.setAction('Removed');
See the docs: http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content%2Fapex_ProcessWorkitemRequest.htm|SkinName=webhelp
Thanks for this! I too was using 'Remove' instead of 'Removed'.
Strikes me as a bit odd that it's called 'Removed' anyway, rather than 'Recall', but hey - it works :)
Note: 'Removed' will translate to 'Recalled' in Approval Status, as you see in the system.
Thank you gbalakri and jlo.
I have added a verification of null for procins before accesing the [0] element of it and also I have made statusToUpdate as a parameter so we can send it as a String from a Process Builder for example.
Regards,
Fabio