Friday, May 4, 2012

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

Dissassociate requests are kind of like the "unfriend" in Facebook, you are breaking off a relationship that is defined within CRM between two entities.

This illustration shows you how to Disassociate two entities in Microsoft Dynamics CRM 2011 using the disassociate request against a known relationship.   This example will be given in VB.NET with the DisassociateRequest message against the CRM 2011 organization service.  In this example we are simply disassociating a contact from 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 code looks like!

Dim req As New DisassociateRequest()

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

'RelatedEntities are the entities you are breaking the main entities association to (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 disassociate the entities. 
'found in settings - customization - entity - relationships
req.Relationship = New Relationship("contact_customer_accounts")

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

I hope this helps!

No comments:

Post a Comment