Dynamics CRM 2013: Diving into the sales pipeline – Part 3

In my previous post of this series, we discussed how to implement ACME’s sales pipeline through the creation of a business process flow (BPF) considering both the Lead and Opportunity entities. We created two business processes, one for opportunities that originated from leads, and another for opportunities that did not originated from leads.

In this post we will discuss how to implement ACME’s opportunity probability matrix through the creation of business rules.

Understanding business rules

Introduced in Dynamics CRM 2013, business rules allow the creation of simple form logic through a simple user interface, without writing JScript code. Business rules allow us to sett field values, set field requirement levels, show or hide fields, enable or disable fields, validate data and show error messages.

It is important to clarify that business rules work at the form controls level. In other words, users must interact with controls within the forms in order to for the business rules to take place. Also fatuously, business rules do not work with mobile forms and according to Microsoft mobile forms do not support business logic (source and follow-up). Introduced in Dynamics CRM 2013, real-time (synchronous) workflows can be used as a complement or alternative to support mobile forms. Although such workflows are not in the scope of this post, I might cover them in a future post.

So let’s start by customising our opportunity form.

Opportunity form: Adding and editing fields

  1. In Dynamics CRM 2013, both the Rating (opportunityratingcode) and the Probability (opportunityratingcode) fields are no longer present in the opportunity form by default. Add both of these fields to the form in a section of your choice. I suggest adding both fields between Purchase Process and Description fields located in the Opportunity Information section under the Summary tab; but this is really up to you.
  2. Double-click the Probability field to change its properties. Under the Display, select the option Field is read-only.
  3. Now add the Pipeline Phase field (stepname) just under the Probability field, which is the field used by the Sales Pipeline Funnel Chart. Change the properties of the field and make sure that under Visibility, the field is not selected as visible by default.

Our Goal: What we are going to achieve here is that when users rate an opportunity record using the Rating field as either hot, warm or cold, the value of the Probability field will change depending on the stage (i.e. phase) the opportunity is at in our custom BPFs. We made the Probability field read-only because its value will be automatically calculated based on the combination of values from the Pipeline Phase and Rating fields, following our probability matrix we defined in the previous post. We added the Pipeline Phase as a hidden field to the form in order for our business rules to work with the field (which we will create next).

Adding our matrix logic: Creating the business rules

In order to achieve the goal mentioned above, we must add some business rules that will work with the customisation we did so far.

There are two paths for accessing the business rule editor in Dynamics CRM 2013. Either through Business Rule under the entity we want to customise through a solution, or by clicking the Business Rule button at the ribbon when customising an entity form. I am assuming you still have the Opportunity form editor open so let’s proceed with the latter path.

  1. Still at the Opportunity form editor click on the Business Rules button at located at the Ribbon to display the Business Rule Explorer pane at the right-side of the form editor.
  2. At the Business Rule Explorer, click on New Business Rule. A new business rule editor window will pop-up.
  3. How many rules do we need?
    We are going to create three business rules for each stage that is within the scope of the Opportunity entity (the Lead entity does not count). So in this case we have four possible stages times three possible ratings, totalling twelve business rules.

  4. So let’s start with the rule for Cold Prospects. In the text box where it says Enter Rule Name, provide the following name: Cold x Prospect = 5%.
  5. Under CONDITION, click on the + sign to add a new condition. Now add the following values:
    Field: Rating
    Operator: Equals
    Type: Value
    Field: Cold

    Then click on the green (check-mark) button to confirm.

  6. Again under CONDITION, click on the + sign to add a new condition. This time add the following values:
    Field: Pipeline Phase
    Operator: Equals
    Type: Value
    Field: 1-Prospect (see below)

    Note that the value here is is based on the name and ordinal value of the prospect stage. You must type this value by hand here, and if you add or remove stages to your BPF you will have to revise all of your business rules. Click on the green (check-mark) button to confirm this condition as well.

  7. Now under ACTION, click on the + sign to add a new action. Then add the following values:
    Field: Probability
    Type: Value
    Field: 5 (see below)

    Note that the value here is the percentage value we want to set base on our rating and pipeline phase combination (in this case, a rating of Cold combined with a pipeline phase of 1-Prospect gives us a probability value of 5. Click on the green (check-mark) button to confirm.

  8. At the top-right corner of the form editor there is a button entitled Scope followed by the form name (i.e.: Opportunity). Click on the form name and choose All Forms to ensure that our business rule will work with other forms as well.
  9. Finally, click on SAVE to save our business rule, then click on ACTIVATE to enable it. Do not close the business rule form yet.

Now we need to create the remaining eleven business rules. As a shortcut, click on SAVE AS to make a copy of your business rule. Then repeat steps 3 to 7, making sure you edit the existing conditions for the Rating field and the Pipeline Phase field, as well as the action for the Probability field in order to cover the entire probability matrix as defined in the first post of this series. Also do not forget to name the business accordingly (see step 3) and to change the scope of the business rule to all forms (see step 7). Also remember to save and activate your business rule (see step 8) before clicking on SAVE AS to make another business rule.

Once we create all of our business rules, this is how the Opportunity form should look like:

Now that we have all twelve business rules, there is one final step we must take before we test our solution. What business rules do for us is to create the required JScript through a point-and-click interface so we do not have to code for ourselves. At the time of writing this post business rules is a new feature still at its infancy, so there are certain aspects of JScript that the business rules editor does not cover.

The case for a small JScript

If we test our solution now we will notice that although the probability matrix works for changing the probability value according to the rating and the pipeline phase, the value is not saved — even if we click the save button at bottom-right corner of the form. We need a small JScript to solve this issue.

  1. Click the Form Properties button located in the Ribbon. In the Form Properties dialog under the Events tab, click on Add under the Forms Libraries section and click on New at the Look Up Record dialog to create a new web resource.
  2. At the new web resource form, name the resource as new_SalesPipeline and set its type as Script (JScript).
  3. Now click on the Text Editor button to open the editor window, then dd the following code:
  4. salesPipelineProbCalc = function() {
      var CRM_FORM_TYPE = Xrm.Page.ui.getFormType();
      if (CRM_FORM_TYPE == 4 || CRM_FORM_TYPE == 3) { return; }
    
    	// Set the submition mode of closeprobability (which is set as read-only) in order to commit the value when saving the record
    	var closeprobabilityAttr = Xrm.Page.getAttribute("closeprobability");
    	closeprobabilityAttr.setSubmitMode("always");
    }
    
  5. Click on OK to return to the new web resource form, then click on SAVE then close the form.
  6. Back at the Look Up Record dialog, select our newly created resource then click on Add. The dialog will close and we should now see the Form Properties window with the new_SalesPipeline and set its type as Script (JScript) listed under Form Libraries.
  7. Under Event Handlers make sure that Control is set to Form and Event is set to OnLoad. Then click on the Add buton.
  8. On the Handler Properties dialog, where it says Library, select new_SalesPipeline. Where it says Function, type in SalesPipelineProbCalc, which is the name of the function we created in the new_SalesPipeline web resource. Then click on OK, then OK again in the previous dialog to confirm our customisation.
  9. Save the form and publish the customisation.

What we just did: By default Dynamics CRM does not commit changes performed in read-only fields. Therefore even if we can see the probability value being calculated on the fly, the value would never be saved on the Probability field of a given record since the Probability field is set as read-only in our form. By binding the SalesPipelineProbCalc function of our web resource to the onChange even of the form, we re overriding this behaviour, thus allowing the automatic calculation of the probability value to be committed to the record.

Testing our solution

When can now go ahead and test our solution by creating an opportunity. We can give our opportunity the rating of hot, warm or cold, and along with the sales stage the opportunity is at, the system will automatically calculate the probability for closing the opportunity.

Coming Up Next: ?

I have been tied up with deadlines and assignments for both work and university, so I have not thought through what would be my next step in this series of posts for Dynamics CRM 2013. I initially though the next post should be about workflows to support the default sales pipeline report (like I did with Dynamic CRM 2011). However there is also real-time workflows which I was hoping to cover in this series. Anyway the next post is something I will need to think about so stay tuned for updates. I will let you all know when I come up with something here at my blog, and also through my twitter (@pinnecco) account.

Acknowledgements:
Special thanks goes to my good friend (hyvä ystävä) Jukka Niiranen (Dynamics CRM MVP), who helped in this particular post with some testing and clarifying some issues around business rules.

Tags: , , , ,

7 Comments

Blog Categories

Recent Posts