0% found this document useful (0 votes)
49 views

Ranjith Krishnan: Session 8

The document discusses custom formula fields in Salesforce. It explains that formulas derive their value from defined expressions and can return various data types. Formulas can include literal values, field references, functions, operators, and comments. The document provides examples of formulas that use operators like IF, AND, OR to return text or number values based on field criteria. It also discusses global data prefixes and predefined functions like NOW(), TEXT() that can be used in formulas.

Uploaded by

Nitish Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Ranjith Krishnan: Session 8

The document discusses custom formula fields in Salesforce. It explains that formulas derive their value from defined expressions and can return various data types. Formulas can include literal values, field references, functions, operators, and comments. The document provides examples of formulas that use operators like IF, AND, OR to return text or number values based on field criteria. It also discusses global data prefixes and predefined functions like NOW(), TEXT() that can be used in formulas.

Uploaded by

Nitish Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Ranjith Krishnan

SESSION 8
About Custom Formula Type
1. Formula is one of the data type of the field.
2. This is used to derive its value from a formula expression you define.

Literal Value The hardcoded text string or number in the formula.


For example, if there is a formula to calculate bonus of employees where the percentage
of bonus is always 20% from the salary field (Salary__c).

Salary__c *0.20

Here the number 0.20 is hardcoded that would never change in the calculation.
Field This denotes the standard or custom field of object that is referred in formula. Salary__c
Reference is a custom field reference.
Field from related object can also be referred in below format.
If standard object and standard field. ObjectName.FieldName

If custom object and custom field.


ObjectName__r.FieldName
Function System defined formula such as ISBLANK(value), TODAY() etc.,. Some required input from
user such as ISBLANK(value), ISCHANGED(value) etc., and some of them do not require
any value like TODAY() which return current date always.
Operator Used to specify the types of calculation to perform
Comment Use forward slash followed by an asterisk (/*) to being the comment and ends with an
asterisk followed by a forward slash (*/).

Example
/*this is a comment in formula*/

3. Every formula in the salesforce returns the result in any of the below format
a. Checkbox
b. Currency
c. Date
d. DateTime
e. Number
f. Text
g .Percent

4. Steps to create the formula.


Setup
|---Build
|---Create
|---Object
|--- Custom Fields & Relationships
|--- New

Step 1: Choose the field type as “Formula”


Step 2: Choose output type (return type of the formula)
Step 3: Create the formula here

Ranjith Krishnan
[email protected]
1
Ranjith Krishnan
Note: If required, select the object field from the option “Insert Field”.
Choose the “Advanced Formula” to select get the list of predefined functions.

Step 4: Give the field level security


Step 5: Add the field to the layouts.

5. Global Data:
a. Data which remains constant throughout the application is called global data
b. All global objects are prefixed with '$' sign
Example:
$Organization
$User
$UserRole
$System
$Api

Formula Example:
1. Object : Training__c
Field Name : Course Duration
Return Type : Number

Formula Editor:
End_DateTime__c - Start_DateTime__c

2. Object : Training__c
Field Name : Total Hours
Return Type : Number

Formula Editor:
Course_Duration__c * Hours per Day

Ranjith Krishnan
[email protected]
2
Ranjith Krishnan
Formula can also be built by using pre-defined functions provided by Salesforce as follows.
PRE-DEFINED FUNCTIONS IN SALESFORCE:
IF(Logic Condition, return_value_if_true, return_value_if_false)
Where
Logic Condition: This is a logical check which would return true or false.

Example:
1. If Fee__c = 0, then return the text ‘is a Free Course’ or ‘is a Paid Course’,
Object : Training__c
Field Name : This Course
Return Type : Text

Formula Editor:
IF (Fee__c = 0,’ is a Free Course',’ is a Paid Course’)

2. If No_of_Seats__c <= 50, then return the text ‘'Seats are available' else 'Seats are filled'’.
Object : Training__c
Field Name : Availability
Return Type : Text

Formula Editor:
IF(No_of_Seats__c < 50, 'Seats are available', 'Seats are filled')

3. Calculate Bonus
Object : Employee__c
Field Name : Bonus
Return Type : Percent

If Salary = 5000, give bonus 0.20


If Salary = 7500, give bonus 0.15
if Salary = 10000, give bonus 0.10
For any other salary, give bonus 0.

This is an example to handle multiple condition using nested IF.


Formula Editor:
IF(Salary__C = 5000, 0.20,
IF(Salary__c = 7500, 0.15,
IF(Salay__c = 10000, 0.10, 0)))

AND (cond1, cond2,....), This will return true if all the arguments (conditions) are true.
NOW() this returns current date time.

This formula returns small if price of the product < 1nd Quantity is lesser than 5
IF(AND(Price<100,Quantity<5),"Small", null)

Use this operator ‘&&’ as an alternative to the logical function AND.


IF((Price<100 && Quantity<5),"Small", null)

Ranjith Krishnan
[email protected]
3
Ranjith Krishnan
Example:
1. If Course had already started and the course type is online, then provide discount 10%
Object : Training__c
Field Name : Discount__c
Return Type : Percent

Step 1: Start_DateTime__c > NOW() and


Step 2: Online_Type__c = TRUE
Step 3: Combine the Step 1 and Step 2
AND(Start_DateTime__c > NOW() , Online_Type__c)
Note: Online_Type__C is a checkbox field and hence can be referred without explicit Boolean check as
“Online_Type__c = true’

Formula Editor:
IF( AND(Start_DateTime__c > NOW() , Online_Type__c ), 0.10,0)

The above formula can also be written as


IF(Start_DateTime__c > NOW() && Online_Type__c, 0.10,0)

2. Show the employee is eligible for bonus only if they are in-service and years of experience > 1.
Object : Employee__c
Field Name : Bonus Eligibility?
Return Type : CheckBox

The Status__c is a picklist type which cannot be used directly referred as Status__c = ‘In Service’ like a text
field. Instead use ay of the below function
TEXT() - this will convert value to text. Eg., TEXT(Status__c) = ‘In Service’
ISPICKVAL(Field,’Literal’) => returns true if field is equal to literal given.
Eg., ISPICKVAL(Status__c,’In Service’)

AND(TEXT(Status__c) = ‘In Service’, Years_of_Exp__c > 1)

Formula Editor
IF(AND(TEXT(Status__c) = ‘In Service’, Years_of_Exp__c > 1),True, False)

OR(Cond1, Cond2, Cond3,,,) This function returns true if any of the argument is true.
Example:
1. If Years_of_Experience__c > 10 or Salary__c > 100000 then display as ‘Critical Employee’
Object : Employee__c
Field Name : Priority
Return Type : Text

Formula Editor
IF(OR(Years_of_Experience__c > 10 , Salary__c > 100000), ‘Critical Employee’, NULL)

Ranjith Krishnan
[email protected]
4
Ranjith Krishnan

Use this operator ‘||’ as an alternative to the logical function AND.


IF((Years_of_Experience__c > 10 || Salary__c > 100000), ‘Critical Employee’, NULL)

2. This formula returns ‘Fast Track Course’ if Course Duration is between 1 and 30.
IF(OR(Course_Duration__c > 1, Course_Duration__c < 30),’Fast Track Course’, ’Regular Course’)

& is used to concatenate the text


TEXT() - this will convert value to text.
1. Display the text to show the course name and when it starts
Eg., The course 'SFDC Admin' begins at 05/07/2019
Object : Training__c
Field Name : Notification
Return Type : Text

Formula Editor:
‘The Course ' & Course_Name__c & ' begins at ' & TEXT(Start_DateTime__c)

Explanation:
The field Start_DateTime__c is not a string and hence cannot be concatenated with another string. So this must
be converted to text using TEXT() function.

ISBLANK(arg) => Returns true if the argument does not have any value. This function will support both number
as well as text type of argument.
1. Bonus with respect to salary range
If Salary > 5000 and < 15000, then provide bonus 10%
If Salary > 15000 and < 30000, then provide bonus 5%
If Salary < 5000, then bonus 15%.

Formula:
IF(OR(Salary > 5000, Salary < 15000), 0.10,
IF(OR(Salary > 15000,< 30000), 0.05,
IF(OR(Salary < 5000, 0.15), 0)))

2. Calculate the lead score with respect to number of contact information available as below
Object : Training__c
Field Name : Lead Score
Return Type : Number
Condition : FieldName Blank/Null Lead Score Points
Phone 0 10
Email 0 10
FirstName 0 10
AnnualRevenue >0 20
Formula Editor:
IF( ISBLANK(Phone) ,0 ,10 ) +
IF( ISBLANk(Email), 0, 10 ) +
IF( ISBLANK(FirstName),0,10) +
IF( AnnualRevenue > 0 , 20 ,0)

Ranjith Krishnan
[email protected]
5
Ranjith Krishnan

NOT(field1/expr) -> return true if argument is resolved to false and vice versa
Example:
i. Salary = 6000
NOT( Salary > 5000) => False

ii. Name => if field is not blank, then return true


NOT(ISBLANK(Name))

CASE() - Checks an expression against a series of values. If the expression compares equal to any value, the
corresponding result is returned
Syntax: CASE(Expression,
value1, return_value1,
value2, return_value2,
value3, return_value3,
return_value4)

Example:
This formula returns how much travelling allowance is allowed with respect to trainer’s city
Formula Editor:
CASE(City,
‘Hyderabad’ , 300,
‘Noida’, 500,
‘Chennai’, 1000,
100)

IMAGE(URL,ErrorMessage) : This will print the image ,if the url is not working then it throws error message.
Note: if you want print image using formulas we have to take return type of the formula as Text.
Object : Lead
Field Name : Lead Score
Formulae Field : Lead Rating
Return Type : Text
Condition : LeadScore Rating
50 5 star
40 4 Star
30 3 Star
20 2 star
10 1 star
0 0 star
Formula Editor:
IMAGE ( CASE(Lead_Score__c ,
50,'img/samples/stars_500.gif',
40, 'img/samples/stars_400.gif',
30, 'img/samples/stars_300.gif',
20, 'img/samples/stars_200.gif',
10, 'img/samples/stars_100.gif',
'img/samples/stars_000.gif'),
'Image not Found' )

Ranjith Krishnan
[email protected]
6
Ranjith Krishnan

DATE RELATED FUNCTION


TODAY () : This will return today's date .
NOW() : This will give you current date and time .
DATE(YYYY,MM,DD) : This will return the instance of the date
DATE(2010,10,23)
DATE(2019,2,23 )
DAY(Date) : This will return you the day in the month .
DAY(TODAY() ) : 8
DAY(Date(2019,2,23) ) : 23
MONTH(Date) :This will return the month in the date
MONTH(TODAY()) : 06
MONTH(DATE(2019,2,23) ) :2
YEAR(Date) : This will return the year in the given date.
YEAR(TODAY()) : 2019
DATEVALUE(expression) :This will return the date in the expression
DATEVALUE(NOW() ): TODAY

UseCase:
Object : Account
Formula Field : Modified before
Condition : Calculate how many days back record was last modified.
LastModifiedDate : DateTime
TODAY() - DATEVALUE(LastModifedDate)

Ranjith Krishnan
[email protected]
7

You might also like