{"id":5953,"date":"2017-03-28T20:26:34","date_gmt":"2017-03-28T20:26:34","guid":{"rendered":"http:\/\/www.jitendrazaa.com\/blog\/?p=5953"},"modified":"2017-08-09T03:39:31","modified_gmt":"2017-08-09T03:39:31","slug":"sfdcrules-simple-yet-powerful-rule-engine-for-salesforce","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/sfdcrules-simple-yet-powerful-rule-engine-for-salesforce\/","title":{"rendered":"SFDCRules &#8211; Simple yet powerful Rule Engine for Salesforce"},"content":{"rendered":"<p style=\"text-align: justify;\">Coming from Java background, we know that there are many open source or free <strong>Business Rules Management System (BRMS)<\/strong>\u00a0tools that can be used like <a href=\"https:\/\/github.com\/EasyRules\/easyrules\">Easy rules<\/a> or <a href=\"https:\/\/www.drools.org\/\">Drools<\/a>. I was in search of such tool for Salesforce but didn&#8217;t find any. There are few <a href=\"https:\/\/appexchange.salesforce.com\/results?keywords=BPM\">AppExchange BRMS products<\/a> however they are paid and heavy in terms of features needed. Who don&#8217;t love free stuff \ud83d\ude42 and wanted it free and open source. While searching, I came across <a href=\"https:\/\/martinfowler.com\/bliki\/RulesEngine.html\">this post<\/a> of Martin Fowler and it encouraged me to write my own <strong>rule engine<\/strong> for Salesforce.<\/p>\n<p style=\"text-align: justify;\">So, here it is. Free, light weight, basic but powerful <strong>rule engine<\/strong> written in Apex for Apex.<!--more--><\/p>\n<h3>Why would I need SFDCRules ?<\/h3>\n<p style=\"text-align: justify;\">Lets say, you need to implement some complex routing rule to assign opportunity to proper sales person. Rules could be like, if Opportunity state is CT then assign it to someone who worked on latest Won Opportunity for same state. We cannot use assignment rules in these case. We cannot hard-code this condition in Trigger or Apex, as it could be changed in future and therefore maintenance would be problem. We need kind of framework, which will read conditions from custom object, evaluate weather its true or false and according perform operation.<br \/>\nThere are three pieces here &#8211;<br \/>\n1. Get binding values which could be used to drive (in this case Opp State)<br \/>\n2. Evaluating condition<br \/>\n3. Taking action if its true \/ false. (Assign Opp to someone who worked on latest Won Opportunity)<br \/>\nLets say, we want to use Workflow rule , how we will take action, because it needs to read latest Opportunity.<\/p>\n<p style=\"text-align: justify;\">So seems, going custom could be the way. In this case as well, end user wants to declare rule on fly. We can give them wizard, where they can choose, objects, fields, and condition. Other part of wizard would be what action needs to taken if condition is true. For action part, it would be altogether different topic, so lets skip it. Now, the question is, how to develop Apex code, which can work like Workflow rule or Assignment rule, which can evaluate conditions on basis of merge fields passed and return true or false. I know it might not be best solution to the problem, this requirement may raise many more questions however wanted to just focus on <strong>dynamically condition evaluation<\/strong> problem part.<\/p>\n<h3><strong>How to use SFDCRules<\/strong><\/h3>\n<p style=\"text-align: justify;\">Its as simple as saying 123 \ud83d\ude09 . In order to use SFDCRules, we need to follow below steps<\/p>\n<ol>\n<li style=\"text-align: justify;\">Define set of all allowed operators. We can move this step to some helper method to\u00a0skip this step<\/li>\n<li style=\"text-align: justify;\">Define set of binding values, which will replace variable or merge fields in rule<\/li>\n<li style=\"text-align: justify;\">call\u00a0<strong>eval()\u00a0<\/strong>method of Rule class and it will return Boolean value indicating that rule evaluated is true or false<\/li>\n<\/ol>\n<p><script src=\"https:\/\/gist.github.com\/JitendraZaa\/f4389d092ec43d4ad17e2ba8aa45697d.js\"><\/script><\/p>\n<h3><strong>Capabilities, Considerations and Limitations<\/strong><\/h3>\n<ul>\n<li style=\"text-align: justify;\">All Operators, variables and values must be separated by one or more spaces. Spaces are used to tokenize expression. We fix this by introducing some normalizing method however it would cost some CPU time.<\/li>\n<li style=\"text-align: justify;\">Instead of writing <em>Value1 == 100 OR Value1 == 200\u00a0<\/em>we can use comma separated values. So, it can be written as\u00a0<em>Value1 == 100,200.\u00a0<\/em><\/li>\n<li style=\"text-align: justify;\">Comma separated value is only supported for Integer and Decimal datatype, not for Strings.<\/li>\n<li style=\"text-align: justify;\">Arithmetic operations like <em>Value1 &lt; 2*3<\/em>\u00a0not supported.<\/li>\n<li style=\"text-align: justify;\">Binding variables must be used as lower case.<\/li>\n<li style=\"text-align: justify;\">Spaces in string values are not allowed. So,\u00a0<em>Value1 == &#8216;My Bad&#8217;\u00a0<\/em>is not supported.<\/li>\n<li>Code does not support short circuit execution of logic yet.\n<ul>\n<li style=\"text-align: justify;\">Example :\u00a0<em>Value1 == 100 &amp;&amp; Value2 == 400\u00a0<\/em>. In this case, if first condition fails, we should not evaluate second, as result will always be false.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h3><strong>Performance<\/strong><\/h3>\n<p style=\"text-align: justify;\">We are talking about lots of string manipulations and comparison in\u00a0<strong>Salesforce BRMS rule engine (SFDCRules).\u00a0<\/strong>If its not used wisely, chances of hitting<strong> CPU limit<\/strong> are high. Lengthy expression size will result in using more CPU time. In Synchronous Apex, we get 10 sec before hitting <strong>CPU time limit error<\/strong>. Speaking about performance, around 7k expressions with 3 to 4 conditions can be evaluated before hitting 10 sec.<\/p>\n<h3><strong>Get Source code<\/strong><\/h3>\n<ul>\n<li><a href=\"https:\/\/github.com\/JitendraZaa\/SFDCRules\">Github repository<\/a><\/li>\n<li><a href=\"https:\/\/login.salesforce.com\/packaging\/installPackage.apexp?p0=04t46000001IPGq\">Install unmanaged package<\/a><\/li>\n<\/ul>\n<h3><strong>More examples of using SFDCRules<\/strong><\/h3>\n<p><script src=\"https:\/\/gist.github.com\/JitendraZaa\/771a8268ebf4ae3b738c1af4ca415373.js\"><\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Open source project to evaluate rules in Salesforce using Apex<\/p>\n","protected":false},"author":1,"featured_media":5957,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[9],"tags":[390,389,386,387,388],"class_list":["post-5953","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-salesforce","tag-brms","tag-business-rules-management-system","tag-open-source","tag-rule-engine","tag-sfdcrules"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/03\/SFDCRules.jpg?fit=1230%2C537&ssl=1","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":30745,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-business-rules-engine-guide\/","url_meta":{"origin":5953,"position":0},"title":"Salesforce Business Rules Engine Guide","author":"Jitendra Zaa","date":"December 20, 2025","format":false,"excerpt":"Master Salesforce Business Rules Engine. Complete guide covering expression sets, decision tables, matrices, real-world use cases, and implementation.","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Salesforce Business Rules Engine Complete Guide - Featured Image","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2025\/12\/BRE-Guide-Featured-16x9-1.png?fit=1200%2C675&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2025\/12\/BRE-Guide-Featured-16x9-1.png?fit=1200%2C675&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2025\/12\/BRE-Guide-Featured-16x9-1.png?fit=1200%2C675&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2025\/12\/BRE-Guide-Featured-16x9-1.png?fit=1200%2C675&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2025\/12\/BRE-Guide-Featured-16x9-1.png?fit=1200%2C675&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":5787,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/automated-code-review-for-apex-in-salesforce-static-code-analysis-video\/","url_meta":{"origin":5953,"position":1},"title":"Automated Code review for Apex in Salesforce &#8211; Static code analysis &#8211; Video","author":"Jitendra","date":"November 8, 2016","format":false,"excerpt":"Using open source PMD tool to generate code quality report for Apex classes","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Automated code review for apex in salesforce (static code analysis)","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/11\/Automated-Code-review-for-Apex-in-Salesforce-Static-code-analysis.jpg?fit=1138%2C423&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/11\/Automated-Code-review-for-Apex-in-Salesforce-Static-code-analysis.jpg?fit=1138%2C423&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/11\/Automated-Code-review-for-Apex-in-Salesforce-Static-code-analysis.jpg?fit=1138%2C423&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/11\/Automated-Code-review-for-Apex-in-Salesforce-Static-code-analysis.jpg?fit=1138%2C423&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/11\/Automated-Code-review-for-Apex-in-Salesforce-Static-code-analysis.jpg?fit=1138%2C423&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":6274,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/enterprise-territory-management-auto-account-assignment-using-apex\/","url_meta":{"origin":5953,"position":2},"title":"Enterprise Territory Management &#8211; Auto Account Assignment using Apex","author":"Jitendra","date":"September 22, 2017","format":false,"excerpt":"Use Apex code to auto assign Accounts on basis of Enterprise Territory Assignment rules","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Enterprise Territory - Auto Account Assignment using Apex","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/09\/Enterprise-Territory-Auto-Account-Assignment-using-Apex.jpg?fit=900%2C600&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/09\/Enterprise-Territory-Auto-Account-Assignment-using-Apex.jpg?fit=900%2C600&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/09\/Enterprise-Territory-Auto-Account-Assignment-using-Apex.jpg?fit=900%2C600&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/09\/Enterprise-Territory-Auto-Account-Assignment-using-Apex.jpg?fit=900%2C600&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":4721,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/list-of-ide-available-for-salesforce-coding\/","url_meta":{"origin":5953,"position":3},"title":"List of IDE available for Salesforce coding","author":"Jitendra","date":"July 17, 2015","format":false,"excerpt":"List of all major IDE like Eclipse, Welkins, Cloud9 etc to code Apex, Visualforce, Trigger and lightning components in Salesforce","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Salesforce Cloud9 IDE","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/07\/Salesforce-Cloud9-IDE.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/07\/Salesforce-Cloud9-IDE.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/07\/Salesforce-Cloud9-IDE.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/07\/Salesforce-Cloud9-IDE.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/07\/Salesforce-Cloud9-IDE.png?resize=1050%2C600&ssl=1 3x"},"classes":[]},{"id":2835,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-apex-code-talk-important-questions-and-answers\/","url_meta":{"origin":5953,"position":4},"title":"Salesforce Apex Code talk &#8211; Important Questions and Answers","author":"Jitendra","date":"May 3, 2012","format":false,"excerpt":"Salesforce Apex Code talk on - 24-April-2012 , Important Interview Questions","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":28,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-questions\/","url_meta":{"origin":5953,"position":5},"title":"Salesforce Interview Questions &#8211; Part 1","author":"Jitendra","date":"May 6, 2010","format":false,"excerpt":"Set of most often asked questions on the salesforce.com developement","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/5953","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=5953"}],"version-history":[{"count":16,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/5953\/revisions"}],"predecessor-version":[{"id":6200,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/5953\/revisions\/6200"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media\/5957"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=5953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=5953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=5953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}