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
carmilyn.c martincarmilyn.c martin 

batch class approvasl

Hello, 

I need to create a batch class that will schedule to run every Friday that will automatically approve Items that are pending from Rep's Approval so that it will proceed to the next level of approver which is their managers, my code is not working here is my code: 

global class PTMS_autoApproveRepBatchable implements Database.batchable<sObject>, Database.Stateful {

    global Iterable<sObject> start(Database.BatchableContext info) {
        return Database.getQueryLocator(
            [Select Id, Auto_Approval__c FROM Professional_Target_List_Line_Item_esi__c WHERE Rep_Approval__c = True AND Proposed_Rep_Approval_Status__c = 'Pending' AND Id = 'a203F000000H0noQAC']);
    }

    global void execute(Database.BatchableContext info, List<Professional_Target_List_Line_Item_esi__c> pendPTLI){
        system.debug('Ming'+ pendPTLI.size());
        Set<Id> pendingRepPTLIset = new Set<Id>();
        List<Professional_Target_List_Line_Item_esi__c> ptliToUpdate = new List<Professional_Target_List_Line_Item_esi__c>();
            
        for(Professional_Target_List_Line_Item_esi__c pendingRepPTLI : pendPTLI){
                pendingRepPTLI.Auto_Approval__c = True;
                ptliToUpdate.add(pendingRepPTLI);
                pendingRepPTLIset.add(pendingRepPTLI.Id); 
                system.debug('Ming2'+ pendingRepPTLIset);
        }
                try {
                    update ptliToUpdate;
                    }
                    
                catch(DmlException e){
                    System.debug('exception caught :' + e.getMessage());    
                    }
        
                Approval.ProcessWorkitemRequest[] prWkItems = New Approval.ProcessWorkItemRequest[]{};
                ProcessInstance[] pi = [Select ID, Status, TargetObject.Name,
                (SELECT Id, ActorId, ProcessInstanceId FROM Workitems),
                (SELECT Id, StepStatus, Comments FROM Steps) From ProcessInstance
                Where TargetObjectID IN :pendingRepPTLIset AND Status = 'Pending'];
                
                for(ProcessInstance instance : pi){
                    for(ProcessInstanceWorkItem workItem : instance.WorkItems){
                        Approval.ProcessWorkitemRequest prWkItem = new Approval.ProcessWorkitemRequest();
                        prWkItem.setWorkItemID(workItem.id);
                        prWkItem.setAction('Approve');
                        prWkItems.add(prWkItem);
                    }
                }         
            
    }
    global void finish(Database.BatchableContext info){   
    }
  
}
so_mayankso_mayank
Hello,

Could you share the details of the error/issue you are getting?
carmilyn.c martincarmilyn.c martin
Hello So_Mayank,

I did not receive any error message when I saved the Apex and when I ran the Batch Class, the problem is it updated the record setting the field Auto-Approval to True but it did not approve the record. It remains pending for Approval from a specific Approver (user). Am I missing a step?