What is the best way to create 1:1 relationship in Force.com. I have an Object A. What i need to create is another object B, which will be like an extension to this object. Since there are already large number of fields on object A, i want to created related set of fields in another object B. When a record in Object A gets saved, i want to make sure that there is one and only one related record in Object B. I dont want to use triggers for validation and want to achieve this only through clicks.
Prior to Spring 15, this was not possible without Apex code. Just to clarify the part that is key:
When a record in Object A [the parent] gets saved, i want to make sure that there is one and only one related record in Object B [the child].
This is different from making sure that the child is only related to a single parent. This is ensuring that every time a parent record is created, you also create a child, and that there only is ever one child.
This would also assume that you would want to ensure that any time the parent is deleted, the child would be deleted.
You would also want to prevent the child from ever being deleted unless the parent is being deleted.
Finally that you would never want the child to be reparented (assigned a new parent record).
With the new Lightning Process Builder tool, this is possible, along with a few old tried and true tricks.
Child__cobjects are created.
Child__cadd a Master-Detail relationship.
- In the relationship, ensure you do not allow reparenting (this is default anyway, but just be sure!)
- A roll-up summary field in the parent to count children.
- With Lightning Process Builder, create a new process. In that process make it so that everytime you create a
Parent__crecord, a new
Child__cis created and assign the
Parent__c.Idvalue to the M-D relationship field. (Don't forget to activate it.)
- Create a validation rule that ensures your RSF never is more than 1, and on update only is never less than 1.
Above, Step 5 ensures that everytime a
Parent__c record is created the child is created, too, with the unique text field populated. This is the new part.
Step 6 ensures that when someone attempts to create a second
Child__c record for the same
Parent__c the second attempted child will fail. It also ensures that the child is never deleted (on update never less than 1 will ensure that the child always sticks around). This isn't particularly new, but with the M-D field, I find the roll-up summary field validation approach is a much more clean way of ensuring a unique child record.
Assume one Chinese Parent can have only 1 Chinese Child. 1) First create the parent object - Chinese Parent
2) Create the child object - Chinese Child
3) Create the 1:M relationship between the 2 objects
4) Create a field named 'Parent Id' on the child object and make it required
5) Create a Workflow Rule One2One Relationship on child object Rule Criteria - Chinese Child: Chinese Child Name not equal to null Evaluation Criteria - Evaluate the rule when a record is created
6) Then create a Workflow Action Field Update Action: Parent Id update Field to Update Chinese Child: Parent Id Formula Value Chinese_Parent__r.Id
Test it creating a parent record, a child record then again a child record with the same parent. You would get an error - Duplicate value on record as we made the Parent Id as unique on the child object in step no. 4