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
ZANE PRATERZANE PRATER 

Database.SaveResult[] not working for bulk updates. I am trying to store the saved results so that each record is handled appropriately but what is happening is when one record fails then all the rest fail to update in that batch.

Here is the code and trigger works fine with a single update but fails the whole batch when one fails.
 
trigger AgreementTrigger on Apttus__APTS_Agreement__c (after update, before update) {
    
    set<Id> agmts = new set<Id>();
    set<Id> agmts2 = new set<Id>();
    set<Id> pbuns = new set<Id>();
    
    List<Product_Bundle__c > pbs = [Select Id From Product_Bundle__c Where Bundle_Type__c = 'Service Pack'];
    
    For(Product_Bundle__c pb : pbs)
        
        If(pbs.size() > 0) {
            
            pbuns.add(pb.Id);
            
            system.debug('^^^^^^^^^^^^^^'+pbuns);
            
        }
    List<Apttus__AgreementLineItem__c> lines = new list<Apttus__AgreementLineItem__c>();
    List<Apttus__AgreementLineItem__c> lines2 = new list<Apttus__AgreementLineItem__c>();
    List<Apttus__AgreementLineItem__c> lines3 = new list<Apttus__AgreementLineItem__c>();        
    
    if(trigger.isUpdate){
        if(trigger.isAfter){
            AgreementTriggerClass.onAfterUpdate(trigger.newMap);
        }
        else if(trigger.isBefore){
            for(Apttus__APTS_Agreement__c aptusap : trigger.New){
                if(aptusap.Auto_Renewal_cust__c == 'Yes')
                    aptusap.Apttus__Auto_Renewal__c = True;
                if(aptusap.Auto_Renewal_cust__c == 'No')
                    aptusap.Apttus__Auto_Renewal__c = False;
                
                if(aptusap.Apttus__Status__c == 'Ready for Signatures' || aptusap.Apttus__Status__c == 'Activated') {
                    
                    agmts.add(aptusap.Id);  
                    
                    system.debug('###############'+agmts);
                    
                } 
                
                if(aptusap.Apttus__Status__c == 'Activated') {
                    
                    agmts2.add(aptusap.Id); 
                } 
            }
            lines = [Select Id From Apttus__AgreementLineItem__c Where Apttus__AgreementId__c In: agmts AND Product_Bundle__r.Product_Bundle__c
                     In: pbuns]; 
            
            If(lines.size()>0) {
                
                lines2 =[Select Id From Apttus__AgreementLineItem__c Where Apttus__AgreementId__c In: agmts];
                
            }
            
            lines3=[Select Id, POB__c, GL_String__c From Apttus__AgreementLineItem__c Where
                    Apttus__AgreementId__c In:agmts2 AND Apttus__AgreementId__r.Rev_Customer__c = true];
            
        }
        
        system.debug('$$$$$$$$$$$$$$'+lines);
        
        For(Apttus__APTS_Agreement__c apt: trigger.new) {  
            For(Apttus__AgreementLineItem__c lis:lines2) {
                If(lines2.size()<2) {
                    
                    system.debug('***********'+lines.size());
                    
                    apt.addError('Agreement needs to have more than one agreement line when there is a service pack.'); 
                }
                
                
                
            }
        }
        //logic for SFCON-880
        list<Apttus__APTS_Agreement__c> lstNewAgt = Trigger.new;
        list<Apttus__APTS_Agreement__c> lstOldAgt = Trigger.old;
        Map<Id, Apttus__APTS_Agreement__c> mapOld = new Map<Id, Apttus__APTS_Agreement__c>();
        Set<Id> agtid = new Set<Id>();
        For(Apttus__APTS_Agreement__c old:trigger.old) {
            
            mapOld.put(old.Id, old);
        }
        
        For(Apttus__APTS_Agreement__c apt: trigger.new) {
            //mapNew.put(apt.Id, apt);
            For(Apttus__AgreementLineItem__c lis:lines3) {
                If((lines3.size()>0) && (lis.POB__c==NULL)) {
                    If(apt.Apttus__Status__c != mapOld.get(apt.Id).Apttus__Status__c) {
                        system.debug('@@@@@@OLD MAP ' +mapOld.get(apt.Id).Apttus__Status__c);
                        system.debug('@@@@@@NEW MAP ' +apt.Apttus__Status__c);
                        agtid.add(apt.id);
                        apt.addError('Every Agreement Line Item should have a POB populated.');
                        
                        system.debug('%%%%%%%%%%%%'+lines3);
                    }
                }
            }
        }
        //store the result for each record
        List<Apttus__APTS_Agreement__c> ag2 = [Select Id From Apttus__APTS_Agreement__c Where Id In: agtid];
        Database.SaveResult[] srList = Database.update(ag2, false);
        
        // Iterate through each returned result and all allow partial processing of records on failure
        for (Database.SaveResult sr : srList) {
            if (sr.isSuccess()) {
                // Operation was successful, so get the ID of the record that was processed
                System.debug('Successfully inserted account. Agreement ID: ' + sr.getId());
            }
            else {
                // Operation failed, so get all errors                
                for(Database.Error err : sr.getErrors()) {
                    System.debug('The following error has occurred.');                    
                    System.debug(err.getStatusCode() + ': ' + err.getMessage());
                    System.debug('Agreement fields that affected this error: ' + err.getFields());
                }
            }
        }
    }
}

 
ZANE PRATERZANE PRATER
The issue pertains from line 75. Here is a snippet of the errorlog and the highlighted is the only one the validation should apply to as the others are valid and should have updated.

User-added image
Raj VakatiRaj Vakati
Looks like you have a data issues ..its not issue with the  Database.SaveResult[]  


In your code you are throwing an error based on conditions on line no 93 


                        apt.addError('Every Agreement Line Item should have a POB populated.');

 
ZANE PRATERZANE PRATER
Thank you Raj but I did look at the debug log for where I am am capturing the saveResult and see this error: SELF_REFERENCE_FROM_TRIGGER: is currently in trigger AgreementTrigger, therefore it cannot recursively update itself.  Any idea as to why I am getting this?
ZANE PRATERZANE PRATER
I found the SaveResult code was causing the SELF REFERENCE ERROR.  The data I am testing with is correct but cannot get it to iterate through each record rather than rolling back.
Raj VakatiRaj Vakati
Ok ..Still issue ???
ZANE PRATERZANE PRATER
I found my issue was something so dumb.  I needed to add a condition to where the lineitem parent id was equal to the agreement id.  

Thus, For(Apttus__APTS_Agreement__c apt: trigger.new) {
            //mapNew.put(apt.Id, apt);
            For(Apttus__AgreementLineItem__c lis:lines3) {
                If((lines3.size()>0) && (lis.POB__c==NULL)) {
                    If(apt.Apttus__Status__c != mapOld.get(apt.Id).Apttus__Status__c && apt.ID == lis.Apttus__AgreementId__c) and now of course it works.  Thanks for looking into it.