We may be wondering how to have a Past Date as Created on - on the records,
This is needed while importing legacy data and we want to have accurate Created on Date, especially if we are building Reports on this data.
The trick is to add the desired Created on Date on to the Overriddencreatedon field.
It is not possible to achieve this by using Import Wizard/ Plugins/WFs directly.
We can get it to work by using:
1)A Console Application , which creates the records, with the attribute Overriddencreatedon set to the desired past date in the Entity created.
2)A Console Application to create the records, and have a PreCreate Plugin/WF to add the desired past date to Overriddencreatedon attribute of the Target entity.
Below are some samples
1)Console App Only
Supply username, password and url in the app.config
<add key="username" value="example@example.onmicrosoft.com" />
<add key="password" value="password" />
<add key="url" value="https://example.crm.dynamics.com" />
//Code Follows as below
namespace sampleUsingOverriddencreatedon
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
/// <summary>
/// Class for the Core importer Logic
/// </summary>
public class ImporterLogic
{
/// <summary>
/// Adds past dated lead
/// </summary>
/// <returns>Output Message</returns>
public void createPastLead()
{
string connectionString = $"Url={RetrieveFromconfig("url")};Username={RetrieveFromconfig("username")};Password={RetrieveFromconfig("password")};AuthType=Office365;RequireNewInstance=true";
CrmServiceClient client = new CrmServiceClient(connectionString);
Entity lead = new Entity("lead");
lead["lastname"] = "Harri " + i;
lead["subject"] = "testoverridencreatedon";
lead["overriddencreatedon"] = new DateTime(year, i, 1);
client.Create(lead);
}
public static string RetrieveFromconfig(string key)
{
string value;
value = ConfigurationManager.AppSettings[key];
return value;
}
}
}
2) Pre-Create Plugin which would override the Created of the Record Created from any external application
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
namespace OverRidePlugin
{
public class UpdatedCreatedOn :IPlugin
{
public void Execute(IServiceProvider serviceProvider) {
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
if (context.InputParameters["Target"] is Entity && context.Depth==1) {
Entity entity = (Entity)context.InputParameters["Target"] ;
entity["overriddencreatedon"] = new DateTime(2012, 2, 22);
}
}
}
}
This is needed while importing legacy data and we want to have accurate Created on Date, especially if we are building Reports on this data.
The trick is to add the desired Created on Date on to the Overriddencreatedon field.
It is not possible to achieve this by using Import Wizard/ Plugins/WFs directly.
We can get it to work by using:
1)A Console Application , which creates the records, with the attribute Overriddencreatedon set to the desired past date in the Entity created.
2)A Console Application to create the records, and have a PreCreate Plugin/WF to add the desired past date to Overriddencreatedon attribute of the Target entity.
Below are some samples
1)Console App Only
Supply username, password and url in the app.config
<add key="username" value="example@example.onmicrosoft.com" />
<add key="password" value="password" />
<add key="url" value="https://example.crm.dynamics.com" />
//Code Follows as below
namespace sampleUsingOverriddencreatedon
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
/// <summary>
/// Class for the Core importer Logic
/// </summary>
public class ImporterLogic
{
/// <summary>
/// Adds past dated lead
/// </summary>
/// <returns>Output Message</returns>
public void createPastLead()
{
string connectionString = $"Url={RetrieveFromconfig("url")};Username={RetrieveFromconfig("username")};Password={RetrieveFromconfig("password")};AuthType=Office365;RequireNewInstance=true";
CrmServiceClient client = new CrmServiceClient(connectionString);
Entity lead = new Entity("lead");
lead["lastname"] = "Harri " + i;
lead["subject"] = "testoverridencreatedon";
lead["overriddencreatedon"] = new DateTime(year, i, 1);
client.Create(lead);
}
public static string RetrieveFromconfig(string key)
{
string value;
value = ConfigurationManager.AppSettings[key];
return value;
}
}
}
2) Pre-Create Plugin which would override the Created of the Record Created from any external application
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
namespace OverRidePlugin
{
public class UpdatedCreatedOn :IPlugin
{
public void Execute(IServiceProvider serviceProvider) {
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
if (context.InputParameters["Target"] is Entity && context.Depth==1) {
Entity entity = (Entity)context.InputParameters["Target"] ;
entity["overriddencreatedon"] = new DateTime(2012, 2, 22);
}
}
}
}
No comments:
Post a Comment