Sunday, May 24, 2020

Are booking rules available only in hourly view in D365 CRM schedule board?

I think you have experience with schedule board in D365 CRM. If you need to create a booking rule, there are so many articles that you can fallow. Here is the link for MSDN article.

https://docs.microsoft.com/en-us/dynamics365/field-service/set-up-booking-rules

So, I'm not going to describe how to create a booking rule here. But I'm going to explain a limitation I faced and a workaround for that limitation.

I created a booking rule and noticed that booking rule is working only in hourly view. Hence I contacted Microsoft and below is their response.

"The booking rule only works in Schedule Board Hourly view, we have a plan to implement it for Daily, Weekly and Monthly view but those changes most probably will be available after those views will be re-designed."

Microsoft is currently redesigning the Schedule Board and we won't get this available soon.
Hence, below is my workaround for this.

Workaround :
Instead of using the Javascript as booking rule functionality, you can use a plugin on the create message of Bookable Resource Booking, my recommendation is to use the pre-validation or pre-operation phase in order to avoid sending the transaction to the DB, ideally the pre-validation is better.

By using the workaround:
Booking rule works in all the views and it shows a nice error message in all the views.
  • In all other views, except in hourly view, it shows the error message as below(This is the main advantage).

  • In hourly view, it shows the error message by suing the popup which is used in OOB booking rules. There is a drawback in hourly view, that the error message is not nice as in OOB booking rules. Hence I have proposed a solution for this under "Extend Solution" below.
  • It gives only the errors and does not have the ability to give warnings and allow to do the booking with warnings.

By using the OOB booking rules:

  • Main drawback is that this works only in hourly view.
  • The biggest advantage is that this gives a nice error message as well as it gives the opportunity to create the records with warnings.
    • Errors
    • Warnings - Allows to continue and do the booking or cancel.


Extended Solution :

Use both OOB booking rules and Pre-Validation plugin in bookable resource booking create.
  • Since the OOB booking rules uses JavaScript and JavaScript fires first, it gives you the nice error message in hourly view instead of the error message view have explained earlier. In other views it works with the plugin and gives the error message as I described earlier.
  • In hourly view, you get an extra ability to use the warning feature.

Have fun with booking rules😊


Tuesday, May 19, 2020

Implement multi level project approval for D365 PSA

In D365 PSA, we do not get the multi level approval functionality in the OOB time/expense approval process.
But I know some of our customers need to have multi level approvals for some of their daytoday business process(specially for expense approval).

When we click the approve button in project approval records(Either using the home ribbon button or form ribbon button), it invokes below OOB msdyn actions accordingly.

Expense approval
  • msdyn_ExpenseEntriesPendingApproval
  • msdyn_ExpenseEntriesApprove
Time Approval
  • msdyn_TimeEntriesPendingApproval
  • msdyn_TimeEntriesApprove

Below is my solution for extending the OOB project approval functionality to enable multi level approvals for PSA. In this article my main intention is just to give you an idea about how to extend above actions to implement multi level approvals. Hence you can use that concept and implement your own extended solution.

Solution:
  1. Create your own setting entity to configure and enable multi level approval("Approval Rule" is my entity). 
  2. Create your own entity to configure approval hierarchy("Approval Hierarchy" is my entity).
    • Has a 1:N relationship with Approval Rule entity.
    • Contains the approvers and the hierarchy.
  3. Create your own entity or add new fields to Project Approval entity to track your current approver and the status. (I prefer to use a new entity "Approval Tracking")
  4. Create a new plugin for post create event in Project Approval entity and create new approval tracking record/s accordingly.
  5. Create pre execute plugins accordingly for above mentioned msdyn actions and implement below logic.
    • In Pending Approval action
      • Remove the id/s form approval action unless it has passed through the hierarchy. (I have explain id removal process below under tips to remove ids form approval and the example.)
    • In Approve action
      • If the project approval record is not in the last approval stage according to the hierarchy, assign the project approval record to the next approver in the hierarchy.
      • Update relevant Approval Tracking entity record with the current stage.
      • Remove the id/s form approval action unless it has passed through the hierarchy. (I have explain id removal process below under tips to remove ids form approval  and the example)
Tips to remove ids form approval :
  • In expense approval actions, the entries for approval are passed to the actions as a string and the input parameter "ExpenseEntryIds" holds the value. 
  • In time approval actions, the entries for approval are passed to the actions as a string and the input parameter "TimeEntryIds" holds the value.
  • From home ribbon button click, it sends all the selected record ids as a comma separated string and from form ribbon button click it just send the relevant record id as a string.
Example :
Step 1: Write a new plugin for msdyn_ExpenseEntriesApprove action.
// Get the entry ids to process from input parameters. var entryIdsToProcess = localContext.PluginExecutionContext.InputParameters["ExpenseEntryIds"].ToString(); // Implement your own method with custom logic to remove expense ids which are not in final stage of the approval process. var newEntryIdsToProcess = RemoveEntryIds(localContext, entryIdsToProcess); // Update ExpenseEntryIds with newEntryIdsToProcess. localContext.PluginExecutionContext.InputParameters["ExpenseEntryIds"] = entryIdsToProcess;

Step 2: Register your plugin for for the pre operation of msdyn_ExpenseEntriesApprove action.


I hope using above steps, you can implement your own, multi level approval solution for PSA.
If you face any issue while implementing, please add a comment or send me a direct message. I'm happy to help you.

Happy coding 😊


Monday, March 2, 2020

Add event on subgrid refresh - Dynamics CRM.

You may need to execute some custom action on your parent form, when you add some records using quick create forms. As an example you may need to disable a field on work order form when you add work order incidents using quick create form.

In this case, you can add a subgrid event listener to your subgrid to perform your logic.
  1.  Create a javascript file as a web resource and add below functions to your javascript file.
  2. function AddSubgridEvents(executionContext) {
      var formContext = executionContext.getFormContext(); 
    
      addSubgridEventListener(formContext);
    } 
    
    function addSubgridEventListener(formContext){
      var gridContext = formContext.getControl("SUBGRID_ID");
      
      //ensure that the subgrid is ready…if not wait and call this function again
      if (gridContext == null){
         setTimeout(function () { addSubgridEventListener(formContext); }, 500);
         return;
      }
      
      //bind the event listener when the subgrid is ready
      gridContext.addOnLoad(subgridEventListener);
    }
    
    function subgridEventListener(gridContext){
        // Gets the form context using subgird context
        var formContext = gridContext.getFormContext();
    
        // Implement your logic here...
    }
  3. Include your javascript file in form libraries.
  4. Add your "AddSubgridEvents" function to Form Onload event handler and pass the execution context as a parameter.
Happy coding 😎