1

I have 1 custom object "Address" having master detail relationship with contact object in salesforce. I also have one checkbox field on both of the objects. The requirement is to have checkbox checked on only one of the objects or at least one of them but not on both or neither of the objects.

On the detail "address" object I have written validation rule and working fine but I am not sure how to write trigger on contact object to compare field from child object and give error.

  • Just note, one consequence of this validation structure is that the checkbox must be true on insert, since there are not yet any child records to be checked. – Adrian Larson Jun 11 '16 at 16:42
  • oh good catch. I almost forgot about insert. I guess I will have to go for a trigger. – apple123 Jun 11 '16 at 20:55
3

You are going to need a conditional rollup summary field to count child Address__c records where the box is checked. Since the relationship is one to many, you need to validate if any child meets the criteria.

The most succinct (parens for clarity):

Checkbox__c = (CheckedChildCount__c > 0)

You can also spell it out in more detail:

OR(
    AND(Checkbox__c, CheckedChildCount > 0),
    AND(NOT(Checkbox__c), CheckedChildCount = 0)
)
  • So basically this field will be on parent (contact) object, Is that correct? My goal here is to put a validation that if child checkbox is checked sf should throw and error that only one of the checkboxes can be checked and also if none of them are checked it should throw error. can I achieve this by the same? – apple123 Jun 11 '16 at 15:50
  • I just edited my comment. and I really appreaciate your help here. thank you. – apple123 Jun 11 '16 at 15:53
  • Ah, the neither checked part was a little unclear to me at first. Updated my answer. @apple – Adrian Larson Jun 11 '16 at 16:02
  • Any luck? @apple – Adrian Larson Jun 14 '16 at 15:27
  • not yet, have not tried yet. I actually wants to render a visualforce page if both of them are unchecked. I am trying to figure out how do I render that page if none of the checkboxes are checked? – apple123 Jun 14 '16 at 17:34

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.