Sunday, February 5, 2012

A comment on formatting formulas and validation rules in Salesforce


This blog is in response to a question posted on the LinkedIn Salesforce group at:
The post lost all formatting and one point being made in my comment was using indentation for clarity so this blog is being posted to help make the point.

The question posted by Katherine was:
Validation rule to make products and Schedules necessary at a certain stage
When the stage reaches X, Y, or Z, sales reps are required to enter products and schedules. Before these stages, products and schedules are not required. Does anyone know how to write a validation rule for this?

My response was:

There seems to be some question about the syntax in the sample validation rule. For those that are interested I thought it might be useful to explain what is going on in the simplest English I can. It is useful to explain from the inside out so first the CASE statement:

Salesforce compares the StageName in the sequence given in the CASE. If any of the values match the StageName then the CASE returns a 1. If none of the values match, then the CASE returns 0. This CASE returns numbers and the enclosing AND function requires a Boolean value (True or False) so the =1 is appended to the CASE statement which Salesforce compares to the result of the CASE. Note: The = sign can mean different things in Salesforce and in this case it is a comparison and not an assignment. If the case returns 1 then the result is True, otherwise it is False.

For clarity the CASE segment of code can be rewritten as (orange reflects difference from original code):

IF (

     CASE(

           StageName,
           "Perception Analysis",1,
           "Proposal/Price Quote", 1,
           "Negotiation/Review", 1,
           "Closed Won", 1,
           0
    ) = 1, True, False
) ,…

In the solution Katherine presented, the IF statement is implied in Salesforce and is shown in shorthand notation.  This type of shorthand is convenient for the programmer but is totally unreadable by anyone who has to support the code later.

In the NOT segment:
If the Opportunity is new or already has an Opportunity Line Item then the result of the NOT function is False.

With the two inner components defined then the AND operation becomes apparent.

For further clarity, the entire validation rule is rewritten with indentation. Note the comments explaining what the =1 does and the logic of the Not segment does.  Since writing comments negates any advantage of shorthand notation it is better if you don't use implied code.  I have decades of programming experience and this use of the Case statement was totally unfamiliar to me. The colored text in the following segment is only for clarity in this blog.

AND (
     
CASE
           StageName
          "Perception Analysis", 1,
         
"Proposal/Price Quote", 1,
         
"Negotiation/Review", 1,
         
"Closed Won", 1, 
          0
    ) = 1, /* Here the = sign is a comparison operator and returns True if any of the CASE Values match the StageName */


/* The next block returns True if Opportunity is neither New nor has an Opportunity Line Item */ 
     NOT (
         
OR (
               
ISNEW(),
               
HasOpportunityLineItem
          )
     )
)

Unfortunately Salesforce strips off extra spaces to save storage space so once the rule is saved you will lose the indentation. I always write complex rules like this in a text editor and use tabs to indent and then copy and paste into the rule editor. You won't see the indentation when viewing the rule but you do see it when you edit it.
I hope that adds clarity to how the validation rule works and useful tips in making your code more readable.

Teno



To find out more about our services visit us at www.quickezlearning.com 

No comments:

Post a Comment