CRM 2013: Understanding Portable Business Logic
We are introducing a simple declarative interface to help you implement and maintain your fast changing, commonly used business rules. The rules will be applied to Main and Quick Create forms for the web application, Outlook and CRM for Tablets.Thats why we call them Portable.
Business rules are designed to allow you - even if you have no programming experience - to create client side scripting to run on forms. You will use a business rule editor to create and modify these Business Rules through the UI (Solution Explorer: Settings -> Customizations -> Customize the system -> [ENTITY] -> Business Rules)
The code behind the rules you create in the editor are bound by the client SDK and this will help reduce unsupported client customizations. Also, they are built on top of workflow infrastructure.
Conditions and Actions
Business rules consist of a collection of conditions and actions that can be used to enforce simple business logic of an organization.Conditions consist of a field, operator and operand (value, type or expression) that make up a logical statement that evaluates to true or false. If all the conditions of a rule evaluate to true, then the actions are executed.
What you can do with Actions
A subset of the capabilities provided by form scripts are available via the following Actions:
- Validate data and show error message
- Set field value
- Enable or disable field
- Show or Hide a field
- Set field requirement levels
Validate data and show error message rules
- Data in fields (as values) can be validated and comparison can be made between different fields.
- If the action states throwing an error message, you can define the control that should display the message.
Show or Hide a field rules
- You can change whether the field is displayed in the form. The options are Show Field and Hide Field.
- These are applicable to only fields and not for tabs, sections or controls like web resources and iframes.
- If you do not specify any conditions, this is similar to setting state by default.
Enable or disable field rules
- The options are Lock and Unlock. When the field is locked, you will not be able to edit the value in the field.
- If you do not specify any conditions, this is similar to locking/unlocking the field by default.
Set field value rules
- Choose the Field and Type. There are three Types:
- Field - Use this type to set the value of one form field with the value of another field.
- Value - Use this type to set the value of a form field with a value you enter.
- Formula - This option only appears for numerical or date data types. It does not appear for fields that contain text. Use this type to set the value to the result of a simple calculation that may use either a value in another form field or a value you enter.
- If you do not specify any condition, this is similar to setting a default value for the field.
Set field requirement rules
- The options are Not Business Required and Business Required. There is no option to set this to business recommended.
- If you do not specify any condition, this is similar to setting the default business requirement state.
To add a condition, click the "+" icon and a new condition row will appear with default values set.
Type in the field name to set the Field and then choose the appropriate Operator (see later). Operator options will change depending on the data type of the field.
You can chose three different types of conditions:
- Field - Use this type to compare the value of one form field with another.
- Value - Use this type to compare the value of one form field with a value you enter.
- Formula - This option only appears for numerical or date data types. It does not appear for fields that contain text. Use this type to compare the result of a simple calculation that may use either a value in another form field or a value you enter.
When you are finished entering or editing the rule, click the check mark icon to save it or the (X) icon to remove it.
An expression always has two operands and an operator in the following format: <operand1><operator> <operand2>
The operands can be two fields of same/similar data type or between a field and a constant. The business rule editor ensures that a mismatch does not occur.
An expression can return a value <operand1><arithmetic operator><operand2> when it is using a formula.(E.g.: AnnualRevenue SUBTRACT attribute1)
To illustrate the above I've created an example. In this example I create an entity ("Showcase") with the sole purpose of helping me do automatic calculations if I so choose.
If I pick Yes in a custom two option field "DoTheMath" my Business Rule will add the value I write in a custom whole number field "FirstInteger" to the value I write a custom whole number field "SecondInteger" and place the result in a custom field "Result".
The defailt view for my new entity I call "Calculations"
How its set up
I create a custom entity, and I select where I want to access it from - which modules in CRM
I also want to expose the entity in CRM for Tablets as well (see later) so I check the "CRM for Tablets" checkbox under the "Outlook and Mobile" section
I create four fields
- a two option field "DoTheMath"
- three whole number fields; "FirstInteger", "SecondInteger", and "Result"
I create a view with four extra columns corresponding to the four custom fields
I modify the main form to accomodate the custom fields
I add a new Business Rule using the "Business Rules" entry in the sitemap and the "New" button
My business rule "Calc This" is setup according to the screenshot below - if I have a "Yes" in the "DoTheMath" field AND values in the "FirstInteger" and "SecondInteger" fields, the rule will do the calculation for me
Note that when I pick the "DoTheMath" field in field chooser to the left in the editor, and "Value" in the "Type" chooser, the "Yes/No" values corresponding to the type for the "DoTheMath" field is automatically made available in the condition
I also want the "FirstInteger" and "SecondInteger" fields to contain data before calculations are done
Moving on to the Actions.
My rule should set the value of the field "Result", using a Formula ("FirstInteger" PLUS "SecondInteger").
First I pick "Result" in the Field chooser. Then I pick Formula in the Type chooser, to reveal a formula building block right below the Field/Type choosers.
In the formula building block I pick the field "FirstInteger", then the operator "+" and the type Field, which lets me pick the "SecondInteger" field in the lower rightmost Field chooser
I activate the rule and I'm ready to test the rule (remember to Activate - otherwise it won't fire)
Testing the rule, I pick "Yes" in the "DoTheMath" field and input numbers in the "FirstInteger" and "SecondInteger" field.
If all conditions are met, the moment I click in any field (change focus) the rule will perform its action
If I pick "No" in the "DoTheMath" field, the rule shall do nothing (note: at this point you probably want to do more, eg. hide/clear the "Result" field - see how in this additional example)
So the rule works as expected in the web client.
Remember I ticked the "CRM for Tablets" checkbox when I created the "Showcase" entity? I did that because I really like my new CRM for Tablets app, and I'd like to leverage my new entity "Showcase", including the business rule, in that app as well.
The default view in the CRM for Tablet app is the "Sales Dasboard", displaying six components ("My Activities", "My Open Opportunities" a.s.o)
In order to see my "Showcase" entity in the dashboard I'm going to replace the Lead component with my "Showcase" component.
To edit the "Sales Dashboard" shown in the CRM for Tablets I must go to the web client and customize the system dashboard "Sales Dashboard".
The "Sales Dashboard" has the same limitations as all other Dashboards, including a limit of 6 components. I cannot 'just' add another component for my entity, so I click the component I want to edit (the component currently displaying "My Open Leads")
I point the component to my custom "Showcase" entity
I check to see the changes has been saved and Publish
Next time I open my CRM for Tablets the server side configurations are downloaded and I now have my entity "Showcase" available to me in CRM for Tablets as well
I can now create new records of the "Showcase" type using the Commandbar button New Record (to display the commandbar on tablet I swipe up from the bottom of the screen - or rightclick if using a mouse)
And sure enough (thats why we call it portable business logic) my rule works as expected in CRM for Tablets too
I hope you agree with me that the Business Rules is a powerful addition to CRM for non-programmers - and I encourage you to give it a try yourself. You can easily create a 30 day free trial and give it a go. The CRM for Tablets app is free for licensed CRM users, and ready for you as well in selected online stores.
You might also want to check out the addition to this example - "Using Multiple Business Rules" to implement If-Then-Else logic (and some simple housekeeping).
- Business rules cannot be run across different entities. They have to be defined within the same entity.
- By default, the business rules are always created in a deactivated/draft state and must be activated to start executing for the form.
- The event handlers are determined by the platform based on the rule definition and involve the on-load event for the form and on-change events for the attributes being checked in the condition logic.
- A new privilege for “Activate Business rules” under customization tab for security roles has been added. Without activating the business rule first, it will not be executed
- Business Rules are imported/exported with the entities that they are attached to. It is not possible to export or import just a collection of business rules.
- If there are multiple solutions, business rules will be run in the order in which the solutions were imported and the latest installed solution’s rules will run last.
- If you package a managed/unmanaged solution, the states of the Business rules will be transported as is into target system: activated rules will be activated and deactivated rules will be deactivated
- There are no limits on number of actions that can be attached to the business rule.
- Business rules run only when the form loads and when field values change. They do not run when a record is saved.
- Business rules work only with fields. Form scripts can interact with other visible elements such as tabs and sections within the form
- When you set a field value using a business rule, any OnChange event handlers for that field will not run. This is to reduce the potential for a circular reference which could lead to an infinite loop.
- If a Business Rule references a field that is not present on a form, the rule will simply not run. There will be no error message on a main form or a quick create form due to null checks and control availability checks.
- There is no ability to connect to external data sources.
- Using Multiple Business Rules - link (adding another rule to the mix to implement "If-Then-Else logic")