Salesforce Tutorial# 5: Object Relationships and Formula Field in Salesforce
In this 5th article in our Salesforce learning journey, we will have some more insights on Salesforce Object Relationships. We will also see how Formula Field in Salesforce is defined.
Salesforce Objects Relationships:
Relationships are used to connect Salesforces objects with each other. We can define three relationships on salesforce objects based on their scope of data sharing, deleting, accessing, etc. To read more in this please refer our previous article Overview of Database Concepts (Salesforce.com)
Differentiation between One-to-Many and Many-to-Many
The basic difference between these two relationships is One to Many relationships is defined using master-detail (lookup relationships) whereas Many to Many relationships is always defined on two master objects.
So that many-to-many relationship can have links like each record of one master object linked to multiple records from another master object or vice versa.
Lookup is a One-to-Many type of relationship in which two objects are linked together but has no effect on deletion or security. Whereas in normal One-to-Many relationship when a record of the master object is deleted, its related detail records are also deleted.
Introduction of Primary-Keys & Foreign-Keys (Conceptual Knowledge)
A Primary key is a column or set of columns that uniquely identifies the record in the table. When a primary key is defined using multiple columns, the data from each column is used to determine whether a record/row is unique. When we defined any column or columns as primary key, there are some constraints that should be satisfied by the primary key that is column value should not be null (Null Constrains) and data should be unique (Unique Constrains.)
A Foreign key is a set of one or more columns in a table that refers to the primary key in another table. Foreign key does not necessarily always point to the primary key, but the basic idea behind foreign key is that any column that is unique to identify that record/row.
Roll-up Summary Field
A roll-up summary field is a custom object field which calculates values from related records or the records in a related list. The roll-up summary field is created to display a value in a master record based on the values of fields in a detailed record. A roll-up summary can be defined on the Master-Detail relationship only.
Formula & Cross Object Formula Field in Salesforce:
Formula Field is a read-only field whose value is evaluated from the formula or expression defined by us. We can define the formula field on both standards as well as custom objects. Any change in expression or formula will automatically update the value of the formula field.
We can define the formula field for 7 types:
- Number
- Currency
- Percent
- Date
- Date/Time
- Checkbox
- Text
Cross Object Formula Fields are the fields that are based on a Cross-object formula.
The cross object formulas are defined on two related objects and reference merge fields on those objects.
This field is built on child objects. It is also known as Reference formula. We can apply cross-object fields up to 10 levels. Cross-object formulas can reference merge fields from a master object as well as if the field is at a detailed side then it can merge field from a master-detail relationship. Cross Objects formula can also be defined on lookup relationships.
Steps to Create Date, Text and number formula field in Salesforce
Steps to Create Formula Field
- Open Accounts page from setup
- Create New Field
- Select Formula and click Next
- In Field Label, type Label for the formula field. (The Field Name will get populate automatically)
- Select the type of data the formula should return. (E.g. Currency, Date, Text etc.)
- Click Next. (Formula Editor will get open.)
Create Date Formula Field
Choose field type as a formula
Write Label to Field Label and select return type as Date
Click next and Write formula
Set field Level security
After saving you can view the newly created field in the list
Formula applied for Date Field
1 2 3 4 5 6 7 |
If ( MONTH (LastDate_C) = 12, DATE (YEAR (LastDate_C), 12, 31), DATE (YEAR (LastDate_C), MONTH (LastDate_C) + 1, 1) -1) |
Create Text Formula Field
Follow the above steps and write the following formula
To find the state name based on initials
1 2 3 4 5 6 7 8 9 10 11 |
IF (ISBLANK (State), "None", IF (CONTAINS ("MH", State), "Maharashtra", IF (CONTAINS ("MP", State), "Madhya Pradesh", IF (CONTAINS ("GJ", Sate), "Gujarat", IF (CONTAINS ("AP", State), "AndhraPradesh", IF (CONTAINS ("HP", State), "Himachal Pradesh", “Other”)))))) |
Create Number Formula Field
To check number must be even
1 |
<strong> </strong>OR (NumberInputed< 0, MOD (NumberInputed, 2) <>0) |
Now after this article, you must be pretty confident in your knowledge of Formula filed in Salesforce. Also, you must have understood the object relationship very well.
Tutorial Index
- Introduction to Cloud Computing (Salesforce.com and Force.com)
- Overview of Database Concepts (Salesforce.com)
- Introduction to Force.com
- Building Salesforce Custom App and Objects
- Object Relationships and Formula Field in Salesforce
- Salesforce Security Model and Overview
- Automation in Salesforce
- Approval Process in Salesforce
- Introduction to SOQL and SOSL
- Introduction to Apex
- Salesforce Data Management
- Visualforce MVC Architecture on Cloud
- Salesforce Reports and Dashboards
- Building a Visualforce (Custom) Page for the Salesforce App
- Salesforce Sandbox and Overview of Force.com capabilities
- Learning Apex and Deployment Tools