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 

Add Case Value

Hello All,
I need to help with my trigger.
What I am trying to accomplish is when a user adds a Procedure from our Case page we want a field on the Case page to populate with the Procedures Center Name.
Here is my trigger.  I am guessing it will need to be a trigger on Procudure__C and not on Case.
The line that is not working is: pd.Case__r.Most_Recent_Center__c = 'a0Qa000000RDrPuEAL';
This ID is the Center__C Id to get the name from the lookup field on the case page.
It seems I have all the values I need, but I cannot display anyof them in my debug statments.
trigger MostRecentCenter2 on Procedure__c (before insert, before update) {
    try{   
         if(trigger.isBefore){
            
            Set<Id> caseIds = new Set<Id>();
               
                for(Procedure__c cs : Trigger.New)
                {
                    if(cs.LastModifiedById != null) 
                    { 
                        caseIds.add(cs.LastModifiedById); 
                         //system.debug('>>>>>  '+(cs.LastModifiedById));

                    }                   
                }
                           
               //Map<string, Procedure__c> caseMap = new map<string, Procedure__c> ( [SELECT id, Name,  LastModifiedById  from Procedure__c 
               // where LastModifiedById IN: caseIds] );
             
              Map<string, Procedure__c> caseMap = new map<string, Procedure__c> ( [SELECT id, CenterID__c, Center__c,  Name, LastModifiedById from Procedure__c 
               where LastModifiedById IN: caseIds Order by Procedure_Date__c Desc] );

                //Map<string, Procedure__c> caseMap = new map<string, Procedure__c> ( [SELECT id, Name, LastModifiedById, Center__r.Name   from Procedure__c
                //where LastModifiedById IN: caseIds] ); 
                for(Procedure__c p: [SELECT LastModifiedById  from Procedure__c 
                                where LastModifiedById IN: caseIds]){                 
                    caseMap.put(p.LastModifiedById, p); 
                } 

               
              system.debug(' *************** caseMap :  ' + caseMap);
                for(Procedure__c pd: trigger.new) 
                {                   
                   
                    system.debug(' ***********  getobj= '+caseMap.getSObjectType());
                    system.debug('>>>>> in for loopA '+caseMap.size());
                    system.debug('>>>>> keysets'+caseMap.keyset());
                    system.debug('>>>>> in for loopB '+caseMap.values());
                    system.debug(' *********** pd.Center__c ' + pd.Center__c);
                    system.debug(' *********** pd.Center__r ' + pd.Center__r.Name);

                    system.debug('>>>>> LastModBy= '+caseMap.get(pd.LastModifiedById));
                    system.debug('>>>>> ID '+caseMap.get(pd.LastModifiedById).id);                    
                                               
            
                    if(caseMap.containsKey(pd.LastModifiedById)) 
                    {
                       
                            system.debug('>>>> BOOfdsfsd');                        
                            system.debug('>>>>> whatid= '+caseMap.get(pd.LastModifiedById).Center__c);
                           // system.debug('>>>>> center= '+caseMap.get(pd.LastModifiedById).Center__r.id);
                           // 'a0Qa000000RDrPuEAL'
                            pd.Case__r.Most_Recent_Center__c = 'a0Qa000000RDrPuEAL';
                           // pd.Case__r.Most_Recent_Center__c = caseMap.get(pd.LastModifiedById).id;
                                               
                    }       
                }  


            }    //End of trigger.isBefore
    
        
    }catch(Exception e){
       //Package suspended, uninstalled or expired, exit gracefully.
       System.debug('MostRecentCenter');
    }
    
}
Debug Output:
14:07:32:020 USER_DEBUG [31]|DEBUG| *************** caseMap :  {00522000000rdLyAAI=Procedure__c:{LastModifiedById=00522000000rdLyAAI, Id=a0M22000000bPu9EAE}, a0M22000000bPu9EAE=Procedure__c:{Id=a0M22000000bPu9EAE, CenterID__c=1376, Center__c=a0Qa000000RDrPuEAL, Name=Proc-97617, LastModifiedById=00522000000rdLyAAI}}
14:07:32:021 USER_DEBUG [35]|DEBUG| ***********  getobj= Procedure__c
14:07:32:021 USER_DEBUG [36]|DEBUG|>>>>> in for loopA 2
14:07:32:021 USER_DEBUG [37]|DEBUG|>>>>> keysets{00522000000rdLyAAI, a0M22000000bPu9EAE}
14:07:32:021 USER_DEBUG [38]|DEBUG|>>>>> in for loopB (Procedure__c:{Id=a0M22000000bPu9EAE, CenterID__c=1376, Center__c=a0Qa000000RDrPuEAL, Name=Proc-97617, LastModifiedById=00522000000rdLyAAI}, Procedure__c:{LastModifiedById=00522000000rdLyAAI, Id=a0M22000000bPu9EAE})
14:07:32:021 USER_DEBUG [39]|DEBUG| *********** pd.Center__c a0Qa000000RDrPuEAL
14:07:32:021 USER_DEBUG [40]|DEBUG| *********** pd.Center__r null
14:07:32:021 USER_DEBUG [42]|DEBUG|>>>>> LastModBy= Procedure__c:{LastModifiedById=00522000000rdLyAAI, Id=a0M22000000bPu9EAE}
14:07:32:021 USER_DEBUG [43]|DEBUG|>>>>> ID a0M22000000bPu9EAE
14:07:32:021 USER_DEBUG [49]|DEBUG|>>>> BOOfdsfsd
14:07:32:022 USER_DEBUG [65]|DEBUG|MostRecentCenter

Thanks,
Keith.
 
Keith Stephens 18Keith Stephens 18
Hello All,
I have revamped my trigger and now I am getting the proper id to pass to a lookup field on the case page, but the field is not populating.
Case is the parent of Procedure__c.

The line that is not working is pd.Case__r.Most_Recent_Center__c = caseMap.get(pd.id).Center__c;?  Is this not the correct way to set a value to the parent?
Here is my debug logs.
09:35:42:020 USER_DEBUG [31]|DEBUG| *************** caseMap :  {a0M22000000bPu9EAE=Procedure__c:{CenterID__c=1376, Center__c=a0Qa000000RDrPuEAL, Id=a0M22000000bPu9EAE}}
09:35:42:021 USER_DEBUG [35]|DEBUG| ***********  getobj= Procedure__c
09:35:42:021 USER_DEBUG [36]|DEBUG|>>>>> in for loopA 1
09:35:42:021 USER_DEBUG [37]|DEBUG|>>>>> keysets{a0M22000000bPu9EAE}
09:35:42:021 USER_DEBUG [38]|DEBUG|>>>>> in for loopB (Procedure__c:{CenterID__c=1376, Center__c=a0Qa000000RDrPuEAL, Id=a0M22000000bPu9EAE})
09:35:42:021 USER_DEBUG [39]|DEBUG| *********** pd.Center__c a0Qa000000RDrPuEAL
09:35:42:022 USER_DEBUG [50]|DEBUG|>>>> BOOfdsfsd
09:35:42:022 USER_DEBUG [51]|DEBUG|>>>>> whatid= a0Qa000000RDrPuEAL
09:35:42:022 USER_DEBUG [52]|DEBUG|>>>>> whatid2= 1376
09:35:42:022 USER_DEBUG [72]|DEBUG|MostRecentCenter

Here is my trigger:
trigger MostRecentCenter2 on Procedure__c (before insert, before update) {
    try{   
         if(trigger.isBefore){
            
            Set<Id> caseIds = new Set<Id>();
               
                for(Procedure__c cs : Trigger.New)
                {
                    if(cs.LastModifiedById != null) 
                    { 
                        caseIds.add(cs.LastModifiedById); 
                         //system.debug('>>>>>  '+(cs.LastModifiedById));
                    }                   
                }
                           
                    Map<string, Procedure__c> caseMap = new map<string, Procedure__c> ( [SELECT CenterID__c, Center__c from Procedure__c 
               where LastModifiedById IN: caseIds Order by Procedure_Date__c Desc] );
    
         
              system.debug(' *************** caseMap :  ' + caseMap);
                for(Procedure__c pd: trigger.new) 
                {                   
                   
                    system.debug(' ***********  getobj= '+caseMap.getSObjectType());
                    system.debug('>>>>> in for loopA '+caseMap.size());
                    system.debug('>>>>> keysets'+caseMap.keyset());
                    system.debug('>>>>> in for loopB '+caseMap.values());
                    system.debug(' *********** pd.Center__c ' + pd.Center__c);
                    //system.debug(' *********** pd.Center__r ' + pd.Center__r.Name);

                    //system.debug('>>>>> LastModBy= '+caseMap.get(pd.LastModifiedById));
                    //system.debug('>>>>> ID '+caseMap.get(pd.LastModifiedById).id);                    
                                               
            
                    //if(caseMap.containsKey(pd.LastModifiedById)) 
                    if(caseMap.containsKey(pd.id)) 
                    {
                       
                            system.debug('>>>> BOOfdsfsd');                        
                            system.debug('>>>>> whatid= '+caseMap.get(pd.id).Center__c);
                            system.debug('>>>>> whatid2= '+caseMap.get(pd.id).CenterID__c);
                            //See if I can't substring by 3.
                            //Maybe the pd.Case__r.Most_Receent_Center__c is wrong.
                            
                           // system.debug('>>>>> center= '+caseMap.get(pd.LastModifiedById).Center__r.id);
                             pd.Case__r.Most_Recent_Center__c = caseMap.get(pd.id).Center__c;
                       (pd.LastModifiedById).id;
                                               
                    }       
                }  


            }    //End of trigger.isBefore
    
        
    }catch(Exception e){
       //Package suspended, uninstalled or expired, exit gracefully.
       System.debug('MostRecentCenter');
    }
    
}
Thansk for any help or advice,
Keith.