Tuesday, April 24, 2012

How To Create a One-To Many Relationship Between Two Entities in Microsoft Dynamics CRM 2011 Using VB.NET

This illustration shows how to create a one-to-many relationship between two entities in Microsoft Dynamics CRM 2011 in code in VB.NET  using the CreateOneToManyRequest.

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

Dim req As New CreateOneToManyRequest()
req.OneToManyRelationship = New OneToManyRelationshipMetadata()
req.OneToManyRelationship.ReferencedEntity = "account"
req.OneToManyRelationship.ReferencingEntity = "contact"
req.OneToManyRelationship.SchemaName = "new_account_contact"

req.OneToManyRelationship.AssociatedMenuConfiguration = New AssociatedMenuConfiguration()
req.OneToManyRelationship.AssociatedMenuConfiguration.Behavior = AssociatedMenuBehavior.UseLabel
req.OneToManyRelationship.AssociatedMenuConfiguration.Group = AssociatedMenuGroup.Details
req.OneToManyRelationship.AssociatedMenuConfiguration.Label = New Label("Account", 1033)
req.OneToManyRelationship.AssociatedMenuConfiguration.Order = 10000

req.OneToManyRelationship.CascadeConfiguration = New CascadeConfiguration()
req.OneToManyRelationship.CascadeConfiguration.Delete = CascadeType.RemoveLink
req.OneToManyRelationship.CascadeConfiguration.Merge = CascadeType.NoCascade
req.OneToManyRelationship.CascadeConfiguration.Assign = CascadeType.NoCascade
req.OneToManyRelationship.CascadeConfiguration.Reparent = CascadeType.NoCascade
req.OneToManyRelationship.CascadeConfiguration.Share = CascadeType.NoCascade
req.OneToManyRelationship.CascadeConfiguration.Unshare = CascadeType.NoCascade

req.Lookup = New LookupAttributeMetadata()
req.Lookup.SchemaName = "new_parent_accountid"
req.Lookup.DisplayName = New Label("Account Lookup", 1033)
req.Lookup.RequiredLevel = New AttributeRequiredLevelManagedProperty(AttributeRequiredLevel.None)
req.Lookup.Description = New Label("Sample Lookup", 1033)

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



Thats all there is to it!

I hope this helps!

No comments:

Post a Comment