## Friday, March 6, 2015

### Salesforce Lesson 3 - Cleaner SFDC Formulas

Have you ever needed to maintain a formula and you look at it and it seems confusing/unmanageable?  Salesforce is a fantastic platform for the non-technical to get into building business rules but it can quickly become a tangle of excess code if this is handled by non-engineer types.  Not all admins are created equal either.  One can learn all the ins and outs of administering an org but really still not be too skilled at the simplicity of clean formulas.

Here's a case of what NOT to do and what TO do...

Lets say, for example, you have the following that Fred the-brilliant-sales-guy-and-aspiring-engineer built as a formula:

OR(
AND(
fieldXYZ > 10,
CONTAINS(fieldABC, 'example1'),
field123 = 'just another case'
),
AND(
fieldXYZ = 5,
CONTAINS(fieldABC, 'example1'),
field123 = 'just another case'
),
AND(
fieldXYZ < 3,
CONTAINS(fieldABC, 'example1'),
field123 = 'just another case'
)
)

Ever see something like that?  Its probably cleaner than the average salesperson would develop after all check out that indentation - classic good geek practice for readability.  However thats not much of the crux here. Note the only difference between the multiple nested AND groups is the criteria on fieldXYZ otherwise each is the same test.  This is redundant and can quickly become more so as new criteria might need to be added.

The simplification that works is the following:

AND(
OR(
fieldXYZ > 10,
fieldXYZ = 5,
fieldXYZ < 3
),
CONTAINS(fieldABC, 'example1'),
field123 = 'just another case'
)

Thats it!  Isnt that better?  Essentially we need to think of the overarching AND instead of an overarching OR and reverse those then add the OR inside that expresses what was different about each original AND group.

This is a simple case but it applies to any similar situation where you have repeating AND group criteria inside an outer OR group.

Carry on and keep it clean and manageable folks!