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.

Relationships:

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:

  1. Number
  2. Currency
  3. Percent
  4. Date
  5. Date/Time
  6. Checkbox
  7. Text

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

  1. Open Accounts page from setup
  2. Create New Field
  3. Select Formula and click Next
  4. In Field Label, type Label for the formula field. (The Field Name will get populate automatically)
  5. Select the type of data the formula should return. (E.g. Currency, Date, Text etc.)
  6. Click Next. (Formula Editor will get open.)

Create Date Formula Field

Choose field type as formula

 New Custom Field

 Write Label to Field Label and select return type as Date

Date formula field in salesforce

 Click next and Write formula

 Formula Area

 Set field Level security

 

After saving you can view the newly created field in the list

 

 Formula applied for Date Field

Create Text Formula Field

Follow the above steps and write following formula

To find the state name based on initials

Create Number Formula Field
To check number must be even

 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.

Tutorial Index

  1. Introduction to Cloud Computing (Salesforce.com and Force.com)
  2. Overview of Database Concepts (Salesforce.com)
  3. Introduction to Force.com
  4. Building Salesforce Custom App and Objects
  5. Object Relationships and Formula Field in Salesforce
  6. Salesforce Security Model and Overview

Add a Comment

Your email address will not be published. Required fields are marked *