How to create one to one relationship between objects in salesforce

Salesforce provides two relationships known as one to many relationship and many to many relationship(this can be done using junction object).

Sometimes we need to establish one to one relationship between two objects, since salesforce doesn't have any direct methodology to build one to one relationship.

Here are a few ways to implement one to one relationship between two objects in salesforce,

1. By using Lookup field:

Step1:

         Create a Lookup field on two custom objects Employeid__c to Employe__c.

  Lookup field:

          Which creates a relationship that links one object to another object. This field allows users to click on a lookup icon and select a value from the popup list. The selected object is source of the values in the list.

          Here, we created lookup field between Employeid__c to Employe__c.

Step2:

         Create a custom field on the Employeid__c object, make this field unique (by using custom field we can hold the id of the associated employe__c).

Step3:

         Create a workflow rule on Employeid__c, then update the value of custom field.

  Workflow rule:

         Workflow rules can automate tasks, email alerts, field updates based on the organization requirement. 

         Here, we used workflow rule to update the value of field.

2. By using Roll up summary and triggers:

Step1:

         Create a Roll up summary field on custom object like numberofemployes__c by using count.

  Rollup summary:

         A field that displays the sum, minimum or maximum value of a field in a related list or the record count of all records listed in a related list.

         In this, we used numberofemployees__c as custom object and roll up type as count.

Step2:

         Create a trigger in Employee object to check whether the number of employees is equal to one. If it is one, then throw an error, else allow the user to create new Employeid__c.

  Trigger:

         Apex triggers enable you to perform custom actions before or after changes to records such as insertions, deletions and updates.

         In this trigger is used to run before the new Employeid__c is created so that it checks whether the number of employees euals to one or not.

    Posted On
  • 29 September 2017
  • By
  • Micropyramid

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
How to send email to a public group in Salesforce

In Salesforce it is quite difficult to send email to users individually when there are many. So, upon creating public groups with required users working ...

Continue Reading...
How to use Data loader in Salesforce

In any business importing and exporting of data is very important. Only to some extent, we can enter the data manually in the fields of ...

Continue Reading...
Packages in salesforce

In the salesforce organization, when you are building a component or application, it's important to know how the component or application should be distributed to ...

Continue Reading...
open source packages

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties