Thursday, May 3, 2012

Associate Two Entity Instances in Microsoft Dynamics CRM 2011 Using VB.NET

This illustration shows you how to associate two entities in Microsoft Dynamics CRM 2011 using the associate request against a known relationship.   This example will be given in VB.NET (.NET) with the Associate message against the CRM 2011 organization service.  In this example we are simply associating a contact to an account as it's parent customer using the existing "contact_customer_accounts" relationship in CRM.

You can find the relationship schema name to use in the request by going to settings - customization - your entity - relationships.  Here is an example of where to find it.


Ok, here is what the calls look like!
In VB.NET:

 Dim req As New AssociateRequest()

 'Target is the entity that you are associating your entities to.
 req.Target = New EntityReference("account", New Guid("063DE6F9-2E6B-E111-B3CA-1CC1DEF1B5FF"))

 'RelatedEntities are the entities you are associating to your target (can be more than 1 depending on type of association)
 req.RelatedEntities = New EntityReferenceCollection()
 req.RelatedEntities.Add(New EntityReference("contact", New Guid("C81416E4-9089-E111-ACD4-1CC1DEF1B5FF")))

 'The relationship schema name in CRM you are using to associate the entities. 
 'found in settings - customization - entity - relationships
 req.Relationship = New Relationship("contact_customer_accounts")

 Dim resp As AssociateResponse = DirectCast(service.Execute(req), AssociateResponse)



I hope this helps!

1 comment:

  1. this code is working
    I have add this code in VB.net Script
    can you please tell me. which dll added to get AssociateRequest() class?

    ReplyDelete