+ Start a Discussion
gbalakrigbalakri 

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

}

Best Answer chosen by Admin (Salesforce Developers) 
jlojlo

 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

Message Edited by jlo on 04-08-2009 12:07 PM

All Answers

jpwagnerjpwagner

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?
            }

Message Edited by jpwagner on 03-03-2009 03:14 PM
Message Edited by jpwagner on 03-03-2009 03:15 PM
heiriticheiritic

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

Glenn at MvistaGlenn at Mvista

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?

Message Edited by Glenn at Mvista on 03-19-2009 11:21 AM
jlojlo

 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

Message Edited by jlo on 04-08-2009 12:07 PM
This was selected as the best answer
danwilkiedanwilkie

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 :)

Mukesh Kumar 107Mukesh Kumar 107
works for me great.
Note: 'Removed' will translate to 'Recalled' in Approval Status, as you see in the system.