working on this trigger for 2 custom objects in a master-detail relationship, rep is the parent, role history is the child. the insert part works perfectly, just am struggling with the update portion. i've included the entire code in case my previous map needs to be used. when there is an existing role history record, when the user updates the "End Date" field on role history object to have any value (making it not null), it needs to set the "Rep Type" field on the rep object to null. Here is my code:


You need to add the End_Date__c to the query on parentsRep Map.

parentReps = new Map<Id, Rep__c>([SELECT Id, Rep_Type__c, (SELECT Id, Type__c, End_Date__c FROM Role_Histories__r WHERE End_Date__c = null ORDER BY CreatedDate DESC LIMIT 1) FROM Rep__c WHERE Id IN :listIds]);

RoleHistoryAfterTrigger: execution of BeforeUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Role_History__c.End_Date__c Trigger.RoleHistoryAfterTrigger: line 37, column 1

The error is telling you what is the issue, every time you come across this issue check if the query has the needed fields.

  • Which kind of field is Rep_Type__c? is a picklist field? if it's a picklist, look for the Picklist Options if Restrict picklist to the values defined in the value set is checked. @testing – Matías Castelli Sep 24 '19 at 17:19
  • It's a required field? – Matías Castelli Sep 24 '19 at 18:07
  • reps.Rep_Type__c = ''; instead of null – Matías Castelli Sep 24 '19 at 19:04
  • i think i finally got it to work?? i updated the original question so you can see the code i used. however the only thing i'm confused by is i have to refresh the page like 4 times for the field to show as null? no idea why there's such a delay – testing Sep 24 '19 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.