You need to sign in to do that
Don't have an account?
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.
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:
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.
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.
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
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.