+ Start a Discussion
SeanCenoSeanCeno 

Prior 12 Months Formula in Apex

Hey guys,

I'm trying to do a date range to grab all trades from past 12 months, from yesterday's date. For some reason the following 2 formulas will not work. Any help would be greatly appreciated!

Returns nothing:
if(trade.trade_date__c < date.today().addDays(-1) && trade.trade_date__c > date.today().addDays(-366)) {
                            accounts.get(trade.Resolved_Firm_Trading_ID__c).put(prior12MOS.get(trade.fund_number__c)[0], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(prior12MOS.get(trade.fund_number__c)[0]))+trade.Dollar_Amount_of_The_Transaction__c);
                            accounts.get(trade.Resolved_Firm_Trading_ID__c).put(prior12MOS.get(trade.fund_number__c)[1], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(prior12MOS.get(trade.fund_number__c)[1]))+trade.Number_of_Shares_of_the_transaction__c);
                            system.debug(prior12MOS);
                            system.debug(LoggingLevel.DEBUG, + Limits.getCpuTime() + '/' + Limits.getLimitCpuTime());

Returns only dates within the past month:
if(trade.trade_date__c < date.today().addDays(-1) && trade.trade_date__c > date.today().addYears(-1)) {
                            

 
Best Answer chosen by SeanCeno
SeanCenoSeanCeno
If anybody needs this, here is how to setup the condition properly to have a Trailing 12 Months.
 
        //Define as variables then write as boolean
        Date maxtt = Date.today().addDays(-1);   // trailing twelve upper bound
        Date mintt = Date.today().addDays(-365); // trailing twelve lower bound

        Boolean isTT = maxtt > trade.Trade_Date__c && mintt < trade.Trade_Date__c;

 

All Answers

Leslie  KismartoniLeslie Kismartoni
Can you verify that trade.trade_date__c is a valid date? 
SeanCenoSeanCeno
Yes, it is. This is preexisting code and I already use this field to calculate YTD, MTD, QTD, etc. Sent from my HTC
SeanCenoSeanCeno
If anybody needs this, here is how to setup the condition properly to have a Trailing 12 Months.
 
        //Define as variables then write as boolean
        Date maxtt = Date.today().addDays(-1);   // trailing twelve upper bound
        Date mintt = Date.today().addDays(-365); // trailing twelve lower bound

        Boolean isTT = maxtt > trade.Trade_Date__c && mintt < trade.Trade_Date__c;

 
This was selected as the best answer