Search This Blog

Sunday, October 22, 2017

RetrieveMultiple Plug-in not Firing when Exporting Records via the View (Dynamics CRM 2015)

Hi all,

Recently at work I came across an issue where business users reported getting different results for a specific entity when viewing results for a query in a view vs attempting to export records to a spreadsheet via the 'Export Records' feature.

A little investigation revealed that there was a plug-in registered on RetrieveMultiple message for that entity that was excluding a specific subset of records based on filter criteria defined in the plug-in in addition to the filter criteria defined in the view. This subset of records had more technical importance and less business importance and were irrelevant to the business users, hence the exclusion.

The result set was correctly displayed when viewed in the view. However when exporting the records, the filter criteria defined in the view was working but filter criteria in the plug-in was not firing, thus the subset of records was not getting excluded. Looking at the plug-in code, I noticed, the plug-in had a Maximum Depth defined to 1.

Take a look at the code snippet below:


1:      public void Execute(XrmPluginContext context)  
2:      {  
3:        var depth = context.PluginExecutionContext.Depth;  
4:        if (depth > 1)  
5:        {  
6:          return;  
7:        }  
8:        ProcessRetrieveMultiple(context);  
9:      }  

I changed the plug-in maximum allowed depth to 2 and re-registered the plug-in, like so:


1:      public void Execute(XrmPluginContext context)  
2:      {  
3:        var depth = context.PluginExecutionContext.Depth;  
4:        if (depth > 2)  
5:        {  
6:          return;  
7:        }  
8:        ProcessRetrieveMultiple(context);  
9:      }  

I re-registered the plug-in, refreshed the view and exported the records in the spreadsheet. This time, the result set in the view and the spreadsheet matched. Further research revealed that in CRM 2015 and onwards, exporting records triggers the plug-in with a depth of 2 in a single transaction context. I hope this helps anyone facing similar issue and looking for resolution.

Until next time, Happy CRMizing!

No comments: