+ Start a Discussion
pjaenick.ax736pjaenick.ax736 

Account Rollup field - updating

We have a rollup field on the Account object that references a custom table - the intent of this rollup is to pull in the latest value only from the most recently posted month.  The rollup today would have filter criteria of (for example) "Posted Date equals 8/9/13".  We manually change this rollup each time we upload more data to the custom table.

 

I'm looking for a way to make this more dynamic, such that the Account obj always pulls in the "latest" data available, without having to adjust the rollup field each time.  I tried changing the rollup to instead  point to a field on the custom object called "Most Recent", defaulted to one, with an accompanying apex trigger/class to set all prior records set to one back to zero, but this caused contention (DML) issues due to the associated rollup field on the Account.

 

Any other ideas?

 

Thanks,

Pete

pjaenick.ax736pjaenick.ax736

Here's my trigger...

trigger MyCustomTable on MyCustomTable__c (Before Insert) {

    Date NewPostingDate=Trigger.new.get(0).Statement_Date__c;

    MyCustomTableModel.UnflagOldRecords(NewPostingDate);
}

 ... and class...

public class MyCustomTableModel{

    public static void UnflagOldRecords(Date NewPostingDate){

        Map<Id, MyCustomTable__c> MCTToUpdate = new Map<Id, MyCustomTable__c>();
        Map<Id, MyCustomTable__c> MCTMap = new Map<Id, MyCustomTable__c>(
            [
             SELECT Most_Recent__c, Statement_Date__c
               FROM MyCustomTable__c
              WHERE Most_Recent__c = 1
                AND Statement_Date__c != :NewPostingDate
            ]
        );

        If (!MCTMap.isempty()) {
            for(MyCustomTable__c MCT: MCTMap.values()) {
                MCT.Most_Recent__c = 0;
                MCTToUpdate.put(MCT.id, MCT);
            }

            update MCTToUpdate;
        }
    }
}