Ranjith Krishnan: Session 8
Ranjith Krishnan: Session 8
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.
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
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
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.
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
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)
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
Formula Editor:
IF( AND(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’)
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
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’)
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
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
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