Inkey Solution Logo
banner

Blogs

How to preserve Modified By and Modified On fields during Migration in Dynamics 365 for Sales

, April 16, 2021 6942 Views

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

mm

Inkey

INKEY is your solution partner.
Our focus is to deliver you in-time intelligent innovative solutions ("key") for the problems in hand. Maintaining a quality standard right from the inception of a project is our top most priority.

Our team of talented professionals will execute your projects with dedication and excellence. We take ownership and accountability for the effort that goes into meeting our client’s needs.

Years of experience and proven success of delivering innovative custom solutions.

More posts by

Leave a Reply

Your email address will not be published. Required fields are marked *

The maximum upload file size: 2 MB. You can upload: image, audio, video, document, spreadsheet, interactive, text, archive, code, other. Drop file here

Would you like to digitize your business and put it on the cloud?
Do you need clear, concise reports for your organization?