Académique Documents
Professionnel Documents
Culture Documents
Salesforce, Spring 16
@salesforcedocs
Last updated: January 22, 2016
Copyright 20002016 salesforce.com, inc. All rights reserved. Salesforce is a registered trademark of salesforce.com, inc.,
as are other names and marks. Other marks appearing herein may be trademarks of their respective owners.
CONTENTS
Tips for Reducing Formula Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Reducing the Length of Your Formula . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Reducing Your Formulas Compile Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Need more help? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Because you repeatedly reference YEAR(Membership_Start_Date__c), you can move that into a formula field called Start
Year. You can also move MONTH(Membership_Start_Date__c) into its own formula field.
Then the main formula becomes:
IF(
Start_Month__c < 10,
DATE( Start_Year__c, 12, 31),
DATE( Start_Year__c + 1, 12, 31)
)
Youre now down to 89 characters from 146. Since there are edition-based limits on how many fields you can create for an object, take
them into account when figuring out how to break up a long formula. You might only want to create Start_Year__c in the example
above.
Keeping comments and field names as short as possible while still being descriptive can also help. You might want Membership Start
Date to be the label for your users, but you could conserve space by overriding the default API name Membership_Start_Date to
Start_Date or Mem_Start_Date. Override the default name when you create a custom field, rather than trying to change it later.
You can remove blank spaces and carriage returns, but this makes complex formulas harder to read, so consider that a last resort.
Using Date1__c in a formula adds 22 characters to the compile size each time its referenced. Date2__c adds 465 characters each
time. Lets look at the impact on this formula that generates a deadline two business days after a given date:
CASE( MOD( SomeDate__c 0, SomeDate__c + 1 + 2,
1, SomeDate__c + 2,
2, SomeDate__c + 2,
3, SomeDate__c + 2,
4, SomeDate__c + 2 + 2,
5, SomeDate__c + 2 + 2,
6, SomeDate__c + 2 + 2,
SomeDate__c
)
DATE( 1900, 1, 7 ), 7 ),
/* Sunday */
/* Monday */
/* Tuesday */
/* Wednesday */
/* Thursday */
/* Friday */
/* Saturday */
/* Default */
Replacing SomeDate__c in the above formula with Date1__c gives us a formula compile size of 487 characters. But replacing
SomeDate__c with Date2__c compiles to 4,474 characters! Most of that is the nine references to Date2__c: 9 * 465 = 4,185
characters.
DATE( 1900, 1, 7 ), 7 ),
/* Sunday */
/* Monday */
/* Tuesday */
/* Wednesday */
/* Thursday */
/* Friday */
/* Saturday */
/* Default */
The default value in this formula is SomeDate__c. But the value of MOD( Date__c - DATE( 1900, 1, 7 ), 7 )
is always 0 to 6, so the default value is never used. This formula could be rewritten as:
CASE( MOD( SomeDate__c 0, SomeDate__c + 1 + 2,
4, SomeDate__c + 2 + 2,
5, SomeDate__c + 2 + 2,
6, SomeDate__c + 2 + 2,
SomeDate__c + 2
)
DATE( 1900, 1, 7 ), 7 ),
/* Sunday */
/* Thursday */
/* Friday */
/* Saturday */
/* Default - Mon/Tues/Wed */
By making the Monday/Tuesday/Wednesday case the default (date + 2 days), we reduce the compile size to 360 characters for
Date1__c, and to 3,018 for Date2__c.
Use CASE() instead of nested OR() statements
The following formula returns the date of the last day of the month for a given date (assume February always has 28 days):
DATE(
YEAR( SomeDate__c ),
MONTH( SomeDate__c ),
IF(
OR(
MONTH( SomeDate__c
MONTH( SomeDate__c
MONTH( SomeDate__c
MONTH( SomeDate__c
),
30,
IF(
MONTH( SomeDate__c
28,
31
)
)
)
)
)
)
)
=
=
=
=
4,
6,
9,
11
) = 2,
The formula first checks for months with 30 days, then February, and the remaining months are 31 days. It requires a nested IF()
function, which isnt very readable and compiles to 1069 characters for Date1__c and a whopping 7,271 characters for Date2__c!
Why? Because the formula references the date seven times. Compare that to this revised version of the formula:
DATE(
YEAR( SomeDate__c ),
MONTH( SomeDate__c ),
CASE(
MONTH( SomeDate__c ),
2, 28,
4, 30,
6, 30,
9, 30,
11, 30,
31
)
)
Not only is this easier to read, the formula compiles to only 645 characters for Date1__c and 3,309 characters for Date2__c,
and it now references the date three times instead of seven.
Rearrange the logic
This example came from the Salesforce Answers community. A picklist stores the name of an agent responsible for an opportunity.
The formula calculates a commission, based on Base Commission value and a multiplier. But because Base_Commission__c
is referenced in each condition of the CASE() statement, the formula exceeds the compile size.
CASE( Agent__c,
"John", Base_Commission__c * 2,
"Jane", Base_Commission__c * 6,
/* Repeat for many other agents */
Base_Commission__c
)
To fix this, move Base_Commission__c outside the CASE() function. The formula can be rewritten as:
Base_Commission__c * CASE( Agent__c,
"John", 2,
"Jane", 6,
/* Repeat for many other agents */
1
)
Even if Base Commission is only a Currency field and not a formula itself, referencing it once instead of multiple times greatly reduces
the formula compile size.
As another example, lets try this with our business days formula:
SomeDate__c
0, 1 + 2,
4, 2 + 2,
5, 2 + 2,
6, 2 + 2,
2
)
We have now further reduced the size to 188 characters for Date1__c and to 1,074 characters for Date2__c, which is almost
a quarter of the original formula size.
There are so many options in the picklist, the formula hits the compile size limit. But since the friendly name for the link is the same as
the picklist text, moving HYPERLINK() outside the CASE() statement allows it to be referenced just once:
HYPERLINK(
CASE( Airline__c,
"Airline 1", "http://airline1.com",
"Airline 2", "http://airline2.com",
/* Dozens of other airlines */
""
), /* CASE() generates the URL for the hyperlink */
TEXT(Airline__c) /* TEXT() generates the friendly name */
)
The revised formula adds only 38 characters to the compile size for each picklist option, instead of 133.
Added bonus: when you add new agents, you dont have to remember to change the formula. It just works.
This formula checks if the difference is a positive value, and if it is, several operations calculate values for days, hours, and minutes in
Diff__c and return a string such as 6 days 2 hours 11 minutes. This formula compiles to 2,547 characters. And it works fine when
Diff__c subtracts two simple Date/Time fields like this:
LastModifiedDate - CreatedDate
However, if Diff__c is a more complex formula, the elapsed time formula becomes too big to compile. Why? Count the number of
times Diff__c is referenced. Also, all those nested FLOOR() and ROUND() functions add up.
Theres a simpler way to think about the problem:
Number of days = DateTime1 - DateTime2
Number of hours = Number of days * 24
Number of minutes = Number of hours * 60
The MOD() function can really help. The modulus of the number of hours divided by 24 is the number of hours not accounted for by
days. The modulus of number of minutes divided by 60 is the number of minutes not accounted for by hours. So we can change the
formula to:
IF(
Diff__c > 0 ,
TEXT(FLOOR(Diff__c)) & " days "
& TEXT(FLOOR(MOD(Diff__c * 24, 24))) & " hours "
& TEXT(ROUND(MOD(Diff__c * 24 * 60, 60), 0)) &" minutes",
""
)
The new version of the formula compiles to 728 characters (down from 2,547), and because it only includes Diff__c four times, it
has room to accommodate more complex formulas in that field.
Heres another example. This validation rule formula returns true if an opportunitys Close Date is not in the current month
OR (
CloseDate < DATE( YEAR(TODAY()), MONTH(TODAY()), 1),
IF (
AND (
MONTH (TODAY() ) =1,
CloseDate > DATE( YEAR(TODAY() ), MONTH(TODAY() ), 31)),
true,
IF (
AND (
MONTH (TODAY() ) =2,
CloseDate > DATE( YEAR(TODAY()
true,
IF (
AND (
MONTH (TODAY() ) =3,
CloseDate > DATE( YEAR(TODAY()
true,
IF (
AND (
MONTH (TODAY() ) =4,
CloseDate > DATE( YEAR(TODAY()
true,
IF (
AND (
MONTH (TODAY() ) =5,
CloseDate > DATE( YEAR(TODAY()
true,
IF (
AND (
MONTH (TODAY() ) =6,
CloseDate > DATE( YEAR(TODAY()
true,
IF (
AND (
MONTH (TODAY() ) =7,
CloseDate > DATE( YEAR(TODAY()
true,
IF (
AND (
MONTH (TODAY() ) =8,
CloseDate > DATE( YEAR(TODAY()
true,
IF (
AND (
MONTH (TODAY() ) =9,
CloseDate > DATE( YEAR(TODAY()
true,
IF (
AND (
MONTH (TODAY() ) =10,
CloseDate > DATE( YEAR(TODAY()
true,
IF (
AND (
MONTH (TODAY() ) =11,
CloseDate > DATE( YEAR(TODAY()
true,
IF (
AND (
MONTH (TODAY() ) =12,
CloseDate > DATE( YEAR(TODAY()
), MONTH(TODAY() ), 28)),
), MONTH(TODAY() ), 31)),
), MONTH(TODAY() ), 30)),
), MONTH(TODAY() ), 31)),
), MONTH(TODAY() ), 30)),
), MONTH(TODAY() ), 31)),
), MONTH(TODAY() ), 31)),
), MONTH(TODAY() ), 30)),
), MONTH(TODAY() ), 31)),
), MONTH(TODAY() ), 30)),
), MONTH(TODAY() ), 31)),
true, false
)))))))))))))
This complex check determines whether the Close Date is earlier than the first day of the current month, or later than the last day of this
month. But actually, the day doesnt matter at all: if the month and year of the Close Date are the same as the month and year of the
current month, then its the same month. So we can rewrite the formula as:
NOT(
AND(
MONTH( CloseDate ) = MONTH( TODAY() ),
YEAR( CloseDate ) = YEAR( TODAY() )
)
)
This new version is much more readable, and only compiles to 200 characters compared to more than 3,000 for the original formula.
)
)
)
)
=
=
=
=
4,
6,
9,
11
) = 2,
Another way we could have solved this is by making Date2__c a custom Date field instead of a formula field, and creating a workflow
rule and field update to set Date2__c to the value of the formula it previously contained
DATE( YEAR( Date1__c ), MONTH( Date1__c ), DAY( Date1__c ) )
Now the size of Date2__c is the same as Date1__c, because it contains a simple date, and the compile size of the
last-day-of-the-month formula is 1069 characters instead of over 7,000.
10
11