function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Keith Stephens 18Keith Stephens 18 

trigger not updating list

Hello All,
One I am new to SF and Apex and their triggers, but I wrote this trigger and at one time it worked in Dev. Now it is not updating the list, with all my system.debug statements it gets all the way down to the update lstCase; statment but then errors out.
Can someone help me out on what might be happening now.
Thanks,
Keith.
 
trigger MostRecentCenter on Procedure__c (after insert, after 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) 
                 {       
                     //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 = 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');
    }
    
}

 
Best Answer chosen by Keith Stephens 18
Keith Stephens 18Keith Stephens 18
Fixed issue was with my lookup, somehow it changed.

All Answers

Keith Stephens 18Keith Stephens 18
FYI,
This is the error I get.
09:54:12:076 USER_DEBUG [52]|DEBUG|Update failed. First exception on row 0 with id 500g000000GCzGkAAL; first error: FIELD_INTEGRITY_EXCEPTION, Most Recent Center: id value of incorrect type: a0Qg000000AEcRzEAL: [Most_Recent_Center__c]
Keith Stephens 18Keith Stephens 18
Fixed issue was with my lookup, somehow it changed.
This was selected as the best answer