+ Start a Discussion
Keith Stephens 18Keith Stephens 18 

Help to rewrite trigger

Can someone help me to rewrite this trigger so that I do not have a select statement in my for loop?
Is this possible or is my trigger ok? It does work, but I thought having a select statment in my loop is bad.
trigger MostRecentCenter on Procedure__c (before insert, before update) {
   try{           
            
           Set<Id> procIds = new Set<Id>();
           Set<Id> caseIds = new Set<Id>();
               
               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) 
                 {                    
                    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;
                        update cs;                      
                                                               
                    }       
                }  
                
          //  }    //End of trigger.isBefore
        
    }catch(Exception e){
       //Package suspended, uninstalled or expired, exit gracefully.
       System.debug('MostRecentCenter');
    }
    
}

Thnaks,
Keith.
Best Answer chosen by Keith Stephens 18
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Here you go,
trigger MostRecentCenter on Procedure__c (before insert, before update) {
   try{           
            
           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) 
                 {                    
                    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;
						caseMap.get(pd.Case__c).Most_Recent_Center__c = procMap.get(pd.id).Center__c;
                        lstCase.add(caseMap.get(pd.Case__c));                      
                                                               
                    }       
                }
				if(lstCase.size()>0)
					update lstCase;
                
          //  }    //End of trigger.isBefore
        
    }catch(Exception e){
       //Package suspended, uninstalled or expired, exit gracefully.
       System.debug('MostRecentCenter');
    }
    
}

Also, I see you are using pd.Id in IF clause so I would suggest to run the trigger in After because pd.Id will be null in Before Insert.

Please mark as BEST ANSWER is it helps.