Create One to One Relationship in Salesforce

Basically we have two relationships provided by salesforce:
1) One to Many
2) Many to Many (could be achieved using Junction object)

But here are few ways to implement scenario to achieve one to one relationship between two objects in salesforce:

Way No. 1:

  1. Create a lookup field on the child
  2. Create a unique field on the child, and hide this field from all page-layouts
  3. Write a workflow, for any change of the lookup field, to copy that value from the lookup field into the unique field
Way No. 2:
  1. create a master detail relationship 
  2. create one roll up summary field of count type on master object
  3. create validation rule on rollup summary field to check if count >1
  4. so it will give you error if it has more than one record for same master detail relation values.
Way No. 3:
  1. Create lookup fields on both objects, to each other
  2. Write triggers, for any change on these lookups, to either copy the record ID into the other object's lookup field when the other object's lookup field is empty, or disallow the change to the original record when the other object's lookup field is already populated with a different ID from the original record (i.e., already having a one-to-one relation)
Way No. 4: (Achieved using Code)
  1. Create trigger to check if the child record already exists for a parent ,not allowing the user to create the other one unless until first child record id deleted.
If you have any other Ideas in mind please do post in comment section. :)

Comments

Popular posts from this blog

List of Key Prefixes in Salesforce

SFDX Install CPQ in Scratch org