Enforcing business rules

For this recipe, we will focus on a different entity. Let's have a look at Opportunity. The Opportunity is the result of a qualified lead in many cases, but Dynamics CRM allows you to also add opportunities directly.

With opportunities, one of the fields we will focus on is the pipeline phase. For specific scenarios, a pipeline phase is tracked, customized, and enforced. Business rules can define the stages that define each pipeline phase, and specific rules that have to be met for an opportunity to progress to the next pipeline phase.

Getting ready

We can start by either reusing one of the previously created solutions or creating a new one. If you do not have a solution created, start by creating one.

How to do it...

We assume the following business rules

  • An Opportunity begins at 10 percent
  • An Opportunity progresses to 25 percent if the Rating field is set to Hot
  • An Opportunity progresses to 50 percent if a currency and price list are defined
  • An Opportunity progresses to 75 percent if a freight amount is defined

We will achieve this by using a combination of scripting and workflows. So let's get to it.

  1. Open your solution and add the Opportunity entity if not already added.
  2. Go to processes and add a new process. Configure it as described in the following screenshot:
    How to do it...
  3. Once you click on OK, the process information window opens up. Set the following items on this window:
    1. Set the Scope to Organization
    2. Set the Start when to Record is created
    3. Add an Update Record step
    4. In the Update make sure Opportunity is selected
    5. Click on Set Properties
  4. In the new form that opens find the Pipeline Phase field, and type in 10%, as shown in the following screenshot:
    How to do it...
  5. Click on Save and Close.
  6. Your form now should look similar to the following screenshot:
    How to do it...
  7. Click on Save and then Activate to activate your process.
  8. Click on Close.
  9. Now your processes window will show you the newly added process along with other processes previously added. Make sure that the Status shows Activated.
    How to do it...
  10. Once we have this process, let's open a new Opportunity, fill in the required fields, and click on Save and Close. Give it a moment for the workflow to execute.
  11. Reopening the opportunity you just created should show you the customized 10% value added in the footer of the page, as seen in the following screenshot:
    How to do it...
  12. Add a new workflow that updates the pipeline phase to 25 percent. Configure it to kick off when the rating field changes value. We check if the rating value is Hot, and then we update the pipeline phase to 25 percent.
    How to do it...
  13. Now we have to check if the Currency and Price List are defined. We will perform this check in JScript, and update a temporary field with a true/false value. Add a two-options field to the form, named new_progressto50.
  14. Insert the following script in a JScript resource, and associate it to the OnChange event of both the Currency (transactioncurrencyid) and Price List (pricelevelid) fields:
    function HasCurrencyAndPriceList()
    {
      var _currency = false;
      var _priceList = false;
      
      var currency = new Array();
      currency = Xrm.Page.getAttribute("transactioncurrencyid").getValue();
      if(currency != null)
      {
        _currency = true;
      }
      
      var priceList = new Array();
      priceList = Xrm.Page.getAttribute("").getValue();
      if(priceList != null)
      {
        _priceList = true;
      }
      
      if(_currency == true && _priceList == true)
      {
        // set temp field to true
        Xrm.Page.getAttribute("new_progressto50").setValue(true);
      }
    }
  15. Create a new workflow similar to the previous one, that starts when the Progress to 50% field is changed, checks the field value, and if set to True, updates the pipeline phase to 50%.
  16. Finally, add a new workflow that starts when the Freight Amount field is changed, checks if the pipeline phase is at 50% and if the amount contains a value greater or equal to 0.00, and updates the pipeline phase to 75%.
    How to do it...
  17. Save and Publish your solution.
  18. Test your solution by filling in progressively the required fields and follow how the pipeline phase updates automatically.

How it works...

The solution presented here enforces business rules through the use of workflows, and shows an example where working with JScript in conjunction with workflows can achieve the expected result.

For the sake of simplicity, I have demonstrated the creation of four different workflows, one for each phase progression. In real life, you would build all these rules in a single workflow. Additionally, you would probably build a reversed process that would downgrade the pipeline phase if a system value is changed to a value that does not allow the opportunity to pass a lower threshold.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.60.166