+ Start a Discussion
Sourav PSourav P 

Trigger not getting fired for bulk update for all records

Hi, Bulk update is done on a custom object via supported application (Heroku). On check of flag in this custom object, there is a after update trigger that gets fired to auto approve the record.
When we update 100 records the trigger is fired only for 5 records.
what can be the issue ?
Raj VakatiRaj Vakati
Can you share code ?
Sourav PSourav P
Hi Raj , sure, plz find below
After Update trigger :

Payment_Details__c Newpaymentdetails = trigger.new[0];                
		Payment_Details__c Oldpaymentdetails = trigger.old[0];
        System.debug('Oldpaymentdetails mark paid value :: '+Oldpaymentdetails.Mark_Paid__c);
        System.debug('Newpaymentdetails mark paid value :: '+Newpaymentdetails.Mark_Paid__c);
        if(Oldpaymentdetails.Mark_Paid__c != Newpaymentdetails.Mark_Paid__c && Newpaymentdetails.Mark_Paid__c)
                    ClaimDatahandler.approveRecord(Newpaymentdetails.id);

-----------------------------------------------------------------------------------------------------------
 

Details : Payment_details__c is custome object with checkbox field Mark_Paid__c 
 
when Mark_Paid__c is true, below code is executed
 
Code Executed :
public Static Void approveRecord(String targetId){        
        ProcessInstanceWorkitem workItem;
        List<ProcessInstanceWorkitem> workItems = [SELECT Id, ProcessInstanceId,ProcessInstance.status FROM ProcessInstanceWorkitem WHERE ProcessInstance.TargetObjectId =: targetId and ProcessInstance.status = 'Pending' and actor.name = 'Pittiporn Sriphiphat'];
        Approval.ProcessWorkitemRequest req;
        
        if(workItems != null && !workItems.isEmpty()){
            workItem = workItems.get(0);
            req = new Approval.ProcessWorkitemRequest();
            req.setWorkitemId(workItem.Id);  
            req.setAction('Approve');
            req.setComments('Approved as per request from KPI. Already approved in IIA');  
                    
            Approval.ProcessResult processResults = Approval.process(req);
        }               
    }

 
Raj VakatiRaj Vakati
try this code
 
List<Payment_Details__c> Newpaymentdetails = trigger.new;                
		List<Payment_Details__c> Oldpaymentdetails = trigger.old ; 
		Map<Id , Payment_Details__c> oldMapOfPayments  = trigger.oldMap ; 
		
        System.debug('Oldpaymentdetails mark paid value :: '+Oldpaymentdetails.Mark_Paid__c);
        System.debug('Newpaymentdetails mark paid value :: '+Newpaymentdetails.Mark_Paid__c);
		Set<Id> payIds = new Set<Id>() ; 
	for(Payment_Details__c p : 	Newpaymentdetails){
		if(oldMapOfPayments.get(p.Id).Mark_Paid__c!=p.Mark_Paid__c && Newpaymentdetails.Mark_Paid__c){
			payIds.add(p.Id) ; 
		}
		
	}
       
                    ClaimDatahandler.approveRecord(payIds);
 
public Static Void approveRecord(Set<Id> targetId){        
        ProcessInstanceWorkitem workItem;
        List<ProcessInstanceWorkitem> workItems = [SELECT Id, ProcessInstanceId,ProcessInstance.status FROM ProcessInstanceWorkitem 
		WHERE ProcessInstance.TargetObjectId IN  :targetId and ProcessInstance.status = 'Pending' and actor.name = 'Pittiporn Sriphiphat'];
        Approval.ProcessWorkitemRequest req;
        
        if(workItems != null && !workItems.isEmpty()){
            workItem = workItems.get(0);
            req = new Approval.ProcessWorkitemRequest();
            req.setWorkitemId(workItem.Id);  
            req.setAction('Approve');
            req.setComments('Approved as per request from KPI. Already approved in IIA');  
                    
            Approval.ProcessResult processResults = Approval.process(req);
        }               
    }