Home > Uncategorized > Replicating CRM Records to another CRM System

Replicating CRM Records to another CRM System

A friend of mine called me up and asked if it was possible to create records in one CRM system when they are created in another one.  Well here is a quick example of how to do it.   

Assumptions

  • Both CRM Systems have the exact same fields for the records you will copying.
  • All records in the two CRM systems have the same guids for matching records.

If there are discrepencies with the guids in each system, there will be issues with this example.   When the postImageEntityXml is Deserialized into a DynamicEntity, it has the guid of the source system. When it’s created in the target system, it’s created with the same guid. 

For this example I will show you a simple Post Callout to replicate a Create action, but I recommend sending these transactions to a Message Queue and then processing them from there.   It’s also not only limited to Create, the same thing can be done for other actions.

Code

public override void PostCreate(CalloutUserContext userContext, CalloutEntityContext entityContext, string postImageEntityXml)
{
    //Create the Service and connect to remote server.
   CrmService service = new CrmService();
   service.Url = "http://<CRMSERVER>/mscrmservices/2006/crmservice.asmx"

   //build the credentials for access if the current user doesn’t have access.
   service.Credentials = new System.Net.NetworkCredential("username","password","domain");

    DynamicEntity entity = (DynamicEntity)Serialization.DeserializeBusinessEntity(postImageEntityXml);

    TargetCreateDynamic targetCreate = new TargetCreateDynamic();
    targetCreate.Entity = entity;

    CreateRequest request = new CreateRequest();

    request.Target = targetCreate;

    CRMSDK.CreateResponse response = (CRMSDK.CreateResponse) service.Execute(request);
}
private Object DeserializeBusinessEntity( string xml)
{
    XmlTextReader readerText = new XmlTextReader(new StringReader(xml));
    try
    {
    // De-serialize XML stream using .NET XmlSerializer class
    XmlRootAttribute root = new XmlRootAttribute("BusinessEntity");
    root.Namespace = "http://schemas.microsoft.com/crm/2006/WebServices"
    XmlSerializer xmlSerializer = new XmlSerializer(typeof(BusinessEntity), root);
    BusinessEntity entity = (BusinessEntity)xmlSerializer.Deserialize(readerText);

    return entity;
    }
    catch (Exception) { throw; }
}

This code isn’t entity specific so it will work for most of the entities in CRM. All the entity information is in the postImageEntityXml and is Deserialized into the DynamicEntity.

Callout Registration

<callout entity="account" event="PostCreate">
      <subscription assembly="Callout.dll" class="Callout.Migrate">
          <postvalue>@all</postvalue>
      </subscription>
</callout>

<callout entity="contact" event="PostCreate">
      <subscription assembly="Callout.Migration.dll" class="Callout.Migrate">
         <postvalue>@all</postvalue>
      </subscription>
</callout>  

There are a lot more things you can do to improve this process.   This is just a simple example.  Some other things to consider adding into your solution is Message Queueing and Exception Handling.

Hope this helps.

A big shout out to Henry who is out in Ohio this week :).

 

This customization may not be supported by Microsoft and is provided as-is with no warranty.

Categories: Uncategorized
  1. October 15, 2012 at 1:49 pm

    May I just say what a relief to find someone that actually understands what they’re discussing on the web. You actually understand how to bring an issue to light and make it important. A lot more people must read this and understand this side of the story. I can’t believe
    you are not more popular because you most certainly have the gift.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: