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
Todd B.Todd B. 

Cleaning up My Code

Hello All, 

I am new to writing Apex code and was able to wrtite the following piece to calculate both Divisional and Individual Sales goals.
 
public with sharing class FinancialPlanningTriggerHandle {
/* 
  Developer     : Todd Barry
  Created       : 05/27/2014
  Last Modified : 
  Test Class    : 
  Objective     : 
  
*/      
    public void beforeUpdate(Financial_Planning__c[] newlist){
        List<Date> dtStart =  new List<Date>();
        List<Date> dtEnd =  new List<Date>();
        List<String> strKey =  new List<String>();
        String strGT ;
        Double fpAmount = 0 ;
        String strname;
        
            for(Financial_Planning__c fp : newList){
              StrGT = fp.GoalType__c;
              dtStart.add(fp.period_start_date__c); 
              dtEnd.add(fp.period_End_date__c);
              strKey.add(fp.rptKey__c); 
              strname = fp.period_start_date__c.year() +' - ' +fp.division__c + ' - (' + fp.Revenue_Type__c + ')';
            }
                System.debug(strKey);
                System.debug(dtStart);
                System.debug(dtEnd);
                System.debug(strName);
                
        //Determine Which Recordtype you are working with
        if(strGT == 'Divisional Goal'){
             List<aggregateResult> fpresults = [select Sum(Incremental_Annualized_Revenue__c) Total from Opportunity 
                                                 where IsWon=True
                                                 And Contract_Start_Date__c >= :dtStart
                                                 And Contract_Start_Date__c <= :dtEnd
                                                 And rptFinancialKey_div__c = :strKey];
                                                
        
            for (AggregateResult ar : fpresults )  {
                fpAmount = (Double)ar.get('Total');
            }
        
            for(Financial_Planning__c fp : newList){
                fp.Realized_Revenue_Actual__c =  fpamount;
                fp.name = strname; 
            }   
        }
        if(strGT == 'Personal Goal'){
             List<aggregateResult> fpresults = [select Sum(Incremental_Annualized_Revenue__c) Total from Opportunity 
                                                 where IsWon=True
                                                 And Contract_Start_Date__c >= :dtStart
                                                 And Contract_Start_Date__c <= :dtEnd
                                                 And rptFinancialKey_User__c = :strKey];
                                                
        
            for (AggregateResult ar : fpresults )  {
                fpAmount = (Double)ar.get('Total');
            }
        
            for(Financial_Planning__c fp : newList){
                fp.Realized_Revenue_Actual__c =  fpamount;
                fp.name = strname; 
            }   
        }        
        
        
        
        
        }
    }

What I can not figure out is how to streamline some of the code.  I want to take this section and combined it into one IF statement:
//Determine Which Recordtype you are working with
        if(strGT == 'Divisional Goal'){
             List<aggregateResult> fpresults = [select Sum(Incremental_Annualized_Revenue__c) Total from Opportunity 
                                                 where IsWon=True
                                                 And Contract_Start_Date__c >= :dtStart
                                                 And Contract_Start_Date__c <= :dtEnd
                                                 And rptFinancialKey_div__c = :strKey];
                                                
        
            for (AggregateResult ar : fpresults )  {
                fpAmount = (Double)ar.get('Total');
            }
        
            for(Financial_Planning__c fp : newList){
                fp.Realized_Revenue_Actual__c =  fpamount;
                fp.name = strname; 
            }   
        }
        if(strGT == 'Personal Goal'){
             List<aggregateResult> fpresults = [select Sum(Incremental_Annualized_Revenue__c) Total from Opportunity 
                                                 where IsWon=True
                                                 And Contract_Start_Date__c >= :dtStart
                                                 And Contract_Start_Date__c <= :dtEnd
                                                 And rptFinancialKey_User__c = :strKey];
                                                
        
            for (AggregateResult ar : fpresults )  {
                fpAmount = (Double)ar.get('Total');
            }
        
            for(Financial_Planning__c fp : newList){
                fp.Realized_Revenue_Actual__c =  fpamount;
                fp.name = strname; 
            }   
        }

Basically, I want something like this:  

  if(strGT == 'Divisional Goal'){
      String A = select Sum(Incremental_Annualized_Revenue__c) Total from Opportunity 
                                                 where IsWon=True
                                                 And Contract_Start_Date__c >= :dtStart
                                                 And Contract_Start_Date__c <= :dtEnd
                                                 And rptFinancialKey_div__c = :strKey];
Else string A = select Sum(Incremental_Annualized_Revenue__c) Total from Opportunity 
                                                 where IsWon=True
                                                 And Contract_Start_Date__c >= :dtStart
                                                 And Contract_Start_Date__c <= :dtEnd
                                                 And rptFinancialKey_User__c = :strKey];
             
Then 

List<aggregateResult> fpresults = String A

But I am not sure how to do it.  Any ideas?

Regards, 

Todd B.
Best Answer chosen by Todd B.
Pankaj_GanwaniPankaj_Ganwani
Hi Todd,


You can use ternary operator(?:) to optimize the code:

List<aggregateResult> fpresults = (strGT == 'Divisional Goal' ? [select Sum(Incremental_Annualized_Revenue__c) Total from Opportunity                                                                                                         where IsWon=True
                                                                                           And Contract_Start_Date__c >= :dtStart
                                                                                         And Contract_Start_Date__c <= :dtEnd
                                                                                         And rptFinancialKey_div__c = :strKey]
                                                                                       :
                                                                                        [select Sum(Incremental_Annualized_Revenue__c) Total from Opportunity                                                                                                          where IsWon=True
                                                                                        And Contract_Start_Date__c >= :dtStart
                                                                                        And Contract_Start_Date__c <= :dtEnd
                                                                                      And rptFinancialKey_User__c = :strKey]);

Please let me know if this helps you out.
 

All Answers

KaranrajKaranraj
Hi Todd - I'm just wondering why you need if conditions, because you are doing same logic in both if conditions or you pasted wrong here? I don't find any difference . The line number 32-46 and 49-63 are look same.
Pankaj_GanwaniPankaj_Ganwani
Hi Todd,


You can use ternary operator(?:) to optimize the code:

List<aggregateResult> fpresults = (strGT == 'Divisional Goal' ? [select Sum(Incremental_Annualized_Revenue__c) Total from Opportunity                                                                                                         where IsWon=True
                                                                                           And Contract_Start_Date__c >= :dtStart
                                                                                         And Contract_Start_Date__c <= :dtEnd
                                                                                         And rptFinancialKey_div__c = :strKey]
                                                                                       :
                                                                                        [select Sum(Incremental_Annualized_Revenue__c) Total from Opportunity                                                                                                          where IsWon=True
                                                                                        And Contract_Start_Date__c >= :dtStart
                                                                                        And Contract_Start_Date__c <= :dtEnd
                                                                                      And rptFinancialKey_User__c = :strKey]);

Please let me know if this helps you out.
 
This was selected as the best answer
David ZhuDavid Zhu
boolean updateFP = false;

if(strGT == 'Divisional Goal'){
    fpamount =(Double)([select Sum(Incremental_Annualized_Revenue__c) from Opportunity                                              where IsWon=True
                                                 And Contract_Start_Date__c >= :dtStart
                                                 And Contract_Start_Date__c <= :dtEnd
                                                 And rptFinancialKey_div__c = :strKey][0].get('expr0'));
    updateFP = true;        

 }
        
if(strGT == 'Personal Goal'){
    fpamount =(Double)([select Sum(Incremental_Annualized_Revenue__c)from Opportunity                                              where IsWon=True
                                                 And Contract_Start_Date__c >= :dtStart
                                                 And Contract_Start_Date__c <= :dtEnd
                                                 And And rptFinancialKey_User__c = :strKey][0].get('expr0'));
    updateFP = true;                
}

if (updateFP)
{
    for(Financial_Planning__c fp : newList){
          fp.Realized_Revenue_Actual__c =  fpamount;
          fp.name = strname; 
    }   
}
Todd B.Todd B.
Thanks for all the replies!  I implemented @Pankaj Ganwani 3 solution, but am going to test @David Zhu also,