How to create and delete data from entity relationship many-to-many in CRM 2011?
Asked Answered
R

4

5

How to create and delete data from entity relationship many-to-many in crm 2011?

Code:

QueryExpression qry = new QueryExpression();
qry.EntityName = "entity1_entity2";
qry.ColumnSet = new ColumnSet(true);

var re = crmservice.RetrieveMultiple(qry).Entities;


crmservice.Delete("entity1_entity2", re[0].Id);

FaultException: The 'Delete' method does not support entities of type 'entity1_entity2'.

Regal answered 10/1, 2012 at 11:49 Comment(0)
S
6

In order to link two records via a N:N relationship, you have to use the Associate/Disassociate request or the corresponding methods of the service proxy.

This will create/delete the corresponding record of the entity1_entity2 entity.

Starflower answered 10/1, 2012 at 12:10 Comment(0)
N
5
using Microsoft.Crm.Sdk.Messages;
...
// get the crm service
...
AssociateEntitiesRequest fooToBar = new AssociateEntitiesRequest
{
    Moniker1 = foo,                // foo is an entity reference
    Moniker2 = bar,                // bar is an entity reference
    RelationshipName = "foo_bar",  // name of the relationship
}

service.Execute(fooToBar)          // relates foo and bar

Here's a blog post: http://charithrajapaksha.blogspot.com/2011/08/creating-many-to-many-records-in-crm.html

Northcliffe answered 27/1, 2012 at 20:27 Comment(0)
S
3

For Delete you try below

        // Create an AssociateEntities request.
        //Namespace is Microsoft.Crm.Sdk.Messages
        DisassociateEntitiesRequest request = new DisassociateEntitiesRequest();

        // 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.
        service.Execute(request);
Seng answered 11/9, 2014 at 12:44 Comment(0)
D
0

In N:N Relationships the records should be associated and disassociated. you can't create and delete records in N:N Relationship. you can use AssociateRequest, DisassociateRequest classes or you can use Associate, Disassociate Messages in Plugin Registration tool.

Dentiform answered 10/1, 2012 at 11:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.