There is a bit of change in CRM 2011 than CRM 4. The code is below.
Microsoft.Xrm.Sdk.EntityReference Moniker1 = new Microsoft.Xrm.Sdk.EntityReference();
Moniker1.Id = ProductID;
Moniker1.Name = "product";//Entity Name
// Code Create Moniker for second Entity: New_CustomEntity
Microsoft.Xrm.Sdk.EntityReference Moniker2 = new Microsoft.Xrm.Sdk.EntityReference();
Moniker2.Id = contactid;
Moniker2.Name = "contact";//Entity Name
AssociateManyToManyEntityRecords(Moniker1, Moniker2, “product_contact”);
public bool AssociateManyToManyEntityRecords(Microsoft.Xrm.Sdk.EntityReference moniker1, Microsoft.Xrm.Sdk.EntityReference moniker2, string strEntityRelationshipName)
{
try
{
// Create an AssociateEntities request.
//Namespace is Microsoft.Crm.Sdk.Messages
AssociateEntitiesRequest request = new AssociateEntitiesRequest();
// Set the ID of Moniker1 to the ID of the lead.
request.Moniker1 = new EntityReference { Id = moniker1.Id, LogicalName = moniker1.Name };
// Set the ID of Moniker2 to the ID of the contact.
request.Moniker2 = new EntityReference { Id = moniker2.Id, LogicalName = moniker2.Name };
// Set the relationship name to associate on.
request.RelationshipName = strEntityRelationshipName; // Execute the request.
this.Service.Execute(request);
return true;
}
Hi Charith,
ReplyDeleteThe class AssociateEntitiesRequest in your example is Deprecated according to Msdn:
AssociateEntitiesRequest Class. Deprecated. Use AssociateRequest.
Namespace: Microsoft.Crm.Sdk.Messages
Assembly: Microsoft.Crm.Sdk.Proxy (in microsoft.crm.sdk.proxy.dll)
Source: http://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.associateentitiesrequest.aspx
Kind regards,
PPerry
Thanks PPerry for showing this out..
ReplyDeleteWill Try to change the code and re publish again..
Regards,
Charith
getting error when we use this code. pls help me
ReplyDeleteUsed AssociateRequest as mentioned in PPerry's post. Worked for me. Sample code:
ReplyDeleteAssociateRequest request = new AssociateRequest();
EntityReference mon1 = new EntityReference(targetEntity.LogicalName, targetEntity.Id);
EntityReference mon2 = new EntityReference(linkEntity.LogicalName, linkEntity.Id);
request.Target = mon1;
request.RelatedEntities = new EntityReferenceCollection { mon2 };
request.Relationship = new Relationship("relationshipName");
context.Execute(request);
Hello,
ReplyDeleteThanks for updating the code above. I will change the code in the post within this week.
Charith.
Thanks Charith. This code helped me a lot today :)
ReplyDeleteAshanth
Hi,
ReplyDeleteI'd like to modify "clone" message for product entity to clone associated many to many records, out of box it only clones the product record and not the relationships, can you help with this please?
Thanks,
Mary
Cool and I have a keen proposal: Whole Home Renovation Cost contractor for home renovation near me
ReplyDelete