Sunday, August 28, 2016

Working with SOQL Aggregate Functions


Working with SOQL Aggregate Functions


  if(remediationCases.isEmpty()) { return; }  
     //Aggregate query for the remediation cases to get the client file count  
     for (AggregateResult aggRes : [  
       SELECT  
       COUNT(Id) crCount,  
       RemediationCase__c  
       FROM ClientFile__c  
       WHERE RemediationCase__c =: remediationCases.keySet()  
       GROUP BY RemediationCase__c  
     ]) {  
       //get the case record to bind the error  
       RemediationCase__c rc = remediationCases.get((Id) aggRes.get('RemediationCase__c'));  
       // read client file count parameter  
       Integer numApps = (Integer) aggRes.get('crCount');  
       //validate number of client files for the case, if more than one client file found for the remediation case  
       //prevent deletion  
       if(numApps > 0 ) {  
         handleError(rc, 'You are uable to delete a remediation case that has client files registered against it');  
       }        
     }  
     //this is option 2  
     /*for (RemediationCase__c rc : remediationCases) {  
       if(rc.numberOfClientFiles__c > 0 ) {  
         handleError(rc, 'You are uable to delete a remediation case that has client files registered against it');  
       }  
     }*/  
   }  

No comments:

Post a Comment