Showing posts with label One To One Relationship in Salesforce. Show all posts
Showing posts with label One To One Relationship in Salesforce. Show all posts

Friday, April 19, 2013

One to One Relationship in Salesforce


We all know about the 1-M && M-M relationship(using junction Object) in salesforce.
Some times we may need 1-1 relationship among two objects.
but Unfortunately Salesforce doesnot allow any direct methodolgy to build 1-1 relationship

We can achieve the same by workflow or trigger. But workflow is more flexible & the easier way

Steps
-------
Say Two Objects OBJECT-A and OBJECT-B

1- create a look up field in one object
    say in OBJECT-A, there is a lookup to OBJECT-B
    field Name= obj-B (say)

2- Create another textfield at same Object with unique property (used to hold same obj-B field value)
     fieldName= unique_Obj-B (say)

3- Define a workFlow Rule /Trigger to  Update the custom "unique_Obj-B" field with the value of the associated OBJECT-B ID.(i.e the field value of "obj-B")

Now we have done with 1-1 Relationship
When we try to add a second same OBJECT-B to the OBJECT-A, the "unique" constraint in OBJECT-A (field "unique_Obj-B")would be violated and an error would be thrown.....