+ Start a Discussion
DodiDodi 

Processing trigger.old records help

Dear Gurus,

 

I have a trigger that I am doing some summary counts on some lookup records(non master detail). My trigger processes fine with the trigger.new records. I am having trouble properly running the same process against a value in the trigger.old context. Since the trigger does not persist to the database prior to the entire trigger completion, I think my aggregate results are the same for both calls to the trigger utility class. How can I process the trigger.old request(second line in the trigger) against the records after they have updated. I am trying to update/refresh the old record count on the Trailer object with the correct count value after the new records are updated. Any help is appreciated....current code is below:

 

trigger ManifestUpdateTrailerContainerInfo on Manifest__c (after delete, after undelete, after update) {

    ManifestContainerInfoTriggerUtil.updateContainerCounts(Trigger.New);
     ManifestContainerInfoTriggerUtil.updateContainerCounts(Trigger.Old);

}

 

 

public with sharing class ManifestContainerInfoTriggerUtil {
    
    public static void updateContainerCounts(List<Manifest__c> lstManifest)    {
        
        Integer containerCount = 0;
        id trailerToUpdate;
        List<Trailer__c> lstTrailersToUpdate = new List<Trailer__c>();
    
        //new trailer collection
        Set<Id> ids = new Set<Id>();
    
          for (Manifest__c mf : lstManifest) {
              ids.add(mf.id);
          }
          
        //new counts
        AggregateResult[] counts = [SELECT Manifest__c ,count(id)containers FROM Container__c WHERE Manifest__c IN :ids GROUP BY Manifest__c];
        
        for(AggregateResult ar : counts){
            for(Manifest__c m: lstManifest){
                if(ar.get('Manifest__c') == m.Id && m.Trailer__c != null) {
                     trailerToUpdate = m.Trailer__c;
                    containerCount = integer.valueof(ar.get('containers'));
                    lstTrailersToUpdate.add(new Trailer__c(Id=trailerToUpdate, Container_Count__c = containerCount));
                }
            }
        }
        update lstTrailersToUpdate;
    }
        
}

 

DodiDodi

For some reason it does not look like all corresponance is making it back to the board....posting some help thus far.

 

Hi Dodi,

SubC4i (Regular Contributor) posted a new Reply in Apex Code Development on 11-07-2013 10:35 AM:

Re: Processing trigger.old records help

The issue I see here is that trigger.old should be processed like a map. Try the changes below:

 For trigger:

ManifestContainerInfoTriggerUtil.updateContainerCounts(trigger.new, trigger.oldMap);

For method:

public static void updateContainerCounts(List<Manifest__c> lstManifest, Map<Id,Manifest__c> IsManifestOld)    {

>>>>>>>>>>

 

Response:

 

That did not appear to help. I think the issue may be with aggregate results....As it is the same results until the new records are updated....I need to somehow process the logic after the records from the trigger.new are persisted.

SubC4i-dev1SubC4i-dev1

Sorry about that.  I had deleted my post after I realized it did not really solve your issue.  I'm wondering if a second trigger would help here.  As long as they occur on the same action, such as both in the after trigger, then one trigger could make a change that would set off the other trigger.  Just need to setup the conditions accordingly (and of course double-check to make sure the actions don't become recursive).