How to Create Relationship between fields in Salesforce using Look up and Master Details force Trigger Example to count number of completed tasks in a single contact.
In Salesforce, a lookup field is a type of relationship between two objects. A lookup field on one object can reference a row of data on another object.
The relationship is typically defined as follows:
- The first object has a lookup field that references the second object.
- On the second object, there is a Master-Detail Relationship field that points back to the first object.
- For example, let’s say you have a custom object called “Property” and another custom object called “Unit”. Each Unit has only one Property, but each Property can have multiple Units. In this case, you would create a lookup field on the Unit object that references the Property object. On the Property object, you would create a Master-Detail Relationship field that references the Unit object.
Creating a Lookup Field:
To create a lookup field, follow these steps:
- From Setup, click Create | Objects.
- Click the New Custom Object tab.
- Enter the required information, and then click Save & New.
- Click the New Field tab.
- For Type, select Lookup Relationship, and then click Next.
- Enter the required information for the new field, and then click Next.
- Select the object you want to reference in the lookup field, and then click Next.
- Select the optional settings for your lookup field, and then click Save & New or Save & Finish.”
Creating a Master-Detail Relationship Field:
To create a Master-Detail Relationship field, follow these steps:
- From Setup, click Create | Objects.
- Click the object you want to add the Master-Detail Relationship field to. In our example, we’ll click Unit.
- Click the New Field tab.
- For Type, select Master-Detail Relationship, and then click Next.
- Enter the required information for the new field, and then click Next.
- Select the object you want to reference in the Master-Detail Relationship field, and then click Next. In our example, we’ll select Property.
- Select the optional settings for your Master-Detail Relationship field, and then click Save & New or Save & Finish.”
Creating the Relationship:
Now that you’ve created the two fields, you need to establish the relationship between them. To do this, follow these steps:
- From Setup, click Create | Objects.
- Click the object you want to establish the relationship with. In our example, we’ll click Property.
- Click the New Field tab.
- For Type, select Lookup Relationship, and then click Next.
- Enter the required information for the new field, and then click Next.
- Select the object you want to reference in the lookup field, and then click Next. In our example, we’ll select Unit.
- Select the optional settings for your lookup field, and then click Save & New or Save & Finish.
- Click the object you want to add the Master-Detail Relationship field to. In our example, we’ll click Unit.
- Click the New Field tab.
- For Type, select Master-Detail Relationship, and then click Next.
- Enter the required information for the new field, and then click Next.
- Select the object you want to reference in the Master-Detail Relationship field, and then click Next. In our example, we’ll select Property.”
You can now reference the Property object from the Unit object, and vice versa. This relationship will be useful when you want to count the number of completed tasks in a single contact. For example, let’s say you have a custom field on the Property object called “Number of Units”. You can use this field to store the number of units that have been completed for a particular property. Then, on the Unit object, you can create a lookup field that references the Property object. When a unit is completed, you can simply update the Number of Units field on the related Property record.
Conclusion:
You can reference the Property object from the Unit object, and vice versa. This relationship will be useful when you want to count the number of completed tasks in a single contact. For example, let’s say you have a custom field on the Property object called “Number of Units”. You can use this field to store the number of units that have been completed for a particular property. Then, on the Unit object, you can create a lookup field that references the Property object. When a unit is completed, you can simply update the Number of Units field on the related Property record.