Payman Biukaghazadeh
Payman Biukaghazadeh

Reputation: 237

Creating record in other databases from CRM 2011

Is there any solution to create record in other DBs from the CRM 2011 records? When a record such as "cost" was created in CRM 2011, we want a record would be created in out Oracle DB. Could it be done through a plugin? Or a service should be created for this? Could you please provide me references or solutions for this. Any helps would be greatly appreciated.

Upvotes: 2

Views: 1104

Answers (2)

glosrob
glosrob

Reputation: 6715

You could use a plugin to create a record in another system, although you would need to think about syncing and ensure you don't get duplicates, but it certainly can be done.

Tutorial on plugins can be found here.

You need to write a plugin that runs on Create and uses the information on the created Cost entity to create a record in your Oracle DB.

As an example:

public void Execute(IServiceProvider serviceProvider)
{
    var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

    //get the created entity from CRM
    var theCreatedEntity = context.InputParameters["Target"] as Entity;

    //build up a stored procedure call
    using (OracleConnection objConn = new OracleConnection("connection string"))
    {
        var cmd = new OracleCommand();
        cmd.Connection = objConn;
        cmd.CommandText = "stored procedure name";
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("param1", OracleType.Number).Value = theCreatedEntity.GetAttributeValue<int>("Attribute1");
        cmd.Parameters.Add("param2", OracleType.Number).Value = theCreatedEntity.GetAttributeValue<int>("Attribute2");

        //etc
        cmd.ExecuteNonQuery();
    }
}

That should give you enough to get going

Upvotes: 2

Konrad Viltersten
Konrad Viltersten

Reputation: 39118

We had a similar request from a customer a while ago. They claimed that CRM's database wasn't to be trusted and wanted to securely store a copy of the records created in - guess what - SQL Server too. (Yes, we do understand the irony. They didn't.)

The way we've resolved it was to create a plugin. However, bear in mind that simply reacting to the message of Create won't really do. You need to set up a listener for three of the CRUD operations (retrieval doesn't affect the external database so it's rather C_UD operations, then).

Here's the skeleton of the main Execute method.

public void Execute(IServiceProvider serviceProvider)
{
  Context = GetContextFromProvider(serviceProvider);
  Service = GetServiceFromProvider(serviceProvider);

  switch (Context.MessageName)
  {
    case "Create": ExecuteCreate(); break;
    case "Update": ExecuteUpdate(); break;
    case "Delete": ExecuteDelete(); break;
  }
}

After this dispatcher, you can implement the actual calls to the other database. There are three gotchas I'd like to give you head-up on.

  1. Remember to provide a suitable value to the outer DB when CRM doesn't offer you one.
  2. Register the plugin as asynchronous since you'll be talking to an external resource.
  3. Consider the problem with entity references, whether to store them recursively as well.

Walk-through for plugin construction
Link to CRM SDK if you haven't got that
Information on registering the plugin
And besides that, I've got a walk-through (including code and structure) on the subject in my blog. The URL to it, you'll have to figure out yourself - I'm not going to self-promote but it's got to do with my name and WP. Google is your friend. :)

Upvotes: 2

Related Questions