In this blog I’ll be taking you over how to preserve “Modified On” and “Modified By” fields during migration or data import. Generally, when we migrate the data, the records’ “Modified On” field value takes system local time and in the “Modified By” field it takes Current logged in User. But our requirement is when migrating email entity data, we preserve “Modified On” and “Modified By” fields values.
We write Pre Plugin on update. In that we get values of “Modified On” and “Modified By” fields value from EntityImage and set those image values in context entity’s “Modified By” and “Modified On” field. I have provided the required code of implementation as below:
using Microsoft.Xrm.Sdk; using System; namespace EmailUpdatePreserveModifiedFields { public class PreEmailUpdatePreserveModifiedFields : IPlugin { public void Execute(IServiceProvider iServiceProvider) { // Obtain the execution context from the service provider. IPluginExecutionContext iPluginExecutionContext = (IPluginExecutionContext)iServiceProvider.GetService(typeof(IPluginExecutionContext)); // Obtain the organization service factory reference. IOrganizationServiceFactory iOrganizationServiceFactory = (IOrganizationServiceFactory)iServiceProvider.GetService(typeof(IOrganizationServiceFactory)); // Obtain the tracing service reference. ITracingService iTracingService = (ITracingService)iServiceProvider.GetService(typeof(ITracingService)); // Obtain the organization service reference. IOrganizationService iOrganizationService = iOrganizationServiceFactory.CreateOrganizationService(iPluginExecutionContext.UserId); try { if (Plugin.ValidateTargetAsEntity("email", iPluginExecutionContext)) { Entity emailEntity = (Entity)iPluginExecutionContext.InputParameters["Target"]; if (emailEntity != null) { string preImageAlias = "PreEmailUpdatePreserveModifiedFieldsPreImage"; Entity emailEntityPreImage = (Entity)iPluginExecutionContext.PreEntityImages(preImageAlias); if (emailEntityPreImage != null) { DateTime modifiedOn = emailEntityPreImage.GetAttributeValue<DateTime>("modifiedon"); EntityReference modifiedBy = emailEntityPreImage.GetAttributeValue<EntityReference>("modifiedby"); emailEntity.SetAttributeValue<DateTime>("modifiedon", modifiedOn); emailEntity.SetAttributeValue<EntityReference>("modifiedby", modifiedBy); } } } } catch (Exception ex) { throw new InvalidPluginExecutionException(ex.Message); } } } }
So, this blog is short and hopefully sweet.
Hope this helps
ATM Inspection PowerApp to ease ATM inspection and report generation process.
https://www.inkeysolutions.com/microsoft-power-platform/power-app/atm-inspection
Insert data into Many-to-Many relationship in Dynamics CRM very easily & quickly, using the Drag and drop listbox.
http://www.inkeysolutions.com/what-we-do/dynamicscrmaddons/drag-and-drop-listbox
Comply your Lead, Contact, and User entities of D365 CRM with GDPR compliance using the GDPR add-on.
https://www.inkeysolutions.com/microsoft-dynamics-365/dynamicscrmaddons/gdpr
Create a personal / system view in Dynamics CRM with all the fields on the form/s which you select for a particular entity using the View Creator.
http://www.inkeysolutions.com/what-we-do/dynamicscrmaddons/view-creator
© All Rights Reserved. Inkey IT Solutions Pvt. Ltd. 2024
Leave a Reply