+ Start a Discussion
Viv RallsViv Ralls 

Trigger for Opportunity pushcount

Hello,  I'd like to have this trigger updated to count opportunity pushes into another quarter OR I might be able to use a pushcount that more than 3 months in the future.  Can anyone offer advice?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
trigger OppPusher on Opportunity (before update) {
Date dNewCloseDate;
Date dOldCloseDate;
Boolean bPushed=false;

for (Opportunity oIterator : Trigger.new) { //Bulk trigger handler so that you can mass update opportunities and this fires for all'
                                            // gets new values for updated rows
    dNewCloseDate = oIterator.CloseDate; // get the new closedate 
    dOldCloseDate = System.Trigger.oldMap.get(oIterator.Id).CloseDate; //get the old closedate for this opportunity
    if (dOldCloseDate<dNewCloseDate) { //if the new date is after the old one, look if the month numbers are different
        if (dOldCloseDate.month()<dNewCloseDate.month()) { // the month number is higher, it's been pushed out
            bPushed=true;
        }
        else {
            if (dOldCloseDate.year()<dNewCloseDate.year()) { // the month wasn't higher, but the year was, pushed!
                bPushed=true;
                }
            }
        
    }
    if (bPushed==true) { // let's go make them sorry
        if (oIterator.PushCount__c==null) {
            oIterator.PushCount__c=1;
        }
        else {
        oIterator.PushCount__c++;           
        }
    }
Veena Sundara-HeraguVeena Sundara-Heragu
You can use math.ceil((Decimal)dt.month()/3) to get the quarter of any date "dt".

So, you can do this

Integer dOldQuarter = Math.ceil((Decimal)dOldCloseDate.month()/3);
Integer dNewQarter = Math.ceil((Decimal)dNewCloseDate.month()/3); 
if (dOldCloseDate < dNewCloseDate && dOldQuarter < dNewQuarter) {
    bPushed=true;
}