user2463758
user2463758

Reputation: 67

Get Connected entities

I have a system whereby entity A is connected to B by a connection C. Different types of connection have different properties.

In C# using the Microsoft Dynamics CRM 2011 SDK, how to I look for all the connections (C) related to a record A which has just been updated and then update the record B at the other end of the connection, using info from the connection? There will be multiple connections for each record A.

Thank you

Upvotes: 0

Views: 1600

Answers (2)

MBender
MBender

Reputation: 5650

If I understand you correctly, you have a 1:N connection between two entities, and you want to make sure that some data from one entity is copied over (or somehow influences) the connected entities.

This is pretty straightforward, BUT you need to grasp some concepts first, because it seems you may not have much experience in coding for CRM 2011 (if you do, I apologize).

Here's a quick guide on how to start writing plugins:

  • Download the CRM 2011 developer SDK from here
  • Install the CRM Developer Tools (SDK\tools\...) for your Visual Studio (either 2010 or 2012)
  • Create a new VS project - make it a Dynamics CRM Plug-in Library and connect to your CRM
  • In the CRM Explorer window select the entity you want to "react" to - changing it will cause the other entities to update
  • Right-click - create a plugin.
  • Well, now you have some reading up to do on plugin registration in CRM 2011. I think you might need to create 3 plugins, actually: post-create, post-update and pre-delete. Coding post-update will benefit from using the post entity image.
  • Create proxy classes - in theory the Developer Tools can create proxy types, but they don't create a DataContext type class, and I think LINQ queries make life a LOT easier. You can generate the proxy classes using the SDK\bin\crmsvcutil.exe, just make sure to add the /serviceContextName parameter. Add the generated file to your project.
  • Well, at this point all that's left is to write the code. You should check the examples available in the SDK to get a better understanding on how to code CRM plugins.

I would write the plugin as follows:

  • Get the currently created (post image) / edited (post image) / deleted (pre image) entity
  • Query the CRM for all affected entities using the ID of the created/updated/deleted entity via relevant lookup field (something along the lines of ctx.new_bSet.Where(b => new_aid.Id == aEntity.Id).ToArray();).
  • Update each of the affected entities as needed.

EDIT:

Here's a sample code based on the pre-generated classes created via CRM Developer Tools on how a plugin might look like:

    protected void ExecutePostAccountCreate(LocalPluginContext localContext)
    {
        if (localContext == null)
        {
            throw new ArgumentNullException("localContext");
        }

        IPluginExecutionContext context = localContext.PluginExecutionContext;

        Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null;

        // TODO: Implement your custom Plug-in business logic.

        //create a data context - DataContext is the name I use - it might be different depending on your usage of crmsvcutil!
        var ctx = new DataContext(localContext.OrganizationService);

        //use the post image to get a strongly typed object
        //you can use aEntity to get whatever information you need for updating other entities
        var aEntity = postImageEntity.ToEntity<new_A>();

        //get the related entities (add using System.Linq!)
        //I'm assuming the relationship is A (1:N) B
        var bEntities = ctx.new_bSet.Where(e => e.new_aId.Id == aEntity.Id).ToArray();
        foreach (var bEntity in bEntities)
        {
            //set values for each of the entities here
            //for example: bEntity.new_field1 = aEntity.new_fieldBase;
            ctx.UpdateObject(bEntity);
        }
        ctx.SaveChanges();
    }

Upvotes: 2

patricgh
patricgh

Reputation: 403

First as you know you must create a Plug-in on update for record A so whenever Record A gets updated the plug-in is triggered. Then in the plugin you need to make a join using linq or any other retrieve method to fetch the connection relevant to record A by looking for the GUID in the connection entity it should be "Record1Id". when you get the connection then you can use GUId for Record B it should be "Record2ID". so when you get the record B then update it based on what you want form already fetched connection and update it.

The following code gives you all connetions related to recordA if you use LINQ and early binding

var connections = (from conn in context.CreateQuery<Connection>()
                                                      where (conn.Record1Id.Id == recordAid
                                                            select conn).ToList();

You can make any other filters for the connection if there is any! Hope it helps

Upvotes: 0

Related Questions