In this article I will go into detail of creating a Booking Rule and will also show an example of one.

 

Below is a working piece of source code which when a Resource Requirement is placed on the Schedule Board, runs a query to retrieve the Work Order ID and display it on the users screen.

 

function newBookingRule(sbContext)
{
 debugger;
 var ruleResult = {
 IsValid: false,
 Message: '',
 Type: 'error'
 };
 var resourceReqId = sbContext.newValues.ResourceRequirementId;
 var workOrderId = null;
 var req = new XMLHttpRequest();
 req.open("GET", encodeURI(Xrm.Page.context.getClientUrl() + "/api/data/v8.2/msdyn_resourcerequirements(" + resourceReqId + ")?$select=_msdyn_workorder_value"), false);
 req.setRequestHeader("Accept", "application/json");
 req.setRequestHeader("Content-Type", "application/json;charset=utf-8");
 req.setRequestHeader("OData-MaxVersion", "4.0");
 req.setRequestHeader("OData-Version", "4.0");
 req.setRequestHeader("Prefer", "odata.include-annotations=OData.Community.Display.V1.FormattedValue");
 req.send();
 if (req.readyState === 4)
 {
 req.onreadystatechange = null;
 if (req.status === 200)
 {
 var result = JSON.parse(req.response);
 workOrderId = result["_msdyn_workorder_value"];
 }
 else
 {
 Xrm.Utility.alertDialog(req.statusText);
 }
 }
 
 if (workOrderId != null)
 {
 ruleResult.IsValid = true;
 ruleResult.Message = "Success";
 ruleResult.Type = 'success';
 alert("WorkOrder Id: " + workOrderId);
 }
 else
 {
 ruleResult.IsValid = false;
 ruleResult.Message = 'No WorkOrder Id found';
 ruleResult.Type = 'error';
 }
 return ruleResult;
}

 

Firstly you will to create a new Web Resource and add it as a new Booking Rule. You will need to add the name of the method that you intend to run which in this case is ‘newBookingRule‘.

 

To trigger the booking rule you will need to drag a Resource Requirement onto an open slot on the schedule board.

 

My Booking rule will display an alert which will show the Work Order ID.

 

Now I will go into more detail on what my code is doing for more clarity.

As you can see I am passing a value called ‘sbContext‘ into my ‘newBookingRule’ method. Below you can see what type of values you have access to when this Booking rule gets executed. In this case I want to get the ‘ResourceRequirementId’ which I can use to get the Work Order ID later.

 

Below is the ‘ResourceRequirementId’ which i’m retrieving.

 

Next I will use that ‘ResourceRequirementId’ value and use it to execute a query to get the Work Order ID.

var req = new XMLHttpRequest(); 
req.open("GET", encodeURI(Xrm.Page.context.getClientUrl() + "/api/data/v8.2/msdyn_resourcerequirements(" + resourceReqId + ")?$select=_msdyn_workorder_value"), false); 
req.setRequestHeader("Accept", "application/json"); 
req.setRequestHeader("Content-Type", "application/json;charset=utf-8"); 
req.setRequestHeader("OData-MaxVersion", "4.0"); 
req.setRequestHeader("OData-Version", "4.0"); 
req.setRequestHeader("Prefer", "odata.include-annotations=OData.Community.Display.V1.FormattedValue"); 
req.send();

 

As you can see the query has worked and my ‘req.response‘ value is bringing back the JSON I need to get the Work Order ID.

 

Below is the WorkOrderId I am retrieving.

 

Finally the last piece of the code is for deciding if the Booking should be blocked or not. In this case I am checking if the ‘workOrderId’ contains a value. If it contains a value then go ahead and Book the Work Order and also show an alert displaying the Work Order ID.

 

That’s it! Hopefully with my basic example code you can go and achieve more advanced requirements. 🙂

 

I tested this Booking Rule in Dynamics 365 V9.

 

 

 

4 thoughts on “Field Service – Booking Rule Example”

  1. Thanks for the article Thomas! Any suggestions what would be the simplest way to automatically create a booking for an existing Work Order for a preferred resource? So the process is: agreement -> booking setup -> work orders (preferred resource is named) -> automatic booking of preferred resource.

    1. Hi Antti,

      I think this should be already be doable out of the box? If you set up a Booking Setup and select a preferred resource. Depending on what the Recurrence pattern is it should create a booking for you.

      If you are looking for something else. Then you could just create a booking via a workflow when a Booking Setup is created when a Preferred Resource contains data? Let me know if you need anything else.

  2. Hi Thomas.

    Thanks for the article. i have been trying to make one little js work whith the schedule boad but it seem it is not executing the script

    its a very simple script with an alert message but it dosnt work. i follow all the stept regarding to the booking rule and the format of the return object in the script but nothing so far. any hint would be much appreciated

    //**************************************
    function newBookingRule(sbContext)
    {
    debugger;
    var url = Xrm.Page.context.getClientUrl();
    var ruleResult = {
    IsValid = false,
    Message = ”,
    Type = ‘error’,
    };

    Xrm.Utility.alertDialog(“alert”);

    console.log(url);

    ruleResult.IsValid = false;
    ruleResult.Message = ‘Some Error Message Here.’;
    ruleResult.Type = ‘error’;

    return ruleResult;

    }

    1. Hi Anthony,

      Sorry for the late reply but I was very busy. Im not sure what was going wrong with your code but I have fixed your code below:


      function newBookingRule(sbContext)
      {
      debugger;
      var url = Xrm.Page.context.getClientUrl();
      var ruleResult = {
      IsValid: false,
      Message: '',
      Type: 'error'
      };

      Xrm.Utility.alertDialog("alert");

      console.log(url);

      ruleResult.IsValid = false;
      ruleResult.Message = "Some Error Message Here.";
      ruleResult.Type = "error";

      return ruleResult;

      }

Leave a Reply

Your email address will not be published. Required fields are marked *