1

The following code checks to see if a contact has at least one membership record and if it does, the contacts first and last name is populated on the respective account. This trigger has been working fine when one creates or edits one membership record at a time but if one dataloads a lot of records, the code ends up putting every contact who matches the criteria in every account. How to get the code to only put the correct contacts in the field on the account for only that account?

trigger trgr_Update_BOG_Members_List on BOG_Membership__c (after insert,after update,after delete) 
{ 
    ID currID; 
    Set<Id> accID = new Set<Id>();

    if(Trigger.isInsert || Trigger.isUpdate) 
    { 
        LIST<BOG_Membership__c> accountMap = [SELECT ID, Contact__r.AccountId FROM BOG_Membership__c 
                                              WHERE ID IN :Trigger.newMap.keySet() 
                                              AND Contact__r.AccountId <> Null]; 
        if(accountMap.size()>0)
        {

            for(BOG_Membership__c bog : Trigger.new)
            {
                  accID.add(bog.frm_AccountID__c);
            }
        }
    } 

    // DELETE    
    if(Trigger.isDelete) 
    { 
         for(BOG_Membership__c bog : Trigger.Old)
         {
             accID.add(bog.frm_AccountID__c);
         }    

    } 

    // Common Operation
    List<AggregateResult> aggregates = [ SELECT Contact__r.AccountId , Contact__r.FirstName , Contact__r.LastName FROM BOG_Membership__c 
                                        WHERE Contact__r.AccountId = :accID AND BOG_Membership_Status__c = 'ACTIVE'
                                        GROUP BY Contact__r.LastName, Contact__r.FirstName, Contact__r.AccountId 
                                        Order by Contact__r.LastName ];     

//system.debug('debug: aggregates size '+aggregates.size() );
    if(aggregates.size()>0)
    {
        Map<Id, Account> parentRecords = new Map<Id, Account>(); 

        String listNames = ''; 
        Integer cnt = 0;
        Integer remainder =0;
        for (AggregateResult aggregate : aggregates) 
        { 
            Id parentId = (Id)aggregate.get('accountId'); 
            String firstName = (String)aggregate.get('FirstName'); 
            String lastName = (String)aggregate.get('LastName'); 

            listNames = listNames + firstName + ' ' + lastName + '-';

            parentRecords.put(parentId, new Account( 
            Id = parentId, BOG_Members_List__c = listNames 
            )); 
        } 
    //    system.debug('debug: parentRecords.values() '+parentRecords.values());
        update parentRecords.values(); 
    }
}
2

You are not resetting the listNames variable between the different accounts. So the list is getting longer and longer. So instead of adding to that string, fetch the account-specific string from the map and append to it.

Something like this:

   if(parentRecords.containsKey(parentId)){
        listNames = parentRecords.get(parentId).BOG_Members_List__c  +             firstName + ' ' + lastName + '-';
   } else {
        listNames = firstName + ' ' + lastName + '-';
   }

   parentRecords.put(parentId, new Account( 
        Id = parentId, BOG_Members_List__c = listNames 
   )); 

As a further improvement you should probably first store the lists in a Map and only create the actual Account records in a final loop.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.