Create One to One Relationship in Salesforce
Basically Salesforce offers two types of relationship:
- One To Many Relationship
- Many To Many Relationship (Using the concept of Junction object)
Sometimes we may need One To One relationship, But unfortunately Salesforce doesn’t allow any direct methodology to build One To one relationship.
Let’s consider the scenario that we want to establish a One to One relationship between two custom objects Employee__c and PAN_Card__c.
So, here are few ways to implement One To One relationship between two objects in Salesforce. We can achieve this by using configuration only and can also achieve this by using code to make it more scalable.
Option 1:
- Create a lookup field on PAN_Card__c to Employee__c.
- Create a custom field on the PAN_Card__c object and make the field unique. This field would be used to hold the ID of the associated Employee__c. Hide this field from all page layouts.
- Create a Workflow rule on PAN_Card__c. For any change of the lookup field on PAN_Card__c object, update the custom field on the PAN_Card__c object with the value of the associated Employee Id.
We have established a one to one relationship between PAN_Card__c and Employee__c. When we try to add a second PAN_Card__c to the Employee__c, the “unique” constraint would be violated and an error would be thrown. This approach will work on both standard and custom object.
Option 2:
- Create a master detail relationship on PAN_Card__c to Employee__c object.
- Create a roll up summary field on Employee__c object of PAN_Card__c with count type.
- Create a validation rule on Employee__c object rollup summary field to check if count > 1.
In this way also, We have established a one to one relationship between PAN_Card__c and Employee__c. So it will throw an error if Employee__c has more than one PAN Card.
Option 3:
- Create lookup fields on both objects PAN_Card__c and Employee__c, to each other.
- 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.
This is already having a one-to-onePassport relation.
Option 4:
- Create a trigger on PAN_Card__c object to check if the PAN Card record already exists for an Employee. If it exist, then throw an error, else allow the user to create.
Here is the example for Employee__c and PAN_Card__c object:trigger PANCardValidation on PAN_Card__c (before insert, before update) { Set<id> employeeIds = new Set<id>(); Map<id, Employee__c> mapEmployee = new Map<id, Employee__c>(); for (PAN_Card__c p : trigger.New) { employeeIds.add(p.Employee__c); } List<Employee__c> lstEmployee = [SELECT Id, Name FROM Employee__c WHERE Id IN : employeeIds]; if (!lstEmployee.isEmpty()) { for (Employee__c emp : lstEmployee) { mapEmployee.put(emp.Id, emp); } for (PAN_Card__c p : trigger.New) { if (mapEmployee.containsKey(p.Employee__c)) { p.addError('A PAN Card already exist for the employee - ' + mapEmployee.get(p.Employee__c).Name); } } } }