The Push Counter
Nov 22, 2009Now that Dreamforce'09 is over, we can go back to talking over twitter, and posting blog entries. This entry is about a counter to detect when an Opportunity has been pushed out. Pushing happens when a sales rep or account exec moves out the predicted close date of an opportunity in salesfore.com into another month.
It's useful to show sales managers when their deals have been pushed often - in the deal review call, you'll now be able to hear the manager saying "This deal has been moved out 3 times. Why should I believe it's going to close this month". Also, you might want to have a dashboard component to rank salespeople by total number of pushes.
The idea
We'll have a field on the Opportunity - PushCount.
That field will be updated every time the Month in the Close Date when the user is saving is later than the month in the Close Date before.
Then every time the user moves out the date and goes over a month boundary, we'll increase the number of times it's been pushed.
When we put the field on page layouts, we're going to make it read-only.
The implementation:
We'll create a new number field with no decimals - PushCount:
now we just need a way to fill that - the trigger:.
The trigger is going to only run when an Opportunity is being updated, before the new version is written to the system. It's going to get the new and old dates, do some quick math, and then, if the Opportunity has been pushed out, update the push counter.
1: trigger OppPusher on Opportunity (before update) { 2: Date dNewCloseDate; 3: Date dOldCloseDate; 4: Boolean bPushed=false; 5: 6: for (Opportunity oIterator : Trigger.new) { //Bulk trigger handler so that you can mass update opportunities and this fires for all'
7: // gets new values for updated rows
8: dNewCloseDate = oIterator.CloseDate; // get the new closedate
9: dOldCloseDate = System.Trigger.oldMap.get(oIterator.Id).CloseDate; //get the old closedate for this opportunity
10: if (dOldCloseDate<dNewCloseDate) { //if the new date is after the old one, look if the month numbers are different
11: if (dOldCloseDate.month()<dNewCloseDate.month()) { // the month number is higher, it's been pushed out
12: bPushed=true; 13: }14: else {
15: if (dOldCloseDate.year()<dNewCloseDate.year()) { // the month wasn't higher, but the year was, pushed!
16: bPushed=true; 17: } 18: } 19: 20: }21: if (bPushed==true) { // let's go make them sorry
22: if (oIterator.PushCount__c==null) {
23: oIterator.PushCount__c=1; 24: }25: else
26: { 27: oIterator.PushCount__c++; 28: } 29: } 30: } 31: }The package:
As usual, here's a package with the two items field (has to have the right developer name) and trigger:
https://login.salesforce.com/?startURL=%2Fpackaging%2FinstallPackage.apexp%3Fp0%3D04t30000000hkQ0
The package also has two reports – a view on the owners of opportunities pushed, and a view of where pushed opportunities are by stage.
Have fun looking at pushed deals!

Awesome, just installed it, will let it peculate for a bit before I show it off
Posted by: Fifedog | November 23, 2009 at 01:46 PM