• Jason Ciemiega
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
  • I am trying to find my way to wrap my head around this problem.  I am trying to eliminate 3 of these for AggregateResult loops and just make one since it is getting the same object.  I am a new Salesforce Developer and I am trying to figure out what will be the most efficient way to do this.
  • If anyone has an input on this I'd definitely love to hear it.

AggregateResult[] asgns = [SELECT LP_SAS__c, SUM(Exposure_Amt__c)total FROM CC_ASGN__c WHERE LP_SAS__c IN :sasIds AND Status__c = 'Assigned' GROUP by LP_SAS__c]; 

AggregateResult[] asgnsres = [SELECT LP_SAS__c, SUM(Reserve_Amount__c)total, SUM(Discount_Unearned__c)total1, SUM(Exposure_Amt__c)total2 FROM CC_ASGN__c WHERE LP_SAS__c IN :sasIds GROUP by LP_SAS__c];

AggregateResult[] asgnsbb = [SELECT LP_SAS__c, SUM(Amount_Deducted_from_Reserve__c)total, SUM(Buyback_DE__c)total1, SUM(Exposure_Amt__c)total2 FROM CC_ASGN__c WHERE LP_SAS__c IN :sasIds AND Status__c = 'Buyback' GROUP by LP_SAS__c];

AggregateResult[] asgnswo = [SELECT LP_SAS__c, SUM(Amount_Deducted_from_Reserve__c)total, SUM(Buyback_DE__c)total1, SUM(Exposure_Amt__c)total2 FROM CC_ASGN__c WHERE LP_SAS__c IN :sasIds AND Status__c = 'Write-off' GROUP by LP_SAS__c];
  • Then it goes as follows

for (AggregateResult ar : asgns) {           
            updateSass.add(new LP_SAS__c(Id = (ID)ar.get('LP_SAS__c'), Exposure_Total__c = (Decimal)ar.get('total')));
            system.debug('ar ' + ar);
        }
        
        if (updateSass.Size()>0){
            update updateSass;
        }
        
for (AggregateResult ares : asgnsres) {           
            updateSassres.add(new LP_SAS__c(Id = (ID)ares.get('LP_SAS__c'), Reserve_ASGN__c = (Decimal)ares.get('total'),                                         Unearned_Discount_ASGN__c = (Decimal)ares.get('total1'), Exposure_Total__c = (Decimal)ares.get('total2')));
         
        }
        
        if (updateSassres.Size()>0){
            update updateSassres;
        }
        
for (AggregateResult bb : asgnsbb) {
            updateSassbb.add(new LP_SAS__c(Id = (id)bb.get('LP_SAS__c'), Reserve_Buyback__c = (Decimal)bb.get('total'),                                             Unearned_Discount_Buyback__c = (Decimal)bb.get('total1'), Exposure_Total__c = (Decimal)bb.get('total2')));
        }
        
        if (updateSassbb.Size()>0){
            update updateSassbb;
        }
        
for (AggregateResult wo : asgnswo) {
            updateSasswo.add(new LP_SAS__c(Id = (id)wo.get('LP_SAS__c'), Reserve_Write_off__c = (Decimal)wo.get('total'),                                           Unearned_Discount_Write_off__c = (Decimal)wo.get('total1'), Exposure_Total__c = (Decimal)wo.get('total2')));         
        }
        
        if (updateSasswo.Size()>0){
            update updateSasswo;
        }


 
  • I am trying to find my way to wrap my head around this problem.  I am trying to eliminate 3 of these for AggregateResult loops and just make one since it is getting the same object.  I am a new Salesforce Developer and I am trying to figure out what will be the most efficient way to do this.
  • If anyone has an input on this I'd definitely love to hear it.

AggregateResult[] asgns = [SELECT LP_SAS__c, SUM(Exposure_Amt__c)total FROM CC_ASGN__c WHERE LP_SAS__c IN :sasIds AND Status__c = 'Assigned' GROUP by LP_SAS__c]; 

AggregateResult[] asgnsres = [SELECT LP_SAS__c, SUM(Reserve_Amount__c)total, SUM(Discount_Unearned__c)total1, SUM(Exposure_Amt__c)total2 FROM CC_ASGN__c WHERE LP_SAS__c IN :sasIds GROUP by LP_SAS__c];

AggregateResult[] asgnsbb = [SELECT LP_SAS__c, SUM(Amount_Deducted_from_Reserve__c)total, SUM(Buyback_DE__c)total1, SUM(Exposure_Amt__c)total2 FROM CC_ASGN__c WHERE LP_SAS__c IN :sasIds AND Status__c = 'Buyback' GROUP by LP_SAS__c];

AggregateResult[] asgnswo = [SELECT LP_SAS__c, SUM(Amount_Deducted_from_Reserve__c)total, SUM(Buyback_DE__c)total1, SUM(Exposure_Amt__c)total2 FROM CC_ASGN__c WHERE LP_SAS__c IN :sasIds AND Status__c = 'Write-off' GROUP by LP_SAS__c];
  • Then it goes as follows

for (AggregateResult ar : asgns) {           
            updateSass.add(new LP_SAS__c(Id = (ID)ar.get('LP_SAS__c'), Exposure_Total__c = (Decimal)ar.get('total')));
            system.debug('ar ' + ar);
        }
        
        if (updateSass.Size()>0){
            update updateSass;
        }
        
for (AggregateResult ares : asgnsres) {           
            updateSassres.add(new LP_SAS__c(Id = (ID)ares.get('LP_SAS__c'), Reserve_ASGN__c = (Decimal)ares.get('total'),                                         Unearned_Discount_ASGN__c = (Decimal)ares.get('total1'), Exposure_Total__c = (Decimal)ares.get('total2')));
         
        }
        
        if (updateSassres.Size()>0){
            update updateSassres;
        }
        
for (AggregateResult bb : asgnsbb) {
            updateSassbb.add(new LP_SAS__c(Id = (id)bb.get('LP_SAS__c'), Reserve_Buyback__c = (Decimal)bb.get('total'),                                             Unearned_Discount_Buyback__c = (Decimal)bb.get('total1'), Exposure_Total__c = (Decimal)bb.get('total2')));
        }
        
        if (updateSassbb.Size()>0){
            update updateSassbb;
        }
        
for (AggregateResult wo : asgnswo) {
            updateSasswo.add(new LP_SAS__c(Id = (id)wo.get('LP_SAS__c'), Reserve_Write_off__c = (Decimal)wo.get('total'),                                           Unearned_Discount_Write_off__c = (Decimal)wo.get('total1'), Exposure_Total__c = (Decimal)wo.get('total2')));         
        }
        
        if (updateSasswo.Size()>0){
            update updateSasswo;
        }