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
AndrewNerneyAndrewNerney 

Apex Grandparent - Grandchild update, need map/list help

I have the following setup:
Physician__c Grandparent object
Account Child object
Assessment__c Grandchild object

I am trying to update values on Assessment__c whenever certain fields on Physician__c are updated, but I am getting an Apex CPU time limit exceeded when doing a bulk load.

From some research I see that I will need to use some combination of lists and/or maps to create a list of records to update. I can pull the list of Physicians but am getting stuck when attempting to identify the appropriate Assessment__c records.

In this code, Apex doesn't seem to like the IN :physWithAssessments.Id part.
List<Physician__c> physWithAssessments = [select Id, Fax__c, Fax_Number_Verified__c, (select Id from Accounts__r) From Physician__c where Id IN :Trigger.newMap.keySet()];

List<Assessment__c> currAssessments = [select Id, Member_Account__r.Primary_Care_Physician__r.Id, z_PCPFaxExists__c, z_PCPFaxNumVerified__c From Assessment__c  Where Member_Account__r.Primary_Care_Physician__r.Id IN :physWithAssessments.Id];
What might be a better approach here?

This is how the code currently looks (without any attempts at using lists or maps). It works fine unless you are doing a bulk load.

Warning: ugly code ahead!
trigger zUpdateFaxInfo_Physician on Physician__c (after update) {

    List<Assessment__c> assessmentsToUpdate = new List<Assessment__c>();

    for (Assessment__c assmnt: [select Id, Member_Account__r.Primary_Care_Physician__r.Id, Member_Account__r.Primary_Care_Physician__r.Fax__c, 
        Member_Account__r.Primary_Care_Physician__r.Fax_Number_Verified__c FROM Assessment__c WHERE Member_Account__r.Primary_Care_Physician__r.Id IN :Trigger.newMap.keySet()]){

        Physician__c oldphys = Trigger.oldMap.get(assmnt.Member_Account__r.Primary_Care_Physician__r.Id);
  
    //only update the database if the Physician's Fax__c or Fax_Number_Verified__c fields have been updated
        if (oldphys.Fax__c != assmnt.Member_Account__r.Primary_Care_Physician__r.Fax__c || 
            oldphys.Fax_Number_Verified__c != assmnt.Member_Account__r.Primary_Care_Physician__r.Fax_Number_Verified__c){ 

            System.debug('@FaxUpdate code fired on Physician__C record ' + assmnt.Member_Account__r.Primary_Care_Physician__r.Id);
    
    //set dummy field z_PCPFaxExists__c to TRUE if the physician has a fax number, or false otherwise
                If(assmnt.Member_Account__r.Primary_Care_Physician__r.Fax__c != null){
                    assmnt.z_PCPFaxExists__c = True;    
                }
                    Else
                {
                    assmnt.z_PCPFaxExists__c = False;      
                }
    
    //set dummy field z_PCPFaxNumVerified__c to TRUE if the physician's fax number has been verified, or false otherwise
                If(assmnt.Member_Account__r.Primary_Care_Physician__r.Fax_Number_Verified__c == True){
                    assmnt.z_PCPFaxNumVerified__c = True;    
                }
                    Else
                {
                    assmnt.z_PCPFaxNumVerified__c = False;      
                }    
    
               assessmentsToUpdate.add(assmnt);
           
        }

     }

//update database
       update assessmentsToUpdate; //update database
}
  1.  
rohitsfdcrohitsfdc
Hello DeadMoney,

Try the code given below. Let me know in case of any issues/concerns.

If that sovles your problem, please like this solution and mark it as the best answer.

Thanks


trigger zUpdateFaxInfo_Physician on Physician__c (after update) {


//map for physican to its assesment records 
map<id,list<Assessment__c>> phytoassesslistmap = new map<id,list<Assessment__c>>();

list<Assessment__c> asseslist = [select Id,Member_Account__r.Primary_Care_Physician__c,  Member_Account__r.Primary_Care_Physician__r.Id, Member_Account__r.Primary_Care_Physician__r.Fax__c, 
        Member_Account__r.Primary_Care_Physician__r.Fax_Number_Verified__c FROM Assessment__c WHERE Member_Account__r.Primary_Care_Physician__r.Id IN :Trigger.newMap.keySet()];
		
		for(Assessment__c assess : asseslist){
		if(phytoassesslistmap.get(assess.Member_Account__r.Primary_Care_Physician__c)!=null){
		list<Assessment__c> temp = phytoassesslistmap.get(assess.Member_Account__r.Primary_Care_Physician__c);
		temp.add(assess);
		phytoassesslistmap.put(assess.Member_Account__r.Primary_Care_Physician__c,temp);
		}
		else{
		phytoassesslistmap.put(assess.Member_Account__r.Primary_Care_Physician__c,new list<Assessment__c>{assess});
		}
		
		}
		
		

for(Physician__c ph : trigger.new){
if(ph.fax__c!=trigger.oldmap.get(ph.id).fax__c || ph.Fax_Number_Verified__c != trigger.oldmap.get(ph.id).Fax_Number_Verified__c){

list<Assessment__c> assesslist = phytoassesslistmap.get(ph.id);

if(ph.fax__c!=null){

for(Assessment__c assess : assesslist){
		assess.z_PCPFaxExists__c = True;    
}

else {
for(Assessment__c assess : assesslist){
		assess.z_PCPFaxExists__c = False;    
}

}
}

if(ph.Fax_Number_Verified__c){
for(Assessment__c assess : assesslist){
		assess.z_PCPFaxExists__c = True;    
}


}

else {
for(Assessment__c assess : assesslist){
		assess.z_PCPFaxExists__c = False;    
}

}


}



}		
		
		


}


AndrewNerneyAndrewNerney
Hi Rohit! Thanks for your response. I am taking your code and trying to work with it. I'm getting a compile error:

Error: Compile Error: expecting right curly bracket, found 'else' at line 35 column 0

But I like your examples. I'm going to try to sort out the brackets and then work with this code. There are an equal number of { and } symbols - 15 - so it just looks like something is out of place somewhere. I'm trying to figure out exactly what.
AndrewNerneyAndrewNerney
Hi Rohit,

I found the out-of-place curly bracket - see the code below. I also added some debug statements and indentations. But when doing a bulk upload I now get this error:


Attempt to de-reference a null objectTriggerzUpdateFaxInfo_Physican: line 32, column 1

Also, shouldn't there be an update statement in here somewhere?

Thanks again for your help. I'm working with this code to try to get it just right.

trigger zUpdateFaxInfo_Physician on Physician__c (after update) {

System.debug('@entering trigger zUpdateFaxInfo_Physician');

    //map for physican to its assesment records 
    map<id,list<Assessment__c>> phytoassesslistmap = new map<id,list<Assessment__c>>();
    
    list<Assessment__c> asseslist = [select Id,Member_Account__r.Primary_Care_Physician__c,  Member_Account__r.Primary_Care_Physician__r.Id, Member_Account__r.Primary_Care_Physician__r.Fax__c, 
            Member_Account__r.Primary_Care_Physician__r.Fax_Number_Verified__c FROM Assessment__c WHERE Member_Account__r.Primary_Care_Physician__r.Id IN :Trigger.newMap.keySet()];
            
    for(Assessment__c assess : asseslist){
System.debug('@Assessment List ID: ' + assess.Id);
    
        if(phytoassesslistmap.get(assess.Member_Account__r.Primary_Care_Physician__c)!=null){
            list<Assessment__c> temp = phytoassesslistmap.get(assess.Member_Account__r.Primary_Care_Physician__c);
            temp.add(assess);
            phytoassesslistmap.put(assess.Member_Account__r.Primary_Care_Physician__c,temp);
        }
        else{
            phytoassesslistmap.put(assess.Member_Account__r.Primary_Care_Physician__c,new list<Assessment__c>{assess});
        }
        
    }
        
    for(Physician__c ph : trigger.new){

        if(ph.fax__c!=trigger.oldmap.get(ph.id).fax__c || ph.Fax_Number_Verified__c != trigger.oldmap.get(ph.id).Fax_Number_Verified__c){
            list<Assessment__c> assesslist = phytoassesslistmap.get(ph.id);
System.debug('@trigger firing on Physician ' + ph.Id);    
            if(ph.fax__c!=null){
System.debug('@fax is NOT NULL on Physician ' + ph.Id);    
                for(Assessment__c assess : assesslist){ //this line is causing the error "Attempt to de-reference a null object"
                    assess.z_PCPFaxExists__c = True;    
                }
            }
    
            else {
System.debug('@fax is NULL on Physician ' + ph.Id);      
                for(Assessment__c assess : assesslist){
                    assess.z_PCPFaxExists__c = False;    
                }
    
            }
    
            if(ph.Fax_Number_Verified__c){
System.debug('@fax is VERIFIED on Physician ' + ph.Id);              
                for(Assessment__c assess : assesslist){
                        assess.z_PCPFaxExists__c = True;    
                }
            
            }
            
            else {
System.debug('@fax is NOT VERIFIED on Physician ' + ph.Id);   
                for(Assessment__c assess : assesslist){
                        assess.z_PCPFaxExists__c = False;    
                }
            
            }
        }
    }       
 
    
}


rohitsfdcrohitsfdc
Hello there,

I guess the problem is that we are accessing the field without querying it. Please try the code below

trigger zUpdateFaxInfo_Physician on Physician__c (after update) {

System.debug('@entering trigger zUpdateFaxInfo_Physician');

    //map for physican to its assesment records 
    map<id,list<Assessment__c>> phytoassesslistmap = new map<id,list<Assessment__c>>();
    
    list<Assessment__c> asseslist = [select Id,z_PCPFaxExists__c, Member_Account__r.Primary_Care_Physician__c,  Member_Account__r.Primary_Care_Physician__r.Id, Member_Account__r.Primary_Care_Physician__r.Fax__c, 
            Member_Account__r.Primary_Care_Physician__r.Fax_Number_Verified__c FROM Assessment__c WHERE Member_Account__r.Primary_Care_Physician__r.Id IN :Trigger.newMap.keySet()];
            
    for(Assessment__c assess : asseslist){
System.debug('@Assessment List ID: ' + assess.Id);
    
        if(phytoassesslistmap.get(assess.Member_Account__r.Primary_Care_Physician__c)!=null){
            list<Assessment__c> temp = phytoassesslistmap.get(assess.Member_Account__r.Primary_Care_Physician__c);
            temp.add(assess);
            phytoassesslistmap.put(assess.Member_Account__r.Primary_Care_Physician__c,temp);
        }
        else{
            phytoassesslistmap.put(assess.Member_Account__r.Primary_Care_Physician__c,new list<Assessment__c>{assess});
        }
        
    }
        
    for(Physician__c ph : trigger.new){

        if(ph.fax__c!=trigger.oldmap.get(ph.id).fax__c || ph.Fax_Number_Verified__c != trigger.oldmap.get(ph.id).Fax_Number_Verified__c){
            list<Assessment__c> assesslist = phytoassesslistmap.get(ph.id);
System.debug('@trigger firing on Physician ' + ph.Id);    
            if(ph.fax__c!=null){
System.debug('@fax is NOT NULL on Physician ' + ph.Id);    
                for(Assessment__c assess : assesslist){ //this line is causing the error "Attempt to de-reference a null object"
                    assess.z_PCPFaxExists__c = True;    
                }
            }
    
            else {
System.debug('@fax is NULL on Physician ' + ph.Id);      
                for(Assessment__c assess : assesslist){
                    assess.z_PCPFaxExists__c = False;    
                }
    
            }
    
            if(ph.Fax_Number_Verified__c){
System.debug('@fax is VERIFIED on Physician ' + ph.Id);              
                for(Assessment__c assess : assesslist){
                        assess.z_PCPFaxExists__c = True;    
                }
            
            }
            
            else {
System.debug('@fax is NOT VERIFIED on Physician ' + ph.Id);   
                for(Assessment__c assess : assesslist){
                        assess.z_PCPFaxExists__c = False;    
                }
            
            }
        }
    }       
 
    
}