function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ 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.
Fabio FarathFabio Farath

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.
 

public class CaseApprovalRecall {
    @InvocableMethod
    public static void recallCase(List<RecallCaseRequest> requests){
        if(requests != null && requests.size() > 0){
            RecallCaseRequest request = requests[0];
            Approval.ProcessWorkItemRequest pwr = new Approval.ProcessWorkItemRequest();
            List<ProcessInstance> procins = new List<ProcessInstance>([select Id from ProcessInstance where Status = 'Pending' and TargetObjectId = :request.caseId]);
            if((procins != null) && (procins.size() > 0)){
                // 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(request.statusToUpdate);
                    pwr.setWorkItemId(workitem[0].id);
                    pwr.setAction('Removed');
                    
                    // Execute process request
                    Approval.ProcessResult pr = Approval.process(pwr);
                } 
            }
        }
    }
    
    //RecallCaseRequest
    public class RecallCaseRequest{
    
        @InvocableVariable
        public Id caseId;
        
        @InvocableVariable
        public String statusToUpdate;
        
    }
}

Regards,

Fabio
Pike MoPike Mo
Much obliged to you gbalakri and jlo. 

I have added (https://rhotels.ae/en/ramada-hotel-suites-by-wyndham-dubai-jbr.html)a confirmation of invalid for procins prior to accesing the [0] component of it and furthermore I have created statusToUpdate as a boundary so we can send it as a String from a Process Builder for instance.
tech updatetech update
Hi,

thanks for sharing the Recalling Approval Process but I have tried for Robotic screwdriving systems (https://www.calvindude.com) but it did not work accordingly, It shows an error in execution. May I know what is the reason behind that..?

thanks
fa dffa df
I like your idea. Can I try this scripts for my life coaching (https://dennisberry.com/) program that hosted on WordPress?
frsdwf gvrfdegfrsdwf gvrfdeg
I want to test this tool for my samaras cuisine (https://samarascuisine.com/) blog. Can you share the complete script?
Justin ThymeJustin Thyme
I will use this script for future reference.

Sacramento Bail (https://www.atlantisbailbonds.net/) must be posted by a Sacramento Bail Bonds Agent (https://www.atlantisbailbonds.net/)who knows How Bail Bonds Work Sacramento (https://www.atlantisbailbonds.net/) and can offer a Bail Payment Plan in Sacramento (https://www.atlantisbailbonds.net/). Fast Bail Sacramento (https://www.atlantisbailbonds.net/) can be obtained from someone who provides 24 Hour Bail Bonds Sacramento (https://www.atlantisbailbonds.net/).

This information has inspired me to take online courses and exams to better myself. I currently work in an auto repair shop in Rancho Cordova (https://autorepairshopranchocordova.com/). As an apprentice, I am learning brake repair, (https://autorepairshopranchocordova.com/) automobile electrical repair, (https://autorepairshopranchocordova.com/) transmission repair (https://autorepairshopranchocordova.com/) and suspension repair, (https://autorepairshopranchocordova.com/) but I need more formal education to get a better job.

 
Dinesh Yadav 34Dinesh Yadav 34
https://forcepective.com/illegal-transition-type-error-in-approval-process/