I am trying to write a before update trigger that deletes all of the child records related to a parent when a checkbox is checked. I have another trigger that fires after update to rebuild these items so it has to happen before.
I am not getting an error when I save the code but I am getting an error when I check the box on a record. It says:
Error: Apex trigger DeleteRecipeSheetItems caused an unexpected exception, contact your administrator: DeleteRecipeSheetItems: execution of BeforeUpdate caused by: System.FinalException: Collection is read-only: External entry point"
My code:
trigger DeleteRecipeSheetItems on Recipe_Sheet__c (before update) {
Set<Id> recShts = Trigger.newmap.keySet();
List<Recipe_Sheet_Item__c> RecipeItemList = new List<Recipe_Sheet_Item__c>();
for(Recipe_Sheet__c rec :trigger.new){
if(rec.Recalculate_Recipe_Sheet_Items__c == True){
recShts.add(rec.Id); //Stores the Id of the Recipe_Sheet__c in the list
}
}
RecipeItemList = [SELECT Id,Recipe_Sheet__c,Item__c from Recipe_Sheet_Item__c
where Recipe_Sheet__c IN: recShts];
if(RecipeItemList.size() > 0)
delete RecipeItemList;
}