+ Start a Discussion
Keith Stephens 18Keith Stephens 18 

Trigger Bulkification

Hello All,
Can someone tell me if my trigger is bulkified?
I have removed all of the SOQL statments out of the loop, but I don't know if that is all I have to do to bulkify my code.
trigger MostRecentCenter on Procedure__c (after insert, after update) {
   try{           
          if (TriggerSettings.areTriggersDisabled()) return;  
           Set<Id> procIds = new Set<Id>();
           Set<Id> caseIds = new Set<Id>();
		   List<Case> lstCase = new List<Case>();
               
               for(Procedure__c proc : Trigger.New)
               {                 
                   if(proc.CreatedDate != null) 
                   { 
                       procIds.add(proc.LastModifiedById);  
                       caseIds.add(proc.Case__c);                      
                   }                        
               }                        
               
              Map<string, Procedure__c> procMap = new map<string, Procedure__c> ( [SELECT CenterID__c, Center__c from Procedure__c 
               where LastModifiedById IN: procIds Order by CreatedDate Desc] );     

              Map<string, Case> caseMap = new map<string, Case> ( [SELECT Id, Most_Recent_Center__c FROM Case WHERE Id IN: caseIds] );  
                         
                 for(Procedure__c pd: trigger.new) 
                 {       
                     //system.debug(pd.id);

                    if(procMap.containsKey(pd.id)) 
                    {           
                      
                        //Id centerID = (Id)procMap.get(pd.id).Center__c;
                        //Case cs = (Case)[SELECT Id, Most_Recent_Center__c FROM Case WHERE Id = :pd.Case__c];
                        //cs.Most_Recent_Center__c = centerID;
						
                        //system.debug('pd.id = ' + pd.id);
                        //system.debug('caseMap = ' + caseMap);
                        //system.debug('pd.case = ' + pd.Case__c);
                        //system.debug('procmap.get = ' + procMap.get(pd.id).Center__c);

                        caseMap.get(pd.Case__c).Most_Recent_Center__c =(Id)procMap.get(pd.id).Center__c;                     
                        lstCase.add(caseMap.get(pd.Case__c));                      
                                                               
                    }       
                }
                   // system.debug('lstCase Size = ' + lstCase.size());
				if(lstCase.size()>0)
                    //system.debug('in size if');
					update lstCase;                
          
        
    }catch(Exception e){
       //Package suspended, uninstalled or expired, exit gracefully.
       System.debug('Error MostRecentCenter');
       system.debug(e.getMessage());
       
    }
    
}


Thanks,
KS
v varaprasadv varaprasad
Hi Keith,

Your trigger is bulkified.

Could you please explain to me what is your requirement and functionality of the trigger.
So that I can understand your code is working or not. 

Thanks
Varaprasad