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
davidesidavidesi 

Update Approval process status

Hi!

When a user updates one opportunity with approved status, I need to change the opportunity status and modify its approval process process.

 

For updating the opportunity I have made a workflow rule that works fine, but I am not able of update its approval process status.

 

My code is below, and it is fired correctly within a trigger:

 

public static void cambiarEstadoAprobacionOportunidad(Opportunity opp){
        List<ProcessInstanceWorkitem> workItemList;
            
        try
        {
    /*        opp.Approval_Status__c='Abierta';
            opp.Production_Approval__c=False;
            update Opp;*/
            }
            Catch(DMLException e)
            {
            opp.addError(e);
            }
        for (SObject s:[SELECT STATUS from processInstance where TargetObjectId=:opp.Id]){
        
        System.debug('Estado ' + s);
        }
        List<ProcessInstance>  processList =  new List<ProcessInstance>();
        processList =[SELECT Id, Status FROM ProcessInstance where TargetObjectId=:opp.Id and (Status =: 'Pending' OR Status=:'Aprobado')];
        Set<ID> idSet = new Set<ID>();
        for(ProcessInstance p :  processList)
            {
            idSet.add(p.Id);   
            }
        workItemList = new List<ProcessInstanceWorkitem>();
        workItemList = [Select id from ProcessInstanceWorkitem where ProcessInstanceId in: idSet];
        for(ProcessInstanceWorkitem w:workItemList)
            {
            Approval.ProcessWorkItemRequest pwr = new Approval.ProcessWorkItemRequest();
            pwr.setWorkitemId(w.id);
            pwr.setAction('Removed');
            Approval.ProcessResult pr = Approval.process(pwr);
            }
}

davidesidavidesi

Anybody has information about how to solve this problem?

 

I also would be very interested in receive any tip about how is de approval workflow . Because the information that I have reached is not enough for me.