tag:blogger.com,1999:blog-31699630920222759382018-09-16T20:06:44.548-07:00Cloud SpecSFDC development tutorials / lessonsRob Lindhttps://plus.google.com/105518267344618466630noreply@blogger.comBlogger5125tag:blogger.com,1999:blog-3169963092022275938.post-36863550254124741652016-01-30T14:56:00.001-08:002016-01-30T14:56:31.670-08:00Salesforce Lesson 5 - More Best Practices with IFs in FormulasToday I want to write about best practice uses of IF within formulas. Sometimes there are misuses of IFs that add extra complexity without gaining functionality.<br /><br />Understanding formulas can be a science of its own. They can become as complex as necessary but adding extra unnecessary complexity should always be avoided to make formulas error-free and more maintainable.<br /><br />Lets take the case of a Validation Rule. When embodied inside an AND( ) the following form of IF is unnecessary and NOT, in this technical engineer's humble opinion, a best practice. <br />Here is a simple formula example with an unnecessary IF:<br /><span style="color: red;">AND (</span><br /><span style="color: red;"> X = Y,</span><br /><span style="color: red;"> IF(A = B, TRUE, FALSE)</span><br /><span style="color: red;">)</span><br />You see the lack of necessity in the IF?<br />The only part necessary here is "A=B" because each line within the AND is a Boolean evaluation anyway returning TRUE when the condition is TRUE and FALSE when it is not. Its just that simple.<br /><br />The above formula should therefore be rewritten as follows:<br /><span style="color: red;">AND (</span><br /><span style="color: red;"> X = Y,</span><br /><span style="color: red;"> A = B</span><br /><span style="color: red;">)</span><br /><br />As alluded to above an important consideration is the purpose/place of the formula? Is it a <b>Formula Field </b>or is it <b>Validation Rule</b> or a <b>Workflow Rule (criteria)</b>? The latter two always return a Boolean value whereas the former can be a Checkbox (Boolean) but can also return various other data types. IF statements can therefore apply to returning something other than TRUE or FALSE.<br />For example a formula field might be written the following way:<br /><span style="color: red;">IF (X > Y, X, Y)</span><br /><br />Finally there can be plenty of uses of IF statements in Validation Rules to use two different Boolean evaluations. Lets say you want an error to be evaluated two different ways - one way when field X is greater than field Y and another way when X is less than or equal to Y. This hypothetical might be coded the following way:<br /><span style="color: red;">IF (X > Y, </span><br /><span style="color: red;"> AND( Amount1 < Amount2,</span><br /><span style="color: red;"> FieldA = FieldB</span><br /><span style="color: red;"> ),</span><br /><span style="color: red;"> A > Z</span><br /><span style="color: red;">)</span><br />An alternative to this would be:<br /><span style="color: red;">OR(</span><br /><span style="color: red;"> AND( X > Y, </span><br /><span style="color: red;"> Amount1 < Amount2, </span><br /><span style="color: red;"> FieldA = FieldB</span><br /><span style="color: red;"> ),</span><br /><span style="color: red;"> AND( X <= Y, </span><br /><span style="color: red;"> A > Z </span><br /><span style="color: red;"> )</span><br /><span style="color: red;">)</span><br /><span style="color: red;"><br /></span>It is less complex to use the first form with the IF. In this instance we are not assigning a static TRUE or FALSE return value which, as a general rule, is pointless.<br /><br /><b>Happy formulating!</b>Rob Lindhttps://plus.google.com/105518267344618466630noreply@blogger.com0tag:blogger.com,1999:blog-3169963092022275938.post-15804489722656819332015-06-01T13:58:00.000-07:002015-07-09T08:12:15.345-07:00Salesforce Lesson 4 - Notable Formula Logic MistakesGreetings aspiring Force.com developers,<br /><br />Something like this does NOT mean anything if included with other AND conditions:<br /><b>OR (FieldX <> 'A', </b><br /><b> FieldX <> 'B'</b><br /><b>)</b><br /><br />Why? Because its <u>always going to return true</u> regardless of what FieldX value is. If the value of FieldX happens to be 'A' its true because then FieldX <> 'B'. If the value of FieldX happens to be 'B' its true because then FieldX <> 'A'. If the value of FieldX happens to be 'Z' (or anything else) its true because then FieldX <> 'A'.<br /><br />OK so don't do that - Im beggin.<br /><br />Maybe you really want NEITHER value A or B here? (Thats what I think when I see code like this anyway.) In that case you want to say: <br /><b>AND (FieldX <> 'A', </b><br /><b> FieldX <> 'B'</b><br /><b>)</b><br /><b><br /></b><u>Although if you had this condition inside another AND condition group, as supposed in my first sentence, then just use:</u><br /><b>FieldX <> 'A', </b><br /><div><b>FieldX <> 'B'</b><br /><b><br /></b><u>Generally speaking this construct is excessive and more complicated for no reason:</u><br /><b>AND(AND(condition1, condition2), condition3)</b><br /><br />Simplify to: <b>AND(condition1, condition2, condition3)</b></div><br /><b>For the developer who needs to expand on a rule it can continue to get more complex so keep it as tight and clean as possible to begin with. </b>Rob Lindhttps://plus.google.com/105518267344618466630noreply@blogger.com0tag:blogger.com,1999:blog-3169963092022275938.post-87788848705704964782015-03-06T07:36:00.000-08:002015-07-09T08:09:03.530-07:00Salesforce Lesson 3 - Cleaner SFDC FormulasHave 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. <br /><br />Here's a case of what NOT to do and what TO do...<br /><br />Lets say, for example, you have the following that Fred the-brilliant-sales-guy-and-aspiring-engineer built as a formula:<br /><br /><span style="background-color: white; color: red;">OR(</span><br /><span style="background-color: white; color: red;"> AND(</span><br /><span style="background-color: white; color: red;"> fieldXYZ > 10,</span><br /><span style="background-color: white; color: red;"> CONTAINS(fieldABC, 'example1'),</span><br /><span style="background-color: white; color: red;"> field123 = 'just another case'</span><br /><span style="background-color: white; color: red;"> ),</span><br /><span style="background-color: white; color: red;"> AND(</span><br /><span style="background-color: white; color: red;"> fieldXYZ = 5,</span><br /><span style="background-color: white; color: red;"> CONTAINS(fieldABC, 'example1'),</span><br /><span style="background-color: white; color: red;"> field123 = 'just another case'</span><br /><span style="background-color: white; color: red;"> ),</span><br /><span style="background-color: white; color: red;"> AND(</span><br /><span style="background-color: white; color: red;"> fieldXYZ < 3,</span><br /><span style="background-color: white; color: red;"> CONTAINS(fieldABC, 'example1'),</span><br /><span style="background-color: white; color: red;"> field123 = 'just another case'</span><br /><span style="background-color: white; color: red;"> )</span><br /><span style="background-color: white; color: red;">)</span><br /><br />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.<br /><br />The simplification that works is the following:<br /><br /><span style="background-color: white; color: red;">AND(</span><br /><span style="background-color: white; color: red;"> OR( </span><br /><span style="background-color: white; color: red;"> fieldXYZ > 10,</span><br /><div><span style="background-color: white; color: red;"> fieldXYZ = 5,</span></div><div><span style="background-color: white; color: red;"> fieldXYZ < 3</span><br /><span style="background-color: white; color: red;"> ),</span><br /><span style="background-color: white; color: red;"> CONTAINS(fieldABC, 'example1'),</span><br /><span style="background-color: white; color: red;"> field123 = 'just another case'</span><br /><span style="background-color: white; color: red;">)</span><br /><br />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.<br /><br />This is a simple case but it applies to any similar situation where you have repeating AND group criteria inside an outer OR group.<br /><br />Carry on and keep it clean and manageable folks!<br /><br /><br /><br /><div><br /></div><br /></div><div><br /></div>Rob Lindhttps://plus.google.com/105518267344618466630noreply@blogger.com0tag:blogger.com,1999:blog-3169963092022275938.post-54825372122580154132014-09-19T10:41:00.002-07:002015-03-06T08:20:32.340-08:00Salesforce Lesson 2 - Formula IF Statement<span style="color: black; font-weight: bold;">What is the specific syntax of IF in Salesforce.com formulae?</span><p>First, to reiterate something (not so emphasized) in my last post (Lesson 1), Salesforce formula syntax is very much like Excel formula syntax. So if you have mastered an IF statement in Excel you will have little trouble understanding Salesforce IF in formulas. <br>(**Do note however that in Salesforce Apex language the IF statement is a considerably different animal. There it is just like it is in Java.) <p>The standard format of the formula IF is: <br><span style="color: blue;">IF(condition, result if condition is TRUE, result if condition is FALSE)</span> <p>Unlike the AND and OR statements which return only a Boolean, the IF stantement will return anything you like, including a Boolean True or False if you wish. <p>And you can nest IF statements inside the True or False result sections to create more complex conditions. <br>For example with custom fields on Lead TotalQuantity__c and TotalDollars__c you want to create a text formula field called Lead Size the formula could be something like: <br><span style="color: blue;">IF(TotalQuantity__c > 1000, IF(TotalDollars__c > 10000, 'Huge', 'Large'), IF(TotalQuantity__c > 500, 'Medium', 'Small') )</span><p>This example works in the following way: <br>a. if TotalQuatity is greater than 1000 and TotalDollars is greater than 10,000 then the Lead Size is "Huge"; <br>b. if TotalQuatity is greater than 1000 and TotalDollars is NOT greater than 10,000 then the Lead Size is "Large"; <br>c. if TotalQuatity is NOT greater than 1000 but is greater than 500 then the Lead Size is "Medium"; <br>d. finally, if TotalQuatity is NOT greater than 1000 NOR is it greater than 500 then the Lead Size is "Small". <p>If you are a Java or Apex coder interested in a comparison here is what this construct would look like in one of those laguages as a nested If statement: <span style="color: blue;"><br>If (TotalQuantity__c > 1000) { <br> If (TotalDollars__c > 10000) {LeadSize__c = 'Huge';} <br> Else {LeadSize__c = 'Large';} <br>} <br>Else { <br> If (TotalQuantity__c > 500) {LeadSize__c = 'Medium';} <br> Else {LeadSize__c = 'Small';} <br>}</span><p>If you simply need Lead Sizes of Large, Medium, and Small conditioning only with TotalQuantity criteria the formula would be a little simpler: <br><span style="color: blue;">IF(TotalQuantity__c > 1000, 'Large', IF(TotalQuantity__c > 500, 'Medium', 'Small') )</span> <p><span style="color: black; font-weight: bold;">Note in the above examples the lack of AND/OR statements.</span> One could combine IF statements with AND/OR combinations but those function in formulas exactly as described in my previous post, not as one who is classically trained with conditional logic might expect. <p><p><span style="color: green; font-weight: bold;">Good luck with reinventing your brain around these and other Salesforce constructs and feel free to comment with questions.</span>Rob Lindhttps://plus.google.com/105518267344618466630noreply@blogger.com0tag:blogger.com,1999:blog-3169963092022275938.post-7407864344580579072014-08-14T08:31:00.000-07:002015-03-06T08:20:32.343-08:00Salesforce Lesson 1 - Formula Constructs AND / OR<span style="color: black; font-weight: bold;">What is the specific syntax of ANDs and ORs in Salesforce.com formulae?</span><p>These constructs look very different from what conventional If statements** with ands or ors in many of the usual languages look like. (Though Excel et.al. are similar in their formula formulations to how Salesforce applies them.) <br>** IF statements will be the topic of my next post. <p>For starters the <span style="color: black; font-weight: bold;">result</span> of a compound conditional statement using either an AND or an OR is going to be a <span style="color: black; font-weight: bold;">Boolean True or False</span>. In Salesforce validation rules a true result is going to be a positive error condition. In essence though it is the same when using a formula for other purposes such as workflow conditions. That is - if the condition is True something will take place, otherwise whatever something is won't. <br>You set an outcome that will occur as the result of a formula evaluating to true. But if it evaluates false, again, the outcome does not "come out". <p>Anyway - back to ANDs and ORs. When a decision is based on multiple - and possibly nested - conditions this is where ANDs and ORs come into play. <p>The standard format of AND is: <br><span style="color: blue;">AND(condition 1, condition 2,..., condition x)</span> <br>and when ALL conditions 1 - x are true then the total result is true. If ONE OR MORE of them are false then the result is false. <p>The standard format of OR is: <br><span style="color: blue;">OR(condition 1, condition 2,..., condition x)</span> <br>and when ONE OR MORE conditions 1 - x are true then total result is true. If ALL of them are false then the result is false. <p>More complex constructs may easily be built: <br><span style="color: blue;">AND(condition 1, condition 2, OR(condition a, condition b))</span> <br>is a simple nested example. This means that if condition 1 is true and condition 2 is true and either or both condition a or condition b is true then the total result is true. <p>A few specifics to pay attention to: <ul><li>Note your punctuation - its important. <ul><li>Parentheses enclose every OR or AND block.</li><li>Commas follow every condition except the last in a given OR or AND block.</li></li></ul> <li>Picklist fields need to be wrapped by the TEXT() function for comparing with a text literal.</li><li>Use other formula functions when necessary or appropriate - such as ISBLANK to determine if a field has no value (null) in it.</li></ul> <p>Once you get the hang of this it becomes second nature to see how to solve a given problem. Mind you it helps to understand logic and set theory. <p>I kicked off my Salesforce.com work in what might be thought of as reverse order - at least by the powers that be @ "[International No Symbol] SOFTWARE". I was coding some advanced solutions with Visualforce and Apex right out of the gate yet I really did not get the formula syntax until later when a client asked me to start building solutions with it. <br> Now its just like a walk in the park. Rob Lindhttps://plus.google.com/105518267344618466630noreply@blogger.com0