You need to sign in to do that
Don't have an account?
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.
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!
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 }
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
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.
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.
I guess the problem is that we are accessing the field without querying it. Please try the code below