Building formulas – best practices

Some best practices and methods to improve the creation and maintenance of formula fields are as follows:

  • Formatting with carriage returns and spacing
  • Commenting

Formatting with carriage returns and spacing

Take a look at the following formula:

Sales Tax (Percent) = 
IF(TEXT(Account.Market__c) = "US", IF(TEXT(Account.State__c) = "California", 0.0925, IF(TEXT(Account.State__c) = "Nevada", 0.081, IF(TEXT(Account.State__c) = "Utah", 0.0835, 0) )) , 0)

To improve the readability of formula fields, you can add spacing and carriage returns. The preceding formula can be made far easier to understand simply by adding spaces and carriage returns, as follows:

Sales Tax (Percent) = 
  IF( TEXT(Account.Market__c) = "US", 
    IF(TEXT(Account.State__c) = "California", 0.0925, 
    IF(TEXT(Account.State__c) = "Nevada", 0.081, 
    IF(TEXT(Account.State__c) = "Utah", 0.0835, 0) )) 
  , 0)

Commenting

Salesforce CRM allows you to put comments in your formulas. These are sections of text that are not run as part of the formula and are typically used to make notes about the formula code, especially if it is particularly complicated. Comments must start with a forward slash followed by an asterisk (/*) and must finish with an asterisk followed by a forward slash (*/).

Comments are useful for explaining specific parts of a formula to other system administrators who're viewing the formula definition. Look at the following code block as an example:

Sales Tax (Percent) = 
/* value only set for US opportunities */
  IF( TEXT(Account.Market__c) = "US", 
/* Check for the US State of the Account record and set accordingly */
    IF(TEXT(Account.State__c) = "California", 0.0925, 
    IF(TEXT(Account.State__c) = "Nevada", 0.081, 
    IF(TEXT(Account.State__c) = "Utah", 0.0835, 0) )) 
  ) 
  , 0)

Carefully using comments to prevent parts of the formula from being activated allows you to test and verify the syntax as you construct and iron out bugs in the formula. However, if you try to comment out the entire formula as syntax, an error is shown. Also, you will experience a syntax error if you try to place comments within other comments because this is not supported in the Salesforce CRM application:

/* /* comment */ */

Note

Including comments and formatting with carriage returns and spacing adds to the number of characters used and, therefore, counts against the character and byte size limits

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.47.218