One question I get quite often in D365FO revolves around the ability to capture when a record is read or viewed within D365FO. The ‘read auditing’ feature is standard in other Dynamics 365 offerings but does not exist natively in D365FO, so what can we use to achieve this outcome? One possible solution is to use the telemetry framework available within D365FO, let’s look at how this can be done!
Custom Events Telemetry
There are a couple different telemetry data types you can enable within a D365FO environment once the feature has been enabled. You can configure which telemetry data events types to track at System Administration -> Setup -> Monitoring and telemetry parameters. Two of those events are ‘custom metrics’ and ‘custom traces’, these allow you to use X++ code to configure when a telemetry data entry should be sent to AppInsights and what data should be sent:
Let’s say we want track any time a sales order header is opened, by navigating to the form itself we can see it is using the SalesTableDetails menu item display and the SalesTable form:
We can also use the Form Information area to show up that this form is using the ‘SalesTable’ table as the data source:
Custom Telemetry Events X++ Code
Once we know the form and datasource we want to use we need to write a small piece of X++ code to provide the custom data for telemetry. Before we do that though we need to be sure our project is referencing the ‘MonitoringandTelemetry’ package, because we are going to utilize the SysGlobalTelemetry class:
Next we can go to the SalesTable form and look for the SalesTable datasource. If we look at the event sequence during a form load, the last event that fires is when the data source is marked ‘activated’. Let’s copy the event handler method:
And paste it into a custom class, there we can add in a short snippet of X++ code.
The first is to convert the FormDataSource sender to the SalesTable using the cursor() method (we can do this since the event handler will only capture the event when the SalesTable table is marked as ‘activated’ on the ‘SalesTable’ form.
The second line is to set up our event string we want to store in the telemetry data entry, here we can utilize the values from the SalesTable record itself (specifically the Sales Id and Sales Name).
And the third line actually sends the event to be logged to our AppInsights telemetry log using the SysGlobalTelemetery class logEvent method:
If we query the ‘customEvents’ object within our AppInsights container we can see this event is logged and the name of the event shows up the Sales Id and Sales Name values of the sales order we opened (along with the other standard telemetry data including date and time, user Id, and legal entity):
Using custom events in telemetry for D365FO for read auditing is just one of the many uses that it can be used for. I hope this blog post shows how powerful this functionality could be and how it could be applied within your D365FO environment.