Monday, January 30, 2012

Dealing with N:N Relationship in MS CRM - 2011

In the latest version of MS CRM 2011, the AssociateEntitiesRequest class has been deprecated. Now, we need to make use of AssociateRequest Class.

Here is an example to associate an entity record.

// Create the request object and set the monikers with the
// orderproduct_association relationship.
AssociateRequest productOrder = new AssociateRequest
{
    Target = new EntityReference(order.EntityLogicalName, orderId),
    RelatedEntities = new EntityReferenceCollection
    {
        new EntityReference(product.EntityLogicalName, productId)
    },
    Relationship = new Relationship("orderproduct_association")
};

// Execute the request.
_serviceProxy.Execute(productOrder);