Wednesday, 28 February 2024

Dynamics 365 Dataverse Change Tracking feature


Dynamics 365 Dataverse Change Tracking feature

Hi, Guys,

Today I would like to share the Change Tracking feature of Dynamics 365 CRM. 

Why enable Change Tracking?

Change Tracking enables incremental export, only records that have changed are exported.

In addition, you can export or retrieve a selected set of data, and then keep the external data warehouse in sync.

Suppose we have a lot of integrations of D365 CRM with other external systems. We need to keep track of changes done after the last synchronization of data and integrate only those changes to the downstream systems. For this scenario, Change Tracking is the best solution.

How to enable Change Tracking?

You can enable Change tracking for both system and custom tables(entities).

Once you enable the Track Changes you cannot disable it.

Using New UI Power Apps Settings

1, Expand Advanced Options.

    For the Create scenario, On the New table page.

    For the Update scenario, select a table, and then in Table Properties select Properties. In the Edit table page, expand Advanced Options.

2, In the For this table section, enable the Track Changes checkbox.

Dynamics 365 Dataverse Change Tracking feature

3, Save and Publish.

 

Using legacy settings

1, Navigate to Customizations-> Customize the System.

2, Select an entity, and under Data Services, select the Change Tracking checkbox.

Dynamics 365 Dataverse Change Tracking feature

How to custom query for Change Tracking

In this section, I will show you how to query the Chang Tracing data in the different scenarios and show the response data structure.

First of all, let's take a look at RetrieveEntityChangesRequest and RetrieveEntityChangesResponse classes.

RetrieveEntityChangesRequest

Input parameters

DataVersion is very import to only retrieve the changed data.

Set it as empty for the first time query, then return all records.

Dynamics 365 Dataverse Change Tracking feature

RetrieveEntityChangesResponse

Dynamics 365 Dataverse Change Tracking feature

Prerequisite

An entity enabled the Change Tracking feature.

Create Scenario:

1, Create a new record named Test1 

Test1

2, Execute below C# code to query the change data.

Request

 

We got 1 record as a NeworUpdatedEntity Item.

NeworUpdatedEntity

The message also returns a DataToken that we can use in the RetrieveEntityChangesRequest, so that when we execute this request the next time, it returns data for those changes that occurred since the last execution of the request.

DataToken

If retrieve again with the DataToken value, then no records were retrieved.

NoChanges

 

Update Scenario:

update the Name to Test1-a.

 Test1-a

then execute the query with the previous DataVersion.

Only 1 record was retrieved, and all the columns responded as the result.

Only 1 record was retrieved

 

Delete Scenario

Delete the previous record Test1-a and create a new record Test2.

Execute the query code with the previous DataVersion

2 items were retrieved, 1 is NewOrUpdatedEntity, and 1 for RemovedItem.

Dynamics 365 Dataverse Change Tracking feature

Multiple updates within the same record.

If we change the Name field from Test2 to Test2-1, then update to Test2-1-1.

The last change was responded to as the result.

The last change was responded

END

Hope this will help.

Thanks for reading.

Monday, 5 February 2024

Stop BPF stage movement : Restrict Manual Movement Of Stages Using JavaScript in BPF

 

Restrict Manual Movement Of Stages Using JavaScript in BPF

Introduction

In certain implementations, we must restrict manual movement of Stages in BPF for a selected record as a business requirement. To achieve this functionality, we can use JavaScript to restrict movement of next and previous stage.

Step 1

Login to the required environment and go to flows and select Business process flows – Vaccination and observe whether BPF is active or not, if not then activate it as shown in the   below figure.

Restrict Manual Movement of Stages using JavaScript in BPF Figure 1

Step 2

After Step 1, under contact customizations solution [my custom crm solution] create a web resource with name RestrictStages and publish all customizations as shown in the below figure.

Restrict Manual Movement of Stages using JavaScript in BPF Figure 2

Step 3

After Step 2, open  RestrictStages  webresource  as we are working on contact entity, provide the required unique name for identification , under form events provide the following method code under form_load method  get formcontext and register and call handleStage Movement method as an parameter to addOnPreStageChange method of formcontext and code looks like below

form_load: function (e)
{
    var formContext = e.getFormContext();
   // use the below code to remove a registered event handler.
   //formContext.data.process.removeOnPreStageChange(Contact.formEvents.handleStageMovement);
   formContext.data.process.addOnPreStageChange(Contact.formEvents.handleStageMovement);
}
JavaScript

as shown in the below figure.

Restrict Manual Movement of Stages using JavaScript in BPF Figure 3

Step 4

After Step 3, under handleStageMovement method, write down the logic to hold eventargs under a variable bpfArguments and write blocks to hold condition to fetch direction using getDirection() method with Previous and Next Strings as shown in the below code

handleStageMovement: function (e) {
    debugger;
    // get the event arguments
    var bpfArguments = e.getEventArgs();
    if (bpfArguments.getDirection() === "Previous")
    {
        // Write your custom logic like provide access to specific roles only.
    }
    if (bpfArguments.getDirection() === "Next")
    {
        // stop the stage movement
        // Write your custom logic like provide access to specific roles only.
     }
}
JavaScript

as shown in the below figure.

Restrict Manual Movement of Stages using JavaScript in BPF Figure 4

Step 5

After Step 4, under condition blocks of Previous and Next write the logic to undo the action with preventDefault() method and form a nice dialog to show user a meaningful information and stop process with the below code

if (bpfArguments.getDirection() === "Previous")
{
    // Write your custom logic like provide access to specific roles only.
    bpfArguments.preventDefault();
    var alertStrings = { confirmButtonLabel: "OK", text: "Back stage movement is not allowed", title: "Cannot Move to Prev Stage" };
    var alertOptions = { height: 200, width: 300 };
    Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
    return;
}
if (bpfArguments.getDirection() === "Next")
{
    // stop the stage movement
    // Write your custom logic like provide access to specific roles only.
    bpfArguments.preventDefault();
    alertStrings = { confirmButtonLabel: "OK", text: "Next stage movement is not allowed", title: "Cannot Move to Next Stage" };
    alertOptions = { height: 200, width: 300 };
    Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
    return;
}
JavaScript

as shown in the below figure.

Restrict Manual Movement of Stages using JavaScript in BPF Figure 5

Step 6

After Step 5, the full code looks like below

var Contact;
Contact = {};
Contact.formEvents = {
    form_load: function (e) {
        var formContext = e.getFormContext();
       // use the below code to remove a registered event handler.
       //formContext.data.process.removeOnPreStageChange(Contact.formEvents.handleStageMovement);
       formContext.data.process.addOnPreStageChange(Contact.formEvents.handleStageMovement);
    },
    handleStageMovement: function (e) {
       debugger;
       // get the event arguments
       var bpfArguments = e.getEventArgs();
      if (bpfArguments.getDirection() === "Previous")
      {
          // Write your custom logic like provide access to specific roles only.
          bpfArguments.preventDefault();
          var alertStrings = { confirmButtonLabel: "OK", text: "Back stage movement is not allowed", title: "Cannot Move to Prev Stage" };
          var alertOptions = { height: 200, width: 300 };
          Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
          return;
       }
       if (bpfArguments.getDirection() === "Next")
       {
           // stop the stage movement
           // Write your custom logic like provide access to specific roles only.
           bpfArguments.preventDefault();
           alertStrings = { confirmButtonLabel: "OK", text: "Next stage movement is not allowed", title: "Cannot Move to Next Stage" };
           alertOptions = { height: 200, width: 300 };
           Xrm.Navigation.openAlertDialog(alertStrings, alertOptions);
           return;
        }
    }
};
JavaScript

as shown in the below figure.

Restrict Manual Movement of Stages using JavaScript in BPF Figure 6

Step 7:

After Step 6, save and publish webresource and now navigate to contact form classic way and add this webresource- restrictstages javascript file and register under OnLoad Event and publish contact form and navigate back to Contact customizations solution and publish all customizations as shown in the below figure.

Restrict Manual Movement of Stages using JavaScript in BPF Figure 7

Step 8:

After Step 7, navigate back to contact entity and open contact record and try to manually try to change to next stage, user will encounter a popup as shown in the below figure

Restrict Manual Movement of Stages using JavaScript in BPF Figure 8

Note:

  1. In this article, I have concentrated on actual logic on javascript where placeholders are provided to get directions of BPF Stages moments.
  2. We can include in the logic like providing access to specific roles for movement of stages for others restriction, have not included role related logic was explained in earlier posts.
  3. Basic knowledge of registering and working with webresource is required to understand clearly.
  4. Even user cannot make previous stage as an active stage as shown in the below figure
  5. Auto save will also not happen if user clicks on any stage.

Restrict Manual Movement of Stages using JavaScript in BPF Figure 9

Conclusion: In this way, one can easily write JavaScript code to restrict direct Manual stage movement for a given business requirement with minimal code.


We can also do by Workflow.















Power Apps Drag & Drop Kanban code sample

  Introduction: The Kanban Board App Developers at a software company use the Kanban board to show their progress on development tasks and b...