How to Create Self Relationship in Salesforce

In Salesforce, a self-relationship is the relationship between the records of the same object. It allows to establish the connection in the same object.

In this Salesforce tutorial, we are going to learn about self-relationships in Salesforce, in that we will see what self-relationship is, its uses, and when we can use self-relationships.

Additionally, we will see step-by-step how to create a self-relationship in Salesforce Lightning and Salesforce Classic.

Self Relationship in Salesforce

What is self-relationship in Salesforce?

In Salesforce, the self-relationship is the relationship that allows us to connect the records within the same object. Many times, self-relationship is used to represent the hierarchical structure of the records.

Following are the uses of self-relationship in Salesforce:

  • Lookup Fields: To establish the self-relationship within the same object, use the lookup fields. The lookup field refers to another record within the same object.
  • Hierarchical Structure: The self-relationship, most of the time, is used to represent the hierarchical structure within an object.
  • Parent-Child Relationship: In self-relationship, we can define parent-child relationships within the same object. For example, an employee can have a parent record as we as multiple child records in the same object.
  • Connects Records in Same Objects: The self-relationship is the relationship between the records of the same object that connects the records within the same object.

When can we use self-relationship in Salesforce?

Let’s take an example: When you are working as a recruiter in an IT company and you hired multiple candidates, then you need to create an employee record for those candidates, and while doing so, you also have to enter the details of the employee who referred the new employee.

In this scenario, the new candidate is referred by the company’s employee, so we need to define the relationship between the candidate and the employee, and to do that, we can use self-relationship.

For that, we need a custom Employee_c object and some existing employee records on which we can create self-relationships. In the next section, we will see how to create a self-relationship from this example.

How to Create Self Relationship in Salesforce Lightning

Using the above example and the following steps, we can create a relationship in Salesforce Lightning.

See also  Manage Territories and Territory Models for Opportunities in Salesforce

1. As you log in to Salesforce.org, on the Home Page, you will see the Object Manager tab. Click on it, and in the search option, search for the object that you want to create a self-relationship. Here, I have selected the Employee_c custom object. Click on that object.

Example of self-relationship in Salesforce

2. Then, you will see the Fields and Relationship option, in which we manage the datatypes, labels, and API names of fields. To create the self-relationship, click on the New button.

Uses of Self-relationship in Salesforce Lightning

3. Here, we have to know the important thing is that when we create the self-relationship, we have to select Lookup Relationship as a Data Type and then click on the Next button.

Create self-relationship in Salesforce Lightning

4. As discussed above, in a self-relationship, we establish the connection within the same object so that we are creating a self-relationship with the employee object, and the Related object will also be the Employee. Then click on the Next button.

How to create self-relationship in Salesforce Lightning

5. In this section, we have to fill in the following fields:

  • Field Label: In this field, we need to enter a label, and this label will be displayed on the record page. Here, I have entered the label as Referred by.
  • Field Name: This is an API field, when you click on that field it will automatically populate the API name.
  • Child Relationship Name: This is the object name on which we are creating the relationship.
  • Required: As we click the required checkbox true while creating the records, this lookup field will be mandatory.
  • Then click on the Next button.
Create self-relationship in Salesforce Lightning example

6. Here, we can select on which page layout we want to add this field that layout we can select and click on the Next button.

How to create self-relationship in Salesforce Lightning example

7. This Related List Label will display on the record page on the parent side, it shows the child records that are along with the parent record.

Then click on the Save button.

Self-relationship in Salesforce Lightning

8. To check how this can be useful in this self-relationship, go to the object on which you have created a relationship. Then click on the New button to create a new record.

You can see in the Referred by field that as we click on the lookup icon, the existing employee will display, and from that, we can select an employee who referred this new employee.

What is self-relationship in Salesforce Lightning

This is how we can create a self-relationship in Salesforce Lightning.

See also  How to Set Default Value For Text Field in Salesforce

How to Create Self Relationship in Salesforce Classic

Example:

Suppose, When you are working as an administrator for a school and you are creating the records of students who got admission to your school. While doing so, you also have to enter the details of the student who referred the new student.

In this scenario, the new student is referred by the student who is learning at your school. So, we need to define the relationship between the student and the new student, and to do that, we can use a self-relationship.

Using the above example and the following steps, we can create a relationship in Salesforce Classic.

1. If you are in Salesforce Lightning then to switch to Salesforce Classic click on the View Profile which is top of the right side, you will get the option that Switch to Salesforce Classic.

In Salesforce Classic, on the Setup page, you will see the + Plus icon in the right corner. Click on that icon. As you click on the + Plus icon, you will see All Tabs displayed on your screen.

Self relationship in Salesforce Classic examples

2. From the All Tabs, select which object you want to create a self-relationship with and click on that object. Here, I have selected a custom Student_c object.

Create self-relationship in Salesforce Classic example

3. After that you will see an arrow on the right side of the screen, click on that arrow and you will see the force.com list pop up. In that, you can see the View Fields option. Click on it.

How to apply self relationship in Salesforce Classic

4. To create the new self-relationship field in Custom Fields & Relationship we have the New Button, click on that button.

Create self-relationship in Salesforce Classic

5. To create the self-relationship, we don’t have a special data type, so we need to select Lookup Relationship as a Data Type to create a self-relationship. Then click on the Next button.

Example of self-relationship in Salesforce Classic

6. Here, we are creating the self-relationship, and in this, we establish a connection within the same object, so in the Related to object, we have to select the Student object only. Then click on the Next button.

How to create self-relationship in Salesforce Classic

7. Enter the necessary information in the following fields:

  • Field Label: In this field, we need to enter a label, and this label will be displayed on the record page. Here, I have entered the label as Referred by.
  • Field Name: This is an API field, when you click on that field it will automatically populate the API name.
  • Child Relationship Name: This is the object name on which we are creating the relationship.
  • Then click on the Next button.
How to create self-relationship in Salesforce Classic example

8. The Related List Label will display on the record page where the related list is displayed, the students’ label is by default you can change the label and enter as per your requirement. Then click on the Save button.

Self-relationship in Salesforce Classic

9. Now we will see how to create a record using self-relationship, for that go to the object on which you have created the self-relationship field. Here, I have displayed the existing students who are learning in school.

What is self-relationship in Salesforce Classic

10. Now you can see while creating the new student record that when we click on the lookup icon of the Referred by field the lookup dialogue box has been popped up, in that we can see the existing students records is displayed.

See also  How to Promote Existing Multi-select Picklist Field Value as Global Value Set on Salesforce

From that student, we can select any student who referred to the new student and click on the Save button.

Uses of Self-relationship in Salesforce Classic

This is how we can create a self-relationship in Salesforce Classic.

Conclusion

I hope you have got an idea about self-relationships in Salesforce, in that we have seen what the self-relationship is, its uses, and when we can use self-relationships. Additionally, we have seen step-by-step how to create a self-relationship in Salesforce Lightning and Salesforce Classic with examples.

You may like to read: