function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Possible to use roll-up-summary fields to enforce a 1:1 relationship btw objects?


I took the 401 Developer class back in September and I recall the instructor saying something about how Roll-up Summary Fields can be used to enforce a 1:1 relationship between two objects. I have a custom object, Membership__c, which has a master-detail relationship to Accounts. I only want there to be one record in Membership__c for each Account.

Is there a way that I can put a RSF in Account, and if the RSF>1 then... (that's the part I can't recall). Would it be a workflow rule? I tried doing that, but then didn't see how I could enforce an error message.

Or since this is a 1:1, would best practices be to simply add those fields to Account (since we get 500 fields) - it just seemed like a lot of clutter since some of the Accounts aren't members.

Or would this have to be done with Apex code?

Many thanks,
Why is it that the answer usually dawns on me soon after posting the question? Probably it's the brain freeze before posting the question.

Here's the answer in case anyone gets stumped with this one:

Very simple. I created a Validation rule on the Accounts object that displays an error message if the Roll-up Sumary Field >1. It displays an error message that is displayed when the user tries to add the duplicate record.

I think I was trying to get it to "do" more (e.g. delete the duplicate, or disable tne "new" button).
Kim -

Thanks for contributing the answer.  You have taken the high road - designing your application to prevent bad data, rather than trying to clean it up later.

Does this approach prevent the user from adding any new child records?  I am confused by your phrase "add the duplicate record."


I want to create a new custom object that relates to the Opportunity object.  I only want there to be one entry per Opportunity. I’ve created a Master-Detail relationship, but that doesn’t appear to limit the entries for the relationship. Will your approach address this issue appropriately?


What would be the syntax to use in the validation rule to make this work?  I don't see how I can check to see what the current count of related records is.