Fields andRelationships – Objects and Data Management – Salesforce Certified Platform Developer I Study Guide

2.2 Fields andRelationships

Fields are columns on an object, which can be used for individual data records stored in an object. They define the specific attributes or characteristics of the data you want to capture. Each field has a data type that determines the kind of data it can store like text, number, date, picklist, and other.

Standard fields are those that are already present in the Salesforce when a new organization is created. They are present in all organizations where the same features are enabled. These fields cannot be customized to the same degree as custom fields. For example, you can change the display label, but not the underlying API name or data type.

Custom fields are fields that have been added to the standard Salesforce schema to tailor the data for each object. The user who creates the field can specify the field type and any applicable limitations such as the maximum number of characters in a text field. Overall, custom fields provide more flexibility and customization options than standard fields, but both are integral to managing data in Salesforce. You can query standard and custom fields and objects using SOQL in the same way. We will come to this in the next chapter.

Relationships define how different objects are related to each other. In Salesforce, you can establish relationships between objects to represent connections between data records.

Master relationship is a type of object relationship that is used to define the relationship between a master or parent and a detail or child object. The master object controls certain behaviors of the detail object such as if record of the master object is deleted, its related detail records are also deleted, or managing sharing and security settings. Additionally, the owner field on the detail object is not available and is automatically set to the owner of its associated master record. Master-detail

relationships can exist between a standard object as master and a custom object as detail, or between two custom objects. Each detail record must be associated with a master record, establishing a hierarchical relationship. Importantly, standard objects cannot be on the detail side of a relationship with a custom object. When a master record is deleted, its detail records are automatically deleted due to the master-detail relationship. In all cases of master-detail relationships, the connection is established through a relationship field on the detail object. These relationships enforce data integrity and enable special features like roll-up summaries and cascading deletions.

Figure 2-1.  Master relationship between two objects

Lookup relationships are a way to link two objects together. They are similar to master-detail relationships, but they do not support sharing and security settings or roll-up summary fields. A lookup relationship is a loosely connected relationship that enables one-to-many connections between objects. It allows you to connect one object to another in a one-to-many fashion. In a lookup relationship, one object has a field that references the field of another object. This field is called a lookup field, and it allows you to look up records on another object. For example, account as standard object represents company records, and project as custom object represents individual projects associated with specific accounts. Each account can have multiple associated project records, as indicated by the lookup relationship in the project object. Deleting an account does not automatically delete associated project records, as lookup relationships do not enforce cascading deletion. Projects remain intact even if the associated account is deleted.

Many-to-many relationships in Salesforce allow each record of one object to be linked to multiple records from another object and vice versa. To create a many-to-many relationship in Salesforce, a junction object is needed. A junction object is a custom object that links two other objects together. This means that multiple records from one object can be associated with multiple records from another object. For example, let’s say we have two objects: “Books” and “Authors.” A book can have multiple authors, and

an author can have written multiple books. In this scenario, we would create a junction object called “Book Author” to establish a many-to-many relationship between the “Books” and “Authors” objects.

Many-to-many relationships are useful when dealing with complex data structures and can help to simplify data management.

Figure 2-2.  Many-to-many relationship with junction object

A hierarchical relationship is a special type of relationship that is related only to the user object in Salesforce. It allows users to use a lookup field to associate one user with another that does not directly or indirectly refer to itself. For example, you can create a custom hierarchical relationship field to store each user’s direct manager.

It is a type of advanced relationship that is on top of the parent-child relationship, which includes the master-detail and lookup relationships.

Salesforce allows users to create customized fields that can be used to calculate and summarize data. Two types of such fields are the formula field and the roll-up summary field.

A formula field is a read-only field that derives its value from a formula expression that you define. A formula can reference fields from the same object, related parent object, and other objects. They can be created on standard or custom objects, and fields can be of different data types. Some common use cases for formula fields include but are not limited to

•\ Calculating of Total Price: You can create a formula field that multiplies the quantity and unit price fields to calculate the total price of a product in an Opportunity or Quote record.

•\ Time Duration Calculation: A formula field can determine the contact duration based on their completion and the current date.

•\ Concatenating Text: You can create a formula field to combine text from multiple fields, such as creating a full name field by combining first name and last name fields.

•\ Conditional Logic: Formulas can include IF statements and logical operators to display different values based on certain conditions.

A roll-up summary field is used to calculate the COUNT, SUM, MIN, or MAX of fields values in the child records of a master-detail relationship. Roll-up summary fields can only be created on the master object of a master-detail relationship to summarize fields on the child object that are themselves not formula fields or fields that reference other objects. Formula fields that reference other objects can’t be summarized in roll-up summary fields.

Both these field types are useful in simplifying data entry and automating calculations in Salesforce.

To create formula or roll-up summary fields, access the Object Manager from Setup and navigate to the object you wish to create the formula field on. Then, select Fields and Relationships on the left menu, and click the button New.