Webinar du 14 mai 2020 du groupe Salesforce Paris Women in Tech. Plus d'info :
https://trailblazercommunitygroups.com/events/details/salesforce-paris-france-women-in-tech-group-presents-les-formules-et-moi-ca-fait-3/
#sfpariswit
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Les formules et moi, ça fait 3!
1.
2. Vincent Finet
Senior Program Architect @ Salesforce
twitter.com/VinceFINET
linkedin.com/in/vincentfinet
trailhead.com/me/vincefinet
3. I II III
Where do
we found
formulas in
Salesforce?
How will
you build a
working
formula?
Examples
IV
Resources
around
formulas
Expert
Starter
Curious
Middle
5. Where do we found formulas in Salesforce?
Location #1: Formula Fields
● Characteristics
○ At the Object level, like any other field
○ 8 different types
○ 1 formula expression
○ Value calculated on the fly (= not stored)
○ Cannot be indexed (unless deterministic + SF case)
○ Treat blank behavior (defined for each field)
● Restrictions:
○ Max number of characters: 3900 characters
○ Max formula size upon save: 4000 bytes
○ Max compiled size: 5000 bytes
○ Can’t reference: long text area, encrypted or description.
○ Cross-Object Formulas (across all fields of the same
object): 15
6. Where do we found formulas in Salesforce?
Location #2: Validation Rules
● Characteristics
○ At the Object level
○ Data quality concerns
○ Can be activated or inactivated
○ Raise an error if a condition is met
○ Save is blocked if any error
○ Error message is raised on the top or on a field
● Restrictions:
○ Little mental gym when writing the formula
because you specify the error condition and not
the success condition
○ Launched only once (for example: not after a
workflow field update)
7. Where do we found formulas in Salesforce?
Location #3: Workflows
● Characteristics
○ At the Object level
○ Automation concerns
○ Workflow rule: entering formula criteria
○ Workflow field update: new value formula
● Restrictions:
○ Old school automation but still robust!
8. Where do we found formulas in Salesforce?
Location #4: Process builders
● Characteristics
○ At the Object level
○ Automation concerns
○ Criteria with a boolean formula
● Restrictions:
○ Duplicate formula (leading to errors) if
you have CASE like criterias
9. Where do we found formulas in Salesforce?
Location #5: Reports
● For a long time, formula in reports were VERY limited, so we had to create
formula field to have formulas in reports
● Use case #1: Cross-Object formula
○ Limit of 15 per object on Formula Fields, WF Rules, WF Field Updates, Approval Processes,
Validation Rules, Assignment Rules, Escalation Rules, and Auto-Response Rules
● Use case #2: Nice icons on report for scoring
○ Use of IMAGE() with standard icons
● Use case #3: “Power Of One”
11. How will you build a working formula?
The basics
● Literal value
= 'test' = "test"
= 0.4
= TRUE = true = TrUe
● Operator
= FieldA__c & 'ooo'
= (FieldA__c + 8)^2 / 100
● Functions
= AND(aa = 0, bb, ...)
= TODAY()
= ROUND(FieldA__c*0.02, 2)
= HYPERLINK('/'+$User.Id)
= IMAGE('/img/samples/flag_red.gif', 'red')
● Field reference
= FieldA__c
= FieldA__r.Name
= Parent.Parent.Parent.Name
= $CustomMetadata.MyCMDT
= $Label.MyCustomLabel
= $Organization.Id
= $Profile.Name
= $UserRole.DeveloperName
= $User.Id
● Comment
= /* Comment here */
= ROUND( /*:)*/ FieldA__c, /*;|*/ 3)
12. How will you build a working formula?
Best practices
● Multiple lines
● Indent
● Functions and keywords in UPPERCASE
● Add comments in the formula (if needed)
● Don’t hard code values, use custom labels instead
● Think reusable → multiple formulas instead of one
● Think maintenance → make the formula readable
● Think efficiency → formula can have an impact on performance
13. How will you build a working formula?
Family functions: Logical
● AND($User.ByP__c, a='ui', b=c, ...)
○ All statement should be true
○ Can be substituted with operator &&
● OR(a='ui', b=c, ...)
○ At least one statement should be true
○ Can be substituted with operator ||
● NOT(a='ui')
○ Returns the logical opposite
○ Can be substituted with operator !
Tips:
NOT(AND(A, B)) = OR(NOT(A), NOT(B)
OR(A, B) = NOT(AND(NOT(A), NOT(B)))
● IF(a='01', 'Yes', 'No')
○ Classical if statement
● CASE (a, '01'='One', ‘02’=‘Two', ... , 'N/A')
○ Nice substitute to multiple IFs
● BLANKVALUE(a, 'N/A')
● NULLVALUE(a, 'N/A')
● ISBLANK(a)
● ISNULL(a)
● ISNUMBER(a)
14. How will you build a working formula?
Family functions: Text
● BR()
● HYPERLINK('/'+$User.Id, 'Me')
● IMAGE('/img/start.png', 'start')
● INCLUDES(MultiPckLst__c, '01')
● ISPICKVAL(PckLst__c, '01')
● TEXT(n) text transform
● VALUE(t) number transform
● CASESAFEID(id15)
● LEN(t) length of a text
● LEFT(a, 3) MID(a, 1, 2) RIGHT(a, 1)
● LOWER(t) UPPER(t) case transform
● LPAD(t, 10, '0') left padding
● RPAD(t, 8, 'X') right padding
● TRIM(t) removes spaces around
● SUBSTITUTE(t, 'old', 'new')
● CONTAINS('oiu', 'o') = 1
● FIND('d', 'abcdef', 6) = 0
15. How will you build a working formula?
Family functions: Math
● ABS(-1) = 1
● CEILING(1.125) = 2
● FLOOR(1.525) = 1
● ROUND(47.4687, 2) = 47.47
● MAX(a, b, …)
● MIN(a, b, …)
● MOD(47, 3) = 2
● EXP(2) = e^2 = 7.38905609893065
● SQRT(4) = 2
● LN(19) = 2.9444389791664403
● LOG(10) = 1
16. How will you build a working formula?
Family functions: Date and Time
● WEEKDAY(date)
○ 1 = Sunday
● DAY(date)
● MONTH(date)
● YEAR(date)
● ADDMONTHS(date, number)
○ Takes care of February
○ Not just adding 30 days
● DATETIMEVALUE
● DATEVALUE
● TIMEVALUE
● NOW
○ Returns today’s datetime
● TODAY
○ Returns today’s date
● TIMENOW
○ Returns today’s time
● DATE(year, month, day)
● HOUR(datetime)
● MINUTE(datetime)
● SECOND(datetime)
● MILLISECOND(datetime)
17. How will you build a working formula?
Family functions: Advanced
● ISCHANGED(field)
● PRIORVALUE(field)
● ISNEW()
● ISCLONE()
● REGEX(field, '(.*test.*){3,}')
○ Validate complex formatting
● VLOOKUP(lookup.fieldA, lookup.fieldB,
fieldC)
○ Look in the table “lookup”
○ For record where lookup.fieldB = fieldC
○ Return the value of fieldA of the record
○ Limit of 10 per object (can be raised to
20 max with a case to SF support)
● $ObjectType.Obj.Fields.Fld
19. Examples
Using CONTAINS instead of multiple IFs or CASE
● Instead of a very long CASE(),
here we use CONTAINS with a
little trick.
● All values that have the same
result, are concatenated with a
separator “:”
● In this case, we do not indent to
have all the contains aligned but
we have multiple “)” at the end
20. Examples
Ultimate parent account
● Using Parent.Name to get the
Account FATHER’s name
● Using Parent.Parent.Name to
get the Account GRAND FATHER’s
● ETC…
● In formula no errors if lookups
are empty or null
● Starting at the 5th generation
● BLANKVALUE simulates that we
continue to check lower or not
● Limit: not up to 10 “generations”
21. Examples
Calculate “age”
● Subtract the current datetime
(with NOW) and a datetime field
(in the future)
● Result is the number of days.
● The result can have decimals
because time may not be aligned
22. Examples
Instead of a button, have a dynamic link
● Use HYPERLINK to create a link
● The URL of the link will contain a
static value and a dynamic
parameter
23. Examples
Check information on current User
● We use MAX to get fast the
maximum value over three values
● $User is used to get the value of
a custom field on the record of
the current user
Tips:
$User is also interesting to have ByPass for
Validation Rule or Workflow on the User level
24. Examples
Merging multiple fields into one with preference
● Using a picklist on the contact to
store the prefered way to call the
contact on the CTI
● We use CASE of course
● Concatenate with “&” a little
prefix and the prefered phone
value
25. Examples
Is this year a leaping year?
● Using MOD to check if an integer
is a multiple of 400, 100 or 4.
● Also YEAR on a date
26. Examples
Images to have a greater impact
● Using IMAGE to return an HTML
code of an image with URL and
title (for accessibility)
● The URL is dynamic depending on
the value of a field
27. Examples
You asked for it: Add 3 business days to today
● If today is Monday → next Thu. → +3
● If today is Tuesday → next Fri. → +3
● If today is Wednesday → next Mon. → +3+2
● If today is Thursday → next Tue. → +3+2
● If today is Friday → next Wed. → +3+2
● If today is Saturday → next Wed. → +3+1
● If today is Sunday → next Wed. → +3
Proposal #1:
/* In three days from Today */
TODAY() + 3 +
/* Cases where we have to skip days */
CASE(WEEKDAY(TODAY()),
/* Wednesday (4) */ 4, 2,
/* Thursday (5) */ 5, 2,
/* Friday (6) */ 6, 2,
/* Saturday (7) */ 7, 1,
/* Other */ 0
)
Proposal #2:
/* In three days from Today */
TODAY() + 3 +
/* If Saturday, we skip Sunday */
IF(WEEKDAY(TODAY()) =7, 1,
/* If Wed, Thu or Fri, we skip the WE */
IF(WEEKDAY(TODAY())>=4, 3,
/* If Sun, Mon or Tue, we skip nothing */
0))
WEEKDAY() Meaning... Expected
1 Sunday TODAY() + 3
2 Monday TODAY() + 3
3 Tuesday TODAY() + 3
4 Wednesday TODAY() + 3 + 2
5 Thursday TODAY() + 3 + 2
6 Friday TODAY() + 3 + 2
7 Saturday TODAY() + 3 + 1
28. Examples
You asked for it: Data quality Scoring
● Initial Score = 0
● If Industry picklist is set → + 1
● If Phone not blank → +1
● If Email not blank → +1
● If Title not blank → +1
● So minimum score is 0
● And maximum score is 4
Field “Score__c”:
IF(ISBLANK(TEXT(Account.Industry)), 0, 1) +
IF(ISBLANK(Phone), 0, 1) +
IF(ISBLANK(Email), 0, 1) +
IF(ISBLANK(Title), 0, 1)
Field “ScoreImage__c”:
IMAGE(
'/img/samples/stars_' &
TEXT(
FLOOR(
Score__c / VALUE($Label.SCORE_MAX) * 5
)
) &
'00.gif',
TEXT(Score__c) & '/' & $Label.SCORE_MAX
)
29. Examples
You asked for it: Fiscal Year condition
● Fiscal Year is from 1 August to 31 July
● What is the FY for a specific date on a
record?
● Examples:
○ 5 August 2020 → “2020”
○ 10 September 2019 → “2019”
○ 20 July 2021 → “2020”
○ 31 July 2029 → “2028”
○ 1 August 1847 → “1847”
Proposal #1
/* First date of FY is 1 August (08/01) */
TEXT(
YEAR(Closed_Date__c) +
IF(MONTH(Closed_Date__c) >= 08,
1, /* Date is in the current FY */
0 /* Date is in the previous FY */
)
)
Proposal #2
TEXT(
YEAR(Closed_Date__c) +
IF(
Closed_Date__c >=
/* First date of FY is 1 August (08/01) */
DATE(YEAR(Closed_Date__c), 08, 01),
1, /* Date is in the current FY */
0 /* Date is in the previous FY */
)
31. Resources around formulas
Trailhead (part 1)
● Beginners:
○ https://trailhead.salesforce.com/content/learn/modules/point_click_business_logic?trail_id=forc
e_com_dev_beginner
● Advanced:
○ https://trailhead.salesforce.com/en/content/learn/modules/advanced_formulas
● Projects:
○ https://trailhead.salesforce.com/en/content/learn/projects/customize-a-salesforce-object
○ https://trailhead.salesforce.com/content/learn/projects/improve-data-quality-for-a-cleaning-
supply-app?trail_id=learn-admin-essentials
● Formula in Reports:
○ https://trailhead.salesforce.com/en/content/learn/projects/rd-summary-formulas
32. Resources around formulas
Trailhead (part 2)
● Formula in Flows:
○ https://trailhead.salesforce.com/en/content/learn/modules/flow-builder
● Formula in Workflows:
○ https://trailhead.salesforce.com/en/content/learn/modules/workflow_migration
● Formulas and Limits (Cross Object Formulas):
○ https://trailhead.salesforce.com/en/content/learn/modules/process-design-without-
limits/process-design-without-limits-object
● Preparing DEV I includes Formulas as well :
○ https://trailhead.salesforce.com/content/learn/modules/platform-developer-i-certification-prep-
fundamentals-and-database-modeling/pd1-1-data-modeling-and-management
33. Resources around formulas
Great additional stuff (out of Trailhead)
● An awesome site to test your formulas: https://formulon.io/
● Need help on limits? Go to Formula Field limits and restrictions
● Ways to optimize you formulas: Tips to reduce your formulas (pdf)
● THE reference on success community: Steve Molis aka “Formula Ninja”
● Find the full Power of One concept
● More information about Cross Object Formula limits
● Full use case for the VLOOKUP method usage
● To raise a ticket to support for more VLOOKUP capability, go here
● Some Regular Expression examples
● For ALL formulas documentation please go here