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.

 

 

 

Leave a Reply

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