{"id":2811,"date":"2017-10-12T11:07:48","date_gmt":"2017-10-12T11:07:48","guid":{"rendered":"http:\/\/himbap.com\/blog\/?p=2811"},"modified":"2017-10-12T11:07:48","modified_gmt":"2017-10-12T11:07:48","slug":"running-business-rule-only-on-record-create","status":"publish","type":"post","link":"https:\/\/himbap.com\/blog\/?p=2811","title":{"rendered":"Running business rule only on record create"},"content":{"rendered":"<p><strong>Requirement<\/strong>: Execute business rule only on create of entity record.<br \/>\n<strong>Solution<\/strong>: As we know that business rules are executed on form load and on change of the associated field, which means if we have business rule for any entity it will fire for new record as well for existing records when form will load.<\/p>\n<p><em>If you are new to business rule, you can refer our <a href=\"https:\/\/himbap.com\/blog\/?s=business+rule\">earlier articles for business rules<\/a><\/em><\/p>\n<p>Let see this by example, consider we have one business rule in account entity which checks if industry field is blank, if yes, it will set it\u2019s value to consulting. Let\u2019s say we have following business rule for account entity which is activated.<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/10\/Businessrule1.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2812 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/10\/Businessrule1-300x211.png\" alt=\"Businessrule1\" width=\"300\" height=\"211\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/10\/Businessrule1-300x211.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/10\/Businessrule1-624x440.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/10\/Businessrule1.png 734w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\nNow if we will try to create a new account record, our business rule will fire and we can see industry field is populated like following:<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/10\/Businessrule2.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2813 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/10\/Businessrule2-300x271.png\" alt=\"Businessrule2\" width=\"300\" height=\"271\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/10\/Businessrule2-300x271.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/10\/Businessrule2.png 480w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\nSimilarly if we will open any existing record where industry field is blank it will populate industry field on form load, for example following record have industry field as blank<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/10\/Businessrule3.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2814 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/10\/Businessrule3-300x75.png\" alt=\"Businessrule3\" width=\"300\" height=\"75\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/10\/Businessrule3-300x75.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/10\/Businessrule3.png 624w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Now if we will open this record, industry field will be populated by our business rule like following:<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/10\/Businessrule4.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2815 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/10\/Businessrule4-300x300.png\" alt=\"Businessrule4\" width=\"300\" height=\"300\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/10\/Businessrule4-300x300.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/10\/Businessrule4-150x150.png 150w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/10\/Businessrule4-176x176.png 176w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/10\/Businessrule4.png 417w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Now to avoid our business rule running on existing record, we can simply apply a workaround here and can put a condition to set value only if createdon field does not contains any data, this way our business rule will only rule for new entity records \ud83d\ude42<br \/>\n<a href=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/10\/Businessrule5.png\"><img decoding=\"async\" loading=\"lazy\" class=\" size-medium wp-image-2816 aligncenter\" src=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/10\/Businessrule5-300x145.png\" alt=\"Businessrule5\" width=\"300\" height=\"145\" srcset=\"https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/10\/Businessrule5-300x145.png 300w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/10\/Businessrule5-624x301.png 624w, https:\/\/himbap.com\/blog\/wp-content\/uploads\/2017\/10\/Businessrule5.png 682w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Hope it will help someone !!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Requirement: Execute business rule only on create of entity record. Solution: As we know that business rules are executed on form load and on change of the associated field, which means if we have business rule for any entity it will fire for new record as well for existing records when form will load. If you are new to business&#8230; <a href=\"https:\/\/himbap.com\/blog\/?p=2811\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[21],"tags":[38,464,465,466],"_links":{"self":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2811"}],"collection":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2811"}],"version-history":[{"count":3,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2811\/revisions"}],"predecessor-version":[{"id":2819,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2811\/revisions\/2819"}],"wp:attachment":[{"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/himbap.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}