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.
Relationships are used to connect Salesforces objects with each other. We can define three relationships on salesforce objects based on their scope about 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 relationship is defined using master-detail (lookup relationships) where as Many to Many relationship 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. Where as 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 constrains which should be satisfied by 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 necessary to be always point to primary key, but the basic idea behind foreign key is that any column which is unique to identify that record/row.
Roll-up Summery Field
A roll-up summary field is custom object field which calculates values from related records or the records in a related list. Roll-up summary field is created to display a value in a master record based on the values of fields in a detail record. Roll-up summary can be defined on 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 formula field on both standard as well as custom objects. Any change in expression or formula will automatically update the value of formula field.
We can define formula field for 7 types:
Cross Object Formula Fields are the fields which are based on a Cross-object formulas.
The cross object formulas are defined on two related objects and reference merge fields on those objects.
This field is built on child object. 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 field is at detail 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 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
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 following formula
To find the state name based on initials
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
<strong> </strong>OR (NumberInputed< 0, MOD (NumberInputed, 2) <>0)
Now after this article you must be pretty confidant on your knowledge on Formula filed in Salesforce. Also you must have understood the object relationship very well.
If you need to refer previous articles please see index below.
- 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