{"id":245,"date":"2015-09-26T09:23:19","date_gmt":"2015-09-26T04:23:19","guid":{"rendered":"https:\/\/learnsf.wordpress.com\/?p=245"},"modified":"2023-12-30T17:12:54","modified_gmt":"2023-12-30T23:12:54","slug":"3-steps-to-optimize-a-complex-formula","status":"publish","type":"post","link":"https:\/\/blog.felineflock.com\/index.php\/2015\/09\/26\/3-steps-to-optimize-a-complex-formula\/","title":{"rendered":"Optimize Complex Salesforce Formulas: 3 Proven Steps"},"content":{"rendered":"<h5><strong>This article was also posted in LinkedIn: \u00a0<a href=\"https:\/\/www.linkedin.com\/pulse\/3-steps-optimize-complex-formula-fernando-mendes-fernandez?trk=prof-post\" target=\"_blank\" rel=\"noopener\">3 Steps to optimize a complex formula in Salesforce<\/a>.<\/strong><\/h5>\n<p>The formula below was created straight from the client requirements and didn&#8217;t fit the maximum formula size (5k bytes) when compiled.<br \/>\nIt\u00a0was intended to display a red, yellow or green square depending on how many days the account was past due and according to the type of the account.<br \/>\nHere are 3 steps\u00a0to\u00a0optimize it to fit the limit.<\/p>\n<blockquote style=\"font-size:8px;\"><p>IF(<br \/>\nISBLANK(Type__c),<br \/>\nIMAGE(&#8220;\/img\/samples\/color_red.gif&#8221;, &#8220;Red&#8221;, 10, 10),<br \/>\nIF(<br \/>\nOR(<br \/>\n(Type__c = &#8220;TA&#8221;),<br \/>\n(Type__c = &#8220;DB&#8221;),<br \/>\n(Type__c = &#8220;AC&#8221;)),<br \/>\nIMAGE(&#8220;\/img\/samples\/color_red.gif&#8221;, &#8220;Red&#8221;, 10, 10),<br \/>\nIF(<br \/>\n(Amount__c &lt; 1), IMAGE(&#8220;\/img\/samples\/color_green.gif&#8221;, &#8220;Green&#8221;, 10, 10), IF( AND( (Type__c = &#8220;C2&#8221;), (Amount__c &gt; 1),<br \/>\n(Days_Past_Due__c &lt;= 7)), IMAGE(&#8220;\/img\/samples\/color_yellow.gif&#8221;, &#8220;Yellow&#8221;, 10, 10), IF( AND( (Type__c = &#8220;C2&#8221;), (Amount__c &gt; 1),<br \/>\n(Days_Past_Due__c &gt; 7)),<br \/>\nIMAGE(&#8220;\/img\/samples\/color_red.gif&#8221;, &#8220;Red&#8221;, 10, 10),<br \/>\nIF(<br \/>\nAND(<br \/>\nOR(<br \/>\n(Type__c = &#8220;PC&#8221;),<br \/>\n(Type__c = &#8220;CN&#8221;)),<br \/>\n(Amount__c &gt; 1),<br \/>\n(Days_Past_Due__c &gt;= 1)),<br \/>\nIMAGE(&#8220;\/img\/samples\/color_red.gif&#8221;, &#8220;Red&#8221;, 10, 10),<br \/>\nIF(<br \/>\nAND(<br \/>\n(Type__c = &#8220;P2&#8221;),<br \/>\n(Amount__c &gt; 1),<br \/>\n(Days_Past_Due__c &lt;= 7)), IMAGE(&#8220;\/img\/samples\/color_red.gif&#8221;, &#8220;Red&#8221;, 10, 10), IF( AND( (Type__c = &#8220;P2&#8221;), (Amount__c &gt; 1),<br \/>\n(Days_Past_Due__c &gt; 7)),<br \/>\nIMAGE(&#8220;\/img\/samples\/color_yellow.gif&#8221;, &#8220;Yellow&#8221;, 10, 10),<br \/>\nIF(<br \/>\nAND(<br \/>\n(Type__c = &#8220;WT&#8221;),<br \/>\n(Amount__c &gt; 1),<br \/>\n(Days_Past_Due__c &lt;= 14)), IMAGE(&#8220;\/img\/samples\/color_green.gif&#8221;, &#8220;Green&#8221;, 10, 10), IF( AND( (Type__c = &#8220;WT&#8221;), (Amount__c &gt; 1),<br \/>\n(Days_Past_Due__c &gt; 14),<br \/>\n(Days_Past_Due__c &lt;= 21)), IMAGE(&#8220;\/img\/samples\/color_yellow.gif&#8221;, &#8220;Yellow&#8221;, 10, 10), IF( AND( (Type__c = &#8220;WT&#8221;), (Amount__c &gt; 1),<br \/>\n(Days_Past_Due__c &gt; 21)),<br \/>\nIMAGE(&#8220;\/img\/samples\/color_red.gif&#8221;, &#8220;Red&#8221;, 10, 10),<br \/>\nIF(<br \/>\nAND(<br \/>\n(Type__c = &#8220;WF&#8221;),<br \/>\n(Amount__c &gt; 1),<br \/>\n(Days_Past_Due__c &lt;= 28)), IMAGE(&#8220;\/img\/samples\/color_green.gif&#8221;, &#8220;Green&#8221;, 10, 10), IF( AND( (Type__c = &#8220;WF&#8221;), (Amount__c &gt; 1),<br \/>\n(Days_Past_Due__c &gt; 28),<br \/>\n(Days_Past_Due__c &lt;= 36)), IMAGE(&#8220;\/img\/samples\/color_yellow.gif&#8221;, &#8220;Yellow&#8221;, 10, 10), IF( AND( (Type__c = &#8220;WF&#8221;), (Amount__c &gt; 1),<br \/>\n(Days_Past_Due__c &gt; 36)),<br \/>\nIMAGE(&#8220;\/img\/samples\/color_red.gif&#8221;, &#8220;Red&#8221;, 10, 10),<br \/>\nIF(<br \/>\nAND(<br \/>\n(Type__c = &#8220;MT&#8221;),<br \/>\n(Amount__c &gt; 1),<br \/>\n(Days_Past_Due__c &lt;= 37)), IMAGE(&#8220;\/img\/samples\/color_green.gif&#8221;, &#8220;Green&#8221;, 10, 10), IF( AND( (Type__c = &#8220;MT&#8221;), (Amount__c &gt; 1),<br \/>\n(Days_Past_Due__c &gt; 37),<br \/>\n(Days_Past_Due__c &lt;= 45)), IMAGE(&#8220;\/img\/samples\/color_yellow.gif&#8221;, &#8220;Yellow&#8221;, 10, 10), IF( AND( (Type__c = &#8220;MT&#8221;), (Amount__c &gt; 1),<br \/>\n(Days_Past_Due__c &gt; 45)),<br \/>\nIMAGE(&#8220;\/img\/samples\/color_red.gif&#8221;, &#8220;Red&#8221;, 10, 10),<br \/>\nIF(<br \/>\nAND(<br \/>\n(Type__c = &#8220;MS&#8221;),<br \/>\n(Amount__c &gt; 1),<br \/>\n(Days_Past_Due__c &lt;= 67)), IMAGE(&#8220;\/img\/samples\/color_green.gif&#8221;, &#8220;Green&#8221;, 10, 10), IF( AND( (Type__c = &#8220;MS&#8221;), (Amount__c &gt; 1),<br \/>\n(Days_Past_Due__c &gt; 67),<br \/>\n(Days_Past_Due__c &lt;= 75)), IMAGE(&#8220;\/img\/samples\/color_yellow.gif&#8221;, &#8220;Yellow&#8221;, 10, 10), IF( AND( (Type__c = &#8220;MS&#8221;), (Amount__c &gt; 1),<br \/>\n(Days_Past_Due__c &gt; 75)),<br \/>\nIMAGE(&#8220;\/img\/samples\/color_red.gif&#8221;, &#8220;Red&#8221;, 10, 10),<br \/>\nNULL<br \/>\n))))))))))))))))))))<\/p><\/blockquote>\n<h3 style=\"padding-left:30px;\"><strong>1) Reorganize\/group the conditions by their outcome<\/strong><\/h3>\n<p>Notice the repeated references to red, yellow and green image links scattered throughout the formula. That sequence of the conditions probably match what makes sense to the end user but this arrangement leads to redundancy in the formula.<\/p>\n<p>Below is how it looked like when I started grouping the conditions for Green:<\/p>\n<blockquote style=\"font-size:8px;\"><p>IF((Amount__c &lt; 1), IMAGE(&#8220;\/img\/samples\/color_green.gif&#8221;, &#8220;Green&#8221;, 10, 10),<br \/>\nIF( AND( (Type__c = &#8220;WT&#8221;), (Amount__c &gt; 1),\u00a0(Days_Past_Due__c &lt;= 14)), IMAGE(&#8220;\/img\/samples\/color_green.gif&#8221;, &#8220;Green&#8221;, 10, 10),<br \/>\nIF( AND( (Type__c = &#8220;WF&#8221;), (Amount__c &gt; 1),\u00a0(Days_Past_Due__c &lt;= 28)), IMAGE(&#8220;\/img\/samples\/color_green.gif&#8221;, &#8220;Green&#8221;, 10, 10),<br \/>\nIF( AND( (Type__c = &#8220;MT&#8221;), (Amount__c &gt; 1),\u00a0(Days_Past_Due__c &lt;= 37)), IMAGE(&#8220;\/img\/samples\/color_green.gif&#8221;, &#8220;Green&#8221;, 10, 10),<br \/>\nIF( AND( (Type__c = &#8220;MS&#8221;), (Amount__c &gt; 1),\u00a0(Days_Past_Due__c &lt;= 67)),<br \/>\nIMAGE(&#8220;\/img\/samples\/color_green.gif&#8221;, &#8220;Green&#8221;, 10, 10),<br \/>\n&#8230;<\/p><\/blockquote>\n<p>The purpose is\u00a0to reorganize the conditions in preparation for the next step, which is:<\/p>\n<h3 style=\"padding-left:30px;\"><strong>2)\u00a0<\/strong><strong>Remove nested IFs &#8211; use ORs and line breaks<\/strong><\/h3>\n<p>This is easier to show than to explain, but it means to replace\u00a0the several nested IFs with a single IF with\u00a0a long sequence of conditions, each condition separated by || which is the shorthand for OR.<\/p>\n<p>It follows the pattern IF expression1 ||\u00a0expression2 || expression3 &#8230; where each OR expression is in a separate line for legibility (use Ctrl or Shift + Enter).<br \/>\nEach expression can (usually) contain nested ANDs.<\/p>\n<p><a href=\"http:\/\/blog.felineflock.com\/wp-content\/uploads\/2015\/09\/screen-shot-2015-09-25-at-9-17-49-pm.png\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-263 aligncenter\" src=\"http:\/\/blog.felineflock.com\/wp-content\/uploads\/2015\/09\/screen-shot-2015-09-25-at-9-17-49-pm.png?w=300\" alt=\"Screen Shot 2015-09-25 at 9.17.49 PM\" width=\"608\" height=\"251\" \/><\/a><\/p>\n<p>Here is how it looks like for the Green conditions reorganized in the previous step.<br \/>\nNotice that now there is only a single reference to the green image link.<br \/>\nThe compiled formula is now smaller because the redundant nested IFs and IMAGE references were replaced.<\/p>\n<blockquote style=\"font-size:8px;\"><p>IF((Amount__c &lt; 1)<br \/>\n|| AND( (Type__c = &#8220;WT&#8221;), (Amount__c &gt; 1),\u00a0(Days_Past_Due__c &lt;= 14))<br \/>\n|| AND( (Type__c = &#8220;WF&#8221;), (Amount__c &gt; 1),\u00a0(Days_Past_Due__c &lt;= 28))<br \/>\n|| AND( (Type__c = &#8220;MT&#8221;), (Amount__c &gt; 1),\u00a0(Days_Past_Due__c &lt;= 37))<br \/>\n|| AND( (Type__c = &#8220;MS&#8221;), (Amount__c &gt; 1),\u00a0(Days_Past_Due__c &lt;= 67)),<br \/>\nIMAGE(&#8220;\/img\/samples\/color_green.gif&#8221;, &#8220;Green&#8221;, 10, 10),<br \/>\n&#8230;<\/p><\/blockquote>\n<p>Additionally, you can use &amp;&amp; (shorthand for AND) and remove unneeded parenthesis:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-262 aligncenter\" src=\"http:\/\/blog.felineflock.com\/wp-content\/uploads\/2015\/09\/screen-shot-2015-09-25-at-9-25-49-pm.png?w=300\" alt=\"Screen Shot 2015-09-25 at 9.25.49 PM\" width=\"578\" height=\"248\" \/><\/p>\n<p>This way you can further identify repeating patterns that you can optimize and eliminate redundancy.<\/p>\n<p>At this point, the formula was pretty close to fitting the limit, but if the formula was lengthier, there are\u00a0other\u00a0optimizations\u00a0to use.<\/p>\n<h3 class=\"left\" style=\"padding-left:30px;\"><strong>3)\u00a0<\/strong><strong>Identify repeated conditions and create separate formulas\/expressions for\u00a0them&#8230; using a few tricks!<\/strong><\/h3>\n<blockquote><p>IF( Amount__c &lt; 1<br \/>\n|| ( <strong>Type__c = &#8220;WT&#8221;<\/strong> &amp;&amp; Amount__c &gt; 1 &amp;&amp; Days_Past_Due__c &lt;= <strong>14<\/strong> )<br \/>\n|| ( <strong>Type__c = &#8220;WF&#8221;<\/strong> &amp;&amp; Amount__c &gt; 1 &amp;&amp; Days_Past_Due__c &lt;= <strong>28<\/strong> )<br \/>\n|| ( <strong>Type__c = &#8220;MT&#8221;<\/strong> &amp;&amp; Amount__c &gt; 1 &amp;&amp; Days_Past_Due__c &lt;= <strong>37<\/strong> )<br \/>\n|| ( <strong>Type__c = &#8220;MS&#8221;<\/strong> &amp;&amp; Amount__c &gt; 1 &amp;&amp; Days_Past_Due__c &lt;= <strong>67<\/strong> )<br \/>\n, IMAGE( &#8220;\/img\/samples\/color_green.gif&#8221;, &#8220;Green&#8221;, 10, 10 ),<br \/>\n&#8230;<\/p><\/blockquote>\n<p>Notice in the above formula that for\u00a0type WT the\u00a0past due limit is\u00a014, while type WF has limit of 28 and so forth for MT and MS? Notice how the amount &gt; 1 condition is the same for all of the 4 cases.<\/p>\n<p class=\"left\">You can condense all the<em>\u00a0&#8220;type-past due&#8221;\u00a0<\/em>conditions into a single CASE() function like below:<\/p>\n<p class=\"left\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-260 aligncenter\" src=\"http:\/\/blog.felineflock.com\/wp-content\/uploads\/2015\/09\/screen-shot-2015-09-25-at-10-01-23-pm.png?w=300\" alt=\"Screen Shot 2015-09-25 at 10.01.23 PM\" width=\"567\" height=\"211\" \/><\/p>\n<blockquote style=\"font-size:8px;\"><p>IF( Amount__c &lt; 1<br \/>\n|| ( Amount__c &gt; 1 &amp;&amp; Days_Past_Due__c &lt;= CASE( Type__c, &#8220;WT&#8221;, 14, &#8220;WF&#8221;, 28, &#8220;MT&#8221;, 37, &#8220;MS&#8221;, 67, -9999 ) )<br \/>\n, IMAGE( &#8220;\/img\/samples\/color_green.gif&#8221;, &#8220;Green&#8221;, 10, 10 ),<br \/>\n&#8230;<\/p><\/blockquote>\n<p class=\"left\">That way Days_Past_Due__c will be compared to a different value depending on the Type__c. The -9999 is there just to fail the condition when the type is not one of the 4 (WT, WF, MT\u00a0or\u00a0MS).<br \/>\nThe CASE expression could also be placed in a separate formula named &#8220;<em>Past_Due_Limit_Per_Type__c<\/em>&#8221; to help with legibility (it doesn&#8217;t reduce the compiled size of the formula though).<\/p>\n<p>Another way of condensing multiple OR conditions like the ones found below is to use CONTAINS().<\/p>\n<blockquote style=\"font-size:8px;\"><p>IF(<br \/>\nOR(<br \/>\n(Type__c = &#8220;TA&#8221;),<br \/>\n(Type__c = &#8220;DB&#8221;),<br \/>\n(Type__c = &#8220;AC&#8221;)),<br \/>\nIMAGE(&#8220;\/img\/samples\/color_red.gif&#8221;, &#8220;Red&#8221;, 10, 10),<br \/>\n&#8230;<\/p><\/blockquote>\n<p>See below: \u00a0you can use CONTAINS and group all the types to check in a single string with vertical pipes as delimiter. When the type matches TA, DB or AC, it will find the type inside the string &#8220;|TA|DB|AC|&#8221;.<\/p>\n<blockquote style=\"font-size:8px;\"><p>IF( CONTAINS( &#8220;|TA|DB|AC|&#8221;, Type__c )<br \/>\n, IMAGE(&#8220;\/img\/samples\/color_red.gif&#8221;, &#8220;Red&#8221;, 10, 10),<br \/>\n&#8230;<\/p><\/blockquote>\n<p>The higher the number of values to check, the more you benefit from this trick.<\/p>\n<p><strong>Beware of when the value does\u00a0not have a fixed length<\/strong>: \u00a0if Type__c is &#8220;A&#8221; it will match &#8220;TA&#8221; in the above expression, so be careful! To make the formula safer, you can use <em><strong>&#8220;|&#8221; + Type__c + &#8220;|&#8221;<\/strong><\/em> instead of Type__c.<\/p>\n<h2><strong>In summary&#8230;<\/strong><\/h2>\n<p>The 3 steps are:<\/p>\n<ol>\n<li>\n<h2>Reorganize the complex formula by outcome\/result<\/h2>\n<\/li>\n<li>\n<h2>Replace nested IFs with the pattern OR ( exprA1 AND exprA2 AND &#8230; ) OR ( exprB1 AND exprB2 AND &#8230; )<\/h2>\n<\/li>\n<li>\n<h2>Identify repeated conditions and create formula\/expressions for them<\/h2>\n<\/li>\n<\/ol>\n<h1><\/h1>\n","protected":false},"excerpt":{"rendered":"<p>This article was also posted in LinkedIn: \u00a03 Steps to optimize a complex formula in Salesforce. The formula below was created straight from the client requirements and didn&#8217;t fit the maximum formula size (5k bytes) when compiled. It\u00a0was intended to display a red, yellow or green square depending on how many days the account was [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[11],"tags":[],"class_list":["post-245","post","type-post","status-publish","format-standard","hentry","category-formula"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/posts\/245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/comments?post=245"}],"version-history":[{"count":1,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/posts\/245\/revisions"}],"predecessor-version":[{"id":556,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/posts\/245\/revisions\/556"}],"wp:attachment":[{"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/media?parent=245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/categories?post=245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.felineflock.com\/index.php\/wp-json\/wp\/v2\/tags?post=245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}