• cfanger
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
trigger EDC_Account_Number_Trigger on EDC_Account_Number__c (before insert, before update, after insert, after update) {

//Description: Associates all EDC Account Number Records with their previous and next iterations according to
//the EDC Account with the closest end date in both the past and future
    for(edc_account_number__c EDC:trigger.new)
        {
            list<edc_account_number__c> EDCListPrevious = [Select Id, name,start_date__c,end_date__c from edc_account_number__c
            where name = :EDC.name and End_Date__c <= :EDC.End_Date__c and ID != :EDC.id ORDER BY End_Date__c DESC];
            
            if(EDCListPrevious.isEmpty())
                {                   
                }
            Else
                {
                    if(trigger.isbefore)
                        {                       
                            EDC.Previous_EDC_Account_Number_Record__c = EDCListPrevious[0].id;
                        }
                    if(trigger.isafter)     
                        {
                            EDC_Account_Number__c EDCPrevious = [Select Id, name,Next_EDC_Account_Number_Record__c 
                            from EDC_Account_Number__c where Id = :EDCListPrevious[0].id];
                    
                            EDCPrevious.Next_EDC_Account_Number_Record__c = EDC.id;
                            update EDCPrevious;             
                        }
                }
            list<edc_account_number__c> EDCListNext = [Select Id, name,start_date__c,end_date__c from edc_account_number__c
            where name = :EDC.name and End_Date__c >= :EDC.End_Date__c and ID != :EDC.id ORDER BY End_Date__c ASC];
                
            if(EDCListNext.isEmpty())
                {
                }
            Else
                {
                    if(trigger.isbefore)
                        {
                            EDC.Next_EDC_Account_Number_Record__c = EDCListNext[0].id;                      
                        }
                }
        }
}

 I know that this trigger works on a single record but I am struggling as to how I should bulkify it. I appreciate any assistance as this is an area that I struggle with on a regular basis. I am fairly new to APEX development so I will take any feedback I can get. Thanks.

trigger EDC_Account_Number_Trigger on EDC_Account_Number__c (before insert, before update, after insert, after update) {

//Description: Associates all EDC Account Number Records with their previous and next iterations according to
//the EDC Account with the closest end date in both the past and future
    for(edc_account_number__c EDC:trigger.new)
        {
            list<edc_account_number__c> EDCListPrevious = [Select Id, name,start_date__c,end_date__c from edc_account_number__c
            where name = :EDC.name and End_Date__c <= :EDC.End_Date__c and ID != :EDC.id ORDER BY End_Date__c DESC];
            
            if(EDCListPrevious.isEmpty())
                {                   
                }
            Else
                {
                    if(trigger.isbefore)
                        {                       
                            EDC.Previous_EDC_Account_Number_Record__c = EDCListPrevious[0].id;
                        }
                    if(trigger.isafter)     
                        {
                            EDC_Account_Number__c EDCPrevious = [Select Id, name,Next_EDC_Account_Number_Record__c 
                            from EDC_Account_Number__c where Id = :EDCListPrevious[0].id];
                    
                            EDCPrevious.Next_EDC_Account_Number_Record__c = EDC.id;
                            update EDCPrevious;             
                        }
                }
            list<edc_account_number__c> EDCListNext = [Select Id, name,start_date__c,end_date__c from edc_account_number__c
            where name = :EDC.name and End_Date__c >= :EDC.End_Date__c and ID != :EDC.id ORDER BY End_Date__c ASC];
                
            if(EDCListNext.isEmpty())
                {
                }
            Else
                {
                    if(trigger.isbefore)
                        {
                            EDC.Next_EDC_Account_Number_Record__c = EDCListNext[0].id;                      
                        }
                }
        }
}

 I know that this trigger works on a single record but I am struggling as to how I should bulkify it. I appreciate any assistance as this is an area that I struggle with on a regular basis. I am fairly new to APEX development so I will take any feedback I can get. Thanks.