-1

All,

**EDIT: Sorry for being too vague. I'm trying to add another field to my sales summary that displays on Account pages. I'm trying to grab the trades that fall within a date range of the past 12 months and have the total resolve to my field Prior 12 Months.

With the following formula, the field will only display totals if the date falls within a Month To Date range from today:

if(trade.trade_date__c < date.today().addDays(-1) && trade.trade_date__c > date.today().addDays(-366))

And this formula returns null:

if(trade.trade_date__c < date.today().addDays(-1) && trade.trade_date__c > date.today().addYears(-1)) {

Here is the full code. The condition I'm trying to define is at line 101:

    public Static Account_Setting__c setting = Account_Setting__c.getInstance();
public Static boolean inprog = false;

public static void execute (Set<Id> accountIds, List<Account> accountsList) {
    Map<Id, Account> accounts = new Map<Id, Account> (AccountsList);
    //system.debug ('execute');
    if(setting.Disable_RollupTrades__c != true) {
        //Map<Id, Account> accounts = new Map<Id, Account>();
        for(Id accountId:accountIds) {
            //system.debug(accountid);
            accounts.put(accountId,
               new Account(
                   Id=accountId,
                   /**YTD_NIOR_I_Sales__c = 0,       YTD_NIOR_I_Shares__c = 0,           QTD_NIOR_I_Sales__c = 0,                QTD_NIOR_I_Shares__c = 0,
                   MTD_NIOR_I_Sales__c = 0,          MTD_NIOR_I_Shares__c = 0,           PY_NIOR_I_Sales__c = 0,                 PY_NIOR_I_Shares__c = 0,
                   Total_NIOR_I_Sales__c = 0,        Total_NIOR_I_Shares__c = 0,         YTD_NS_Income_Sales__c = 0,             YTD_NS_Income_Shares__c = 0,
                   QTD_NS_Income_Sales__c = 0,       QTD_NS_Income_Shares__c = 0,        MTD_NS_Income_Sales__c = 0,             MTD_NS_Income_Shares__c = 0,
                   PY_NS_Income_Sales__c = 0,        PY_NS_Income_Shares__c = 0,         Total_NS_Income_Sales__c = 0,           Total_NS_Income_Shares__c = 0,
                   Prior_12_MOS_NIOR_I_Sales__c = 0, Prior_12_MOS_NIOR_I_Shares__c = 0,  Prior_12_MOS_NS_Income_Sales__c = 0,    Prior_12_MOS_NS_Income_Shares__c = 0 **/
                   Total_NS_HI_Sales__c = 0,         Total_NS_HI_Shares__c = 0,          YTD_NS_HI_Sales__c = 0,                 YTD_NS_HI_Shares__c = 0,
                   QTD_NS_HI_Sales__c = 0,           QTD_NS_HI_Shares__c = 0,            MTD_NS_HI_Sales__c = 0,                 MTD_NS_HI_Shares__c = 0,
                   PY_NS_HI_Sales__c = 0,            PY_NS_HI_Shares__c = 0,             Prior_12_MOS_NS_HI_Sales__c = 0,        Prior_12_MOS_NS_HI_Shares__c = 0,
                   Total_NS_Income_II_Sales__c = 0,  Total_NS_Income_II_Shares__c = 0,   YTD_NS_Income_II_Sales__c = 0,          YTD_NS_Income_II_Shares__c = 0,
                   QTD_NS_Income_II_Sales__c = 0,    QTD_NS_Income_II_Shares__c = 0,     MTD_NS_Income_II_Sales__c = 0,          MTD_NS_Income_II_Shares__c = 0,
                   PY_NS_Income_II_Sales__c = 0,     PY_NS_Income_II_Shares__c = 0,      Prior_12_MOS_NS_Income_II_Sales__c = 0, Prior_12_MOS_NS_Income_II_Shares__c = 0,
                   Total_NS_RXR_Sales__c = 0,        Total_NS_RXR_Shares__c = 0,         YTD_NS_RXR_Sales__c = 0,                YTD_NS_RXR_Shares__c = 0,
                   QTD_NS_RXR_Sales__c = 0,          QTD_NS_RXR_Shares__c = 0,           MTD_NS_RXR_Sales__c = 0,                MTD_NS_RXR_Shares__c = 0,
                   PY_NS_RXR_Sales__c = 0,           PY_NS_RXR_Shares__c = 0,            Prior_12_MOS_RXR_Sales__c = 0,          Prior_12_MOS_RXR_Shares__c = 0,
                   Rollup_Trades__c = DateTime.now()
               )
                        );
        }
        Map<String, SObjectField[]>
            ytd = new map<string, sobjectfield[]> {
                //'3910' => new sobjectfield[] { account.YTD_NIOR_I_Sales__c , account.YTD_NIOR_I_Shares__c},
                //'3911' => new sobjectfield[] { account.YTD_NS_Income_Sales__c , account.YTD_NS_Income_Shares__c },
                '3912' => new sobjectfield[] { account.YTD_NS_HI_Sales__c , account.YTD_NS_HI_Shares__c },
                '3915' => new sobjectfield[] { account.YTD_NS_Income_II_Sales__c , account.YTD_NS_Income_II_Shares__c },
                '3960' => new sobjectfield[] { account.YTD_NS_RXR_Sales__c, account.YTD_NS_RXR_Shares__c}
            },
            qtd = new map<string, sobjectfield[]> {
                //'3910' => new sobjectfield[] { account.QTD_NIOR_I_Sales__c , account.QTD_NIOR_I_Shares__c},
                //'3911' => new sobjectfield[] { account.QTD_NS_Income_Sales__c , account.QTD_NS_Income_Shares__c },
                '3912' => new sobjectfield[] { account.QTD_NS_HI_Sales__c , account.QTD_NS_HI_Shares__c },
                '3915' => new sobjectfield[] { account.QTD_NS_Income_II_Sales__c , account.QTD_NS_Income_II_Shares__c },
                '3960' => new sobjectfield[] { account.QTD_NS_RXR_Sales__c, account.QTD_NS_RXR_Shares__c}
            },
            mtd = new map<string, sobjectfield[]> {
                //'3910' => new sobjectfield[] { account.MTD_NIOR_I_Sales__c , account.MTD_NIOR_I_Shares__c},
                //'3911' => new sobjectfield[] { account.MTD_NS_Income_Sales__c , account.MTD_NS_Income_Shares__c },
                '3912' => new sobjectfield[] { account.MTD_NS_HI_Sales__c , account.MTD_NS_HI_Shares__c },
                '3915' => new sobjectfield[] { account.MTD_NS_Income_II_Sales__c , account.MTD_NS_Income_II_Shares__c },
                '3960' => new sobjectfield[] { account.MTD_NS_RXR_Sales__c, account.MTD_NS_RXR_Shares__c}
            },
            py = new map<string, sobjectfield[]> {
                //'3910' => new sobjectfield[] { account.PY_NIOR_I_Sales__c , account.PY_NIOR_I_Shares__c},
                //'3911' => new sobjectfield[] { account.PY_NS_Income_Sales__c , account.PY_NS_Income_Shares__c },
                '3912' => new sobjectfield[] { account.PY_NS_HI_Sales__c , account.PY_NS_HI_Shares__c },
                '3915' => new sobjectfield[] { account.PY_NS_Income_II_Sales__c , account.PY_NS_Income_II_Shares__c },
                '3960' => new sobjectfield[] { account.PY_NS_RXR_Sales__c, account.PY_NS_RXR_Shares__c}
            },
            total = new map<string, sobjectfield[]> {
                //'3910' => new sobjectfield[] { account.Total_NIOR_I_Sales__c , account.Total_NIOR_I_Shares__c},
                //'3911' => new sobjectfield[] { account.Total_NS_Income_Sales__c , account.Total_NS_Income_Shares__c },
                '3912' => new sobjectfield[] { account.Total_NS_HI_Sales__c , account.Total_NS_HI_Shares__c },
                '3915' => new sobjectfield[] { account.Total_NS_Income_II_Sales__c , account.Total_NS_Income_II_Shares__c },
                '3960' => new sobjectfield[] { account.Total_NS_RXR_Sales__c, account.Total_NS_RXR_Shares__c}
            },
            prior12MOS = new map<string, sobjectfield[]> {
                //'3910' => new sobjectfield[] { account.Prior_12_MOS_NIOR_I_Sales__c , account.Prior_12_MOS_NIOR_I_Shares__c},
                //'3911' => new sobjectfield[] { account.Prior_12_MOS_NS_Income_Sales__c , account.Prior_12_MOS_NS_Income_Shares__c },
                '3912' => new sobjectfield[] { account.Prior_12_MOS_NS_HI_Sales__c , account.Prior_12_MOS_NS_HI_Shares__c },
                '3915' => new sobjectfield[] { account.Prior_12_MOS_NS_Income_II_Sales__c , account.Prior_12_MOS_NS_Income_II_Shares__c },
                '3960' => new sobjectfield[] { account.Prior_12_MOS_RXR_Sales__c, account.Prior_12_MOS_RXR_Shares__c}
            };

for(Trades__c[] tradesList : [select Dollar_Amount_of_the_transaction__c, Fund_Number__c, Number_of_Shares_of_the_transaction__c, Resolved_Firm_Trading_ID__c, Resolved_Firm_Trading_IDs__c, Trade_Date__c from Trades__c where Resolved_Firm_Trading_ID__c in :accountIds and Fund_Number__c in (/**'3910', '3911',**/ '3912', '3915', '3960') and Dollar_Amount_of_the_transaction__c != null and Number_of_Shares_of_the_transaction__c != null and Trade_Date__c != null and Dollar_Amount_of_the_transaction__c >= 0 and Number_of_Shares_of_the_transaction__c >= 0 FOR UPDATE]){
    for(trades__c trade: tradesList) {

        if(date.today().year() == trade.trade_date__c.year()) {
            accounts.get(trade.Resolved_Firm_Trading_ID__c).put(ytd.get(trade.fund_number__c)[0], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(ytd.get(trade.fund_number__c)[0]))+trade.Dollar_Amount_of_The_Transaction__c);
            accounts.get(trade.Resolved_Firm_Trading_ID__c).put(ytd.get(trade.fund_number__c)[1], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(ytd.get(trade.fund_number__c)[1]))+trade.Number_of_Shares_of_the_transaction__c);
            //system.debug(ytd);
            //system.debug(LoggingLevel.DEBUG, + Limits.getCpuTime() + '/' + Limits.getLimitCpuTime());

            //( (date.ValueOf(date.today().month()).divide(3, 0) == date.ValueOf(trade.trade_date__c.month()).divide(3, 0)) )
            if((((date.today().month() - 1) / 3) + 1) == (((trade.Trade_Date__c.month() - 1) / 3) + 1))   {
                accounts.get(trade.Resolved_Firm_Trading_ID__c).put(qtd.get(trade.fund_number__c)[0], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(qtd.get(trade.fund_number__c)[0]))+trade.Dollar_Amount_of_The_Transaction__c);
                accounts.get(trade.Resolved_Firm_Trading_ID__c).put(qtd.get(trade.fund_number__c)[1], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(qtd.get(trade.fund_number__c)[1]))+trade.Number_of_Shares_of_the_transaction__c);
                //system.debug(qtd);
                //system.debug(LoggingLevel.DEBUG, + Limits.getCpuTime() + '/' + Limits.getLimitCpuTime());

                if(date.today().month() == trade.trade_date__c.month()) {
                    accounts.get(trade.Resolved_Firm_Trading_ID__c).put(mtd.get(trade.fund_number__c)[0], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(mtd.get(trade.fund_number__c)[0]))+trade.Dollar_Amount_of_The_Transaction__c);
                    accounts.get(trade.Resolved_Firm_Trading_ID__c).put(mtd.get(trade.fund_number__c)[1], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(mtd.get(trade.fund_number__c)[1]))+trade.Number_of_Shares_of_the_transaction__c);
                    //system.debug(mtd);
                    //system.debug(LoggingLevel.DEBUG, + Limits.getCpuTime() + '/' + Limits.getLimitCpuTime());

                    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());
                    }
                }
            }
        }
        else if(date.today().year()-1 == trade.trade_date__c.year()) {
            accounts.get(trade.Resolved_Firm_Trading_ID__c).put(py.get(trade.fund_number__c)[0], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(py.get(trade.fund_number__c)[0]))+trade.Dollar_Amount_of_The_Transaction__c);
            accounts.get(trade.Resolved_Firm_Trading_ID__c).put(py.get(trade.fund_number__c)[1], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(py.get(trade.fund_number__c)[1]))+trade.Number_of_Shares_of_the_transaction__c);
            //system.debug(py);
            //system.debug(LoggingLevel.DEBUG, + Limits.getCpuTime() + '/' + Limits.getLimitCpuTime());
        }
        accounts.get(trade.Resolved_Firm_Trading_ID__c).put(total.get(trade.fund_number__c)[0], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(total.get(trade.fund_number__c)[0]))+trade.Dollar_Amount_of_The_Transaction__c);
        accounts.get(trade.Resolved_Firm_Trading_ID__c).put(total.get(trade.fund_number__c)[1], ((Decimal)accounts.get(trade.Resolved_Firm_Trading_ID__c).get(total.get(trade.fund_number__c)[1]))+trade.Number_of_Shares_of_the_transaction__c);
        //system.debug(total);
        //system.debug(LoggingLevel.DEBUG, + Limits.getCpuTime() + '/' + Limits.getLimitCpuTime());
    }
}
    }
    inprog = true;
    update accounts.values();
    inprog = false;
}

}

4
  • Are you looking for a formula e.g. for a validation rule or a condition you can use in Apex? May 28, 2015 at 20:49
  • when you mean nothing does your if condition fail? I can see output with both adddays() and addyears() methods
    – Rao
    May 28, 2015 at 20:57
  • Hi @Seanceno, welcome to SFSE! Please take the time to visit the Help center and read How do I ask a good question. The more details you provide, particularly full code you've written so it has context, the more likely it is that someone will respond to your question with an answer that you'll find helpful. With what you've provided, it's unclear if your problem is with your data, an incomplete query or something else.
    – crmprogdev
    May 28, 2015 at 21:22
  • All, I edited my post with the full code as I wasn't sure how to add that many characters in a comment. Sorry, newbie here.
    – Seanceno
    May 28, 2015 at 22:02

3 Answers 3

2

I ran this in Execute Anonymous and it worked as expected (returned true):

Date a = date.today().addDays(-1);
Date b = date.today().addDays(-366);
Date c = date.today().addDays(-150);
System.debug((c > b) && (c < a));

What do you mean by it won't work?

1
  • I updated my question. Please take a look.
    – Seanceno
    May 28, 2015 at 22:30
1

I ran this code which shows the condition as you mentioned, and it is working

Date trade_date1= Date.newInstance(2014,2, 5);
Date trade_date2= Date.newInstance(2014,8, 5);
if(trade_date1 < date.today().addDays(-1) && trade_date1 > date.today().addDays(-366))
{
    system.debug('Inside the condition.1 ');

}
if(trade_date2 < date.today().addDays(-1) && trade_date2 > date.today().addDays(-366))
{
    system.debug('Inside the condition.2 ');

}


Please share the exact error you are getting and code which has problem

1
  • During my testing of this date range, if I set the date of the trade to last month (4/30/15), it will not display the total dollar amount of the trade in the Prior 12 Months field. If I set the date of the trade within this month (5/1/15), it displays correctly. The date range using addDays(-366) seems to only go back for the current month, not the a full year.
    – Seanceno
    May 28, 2015 at 22:23
1

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;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .