You need to sign in to do that
Don't have an account?
alekhya mathukumilli 28
I need to write a trigger on child object before insert and before update to check that only one child record related to the parent has the check box to true. else throw an error on the new records
child_object__c
Parent_object__c
checkbox__c
Parent_object__c
checkbox__c
All Answers
In this trigger, the first for-loop groups all of the child records with the checkbox set by their parent IDs. The second for-loop adds an error to child records if two or more are being inserted onto the same parent. After the second for-loop, the map contains only lists with a single child record in them. The third for-loop uses an aggregate query to find any parent records that already have child records with the checkbox set, and adds an error to the child records that are being inserted onto those parents.