Reputation: 67
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
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:
SDK\tools\...
) for your Visual Studio (either 2010 or 2012)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.I would write the plugin as follows:
ctx.new_bSet.Where(b => new_aid.Id == aEntity.Id).ToArray();
).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
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