CRM 4.0 Help plugin relationship N-N RRS feed

  • Question




    i have a problem to create records on relationship N-N with a plugin on POST Create contact.


    I want copy all contacts on a relationship N-N with accounts.


    I have created N-N on CRM. On the database, i see a new table new_contact_accountBase(contains new_contactaccountid,version number,contactid,accountid)


    I retrieve all account and for each i want created a record on this table with accountid and contactid. but i have an error


    Server was unable to process request.Itryto retrievemultiple on this new_contact_account  but same error.


    Here my code:


    Dim contactId As Guid = context.OutputParameters("id")

    Dim service As ICrmService = CType(context.CreateCrmService(False), ICrmService)

    Dim indice As Integer

    Dim info_contact As New contact

    info_contact = service.Retrieve(EntityName.contact.ToString, contactId, New AllColumns)

    Dim accounts As BusinessEntityCollection

    Dim query As New QueryExpression



    query.ColumnSet = New AllColumns

    query.EntityName = EntityName.account.ToString

    accounts = service.RetrieveMultiple(query)


    For indice = 0 To accounts.BusinessEntities.Count - 1


    Dim account_detail As account

    account_detail = accounts.BusinessEntities.Item(indice)

    Dim contactnn As New DynamicEntity

    contactnn.Name = "new_contact_account"


    Dim collection_property As New PropertyCollection

    Dim id_contact As New LookupProperty

    id_contact.Name = "contactid"

    id_contact.Value = New Lookup(EntityName.contact.ToString, contactId)

    Dim id_account As New LookupProperty

    id_account.Name = "accountid"

    id_account.Value = New Lookup(EntityName.account.ToString, account_detail.accountid.Value)




    contactnn.Properties = collection_property


    Dim mytarget As New TargetCreateDynamic

    mytarget.Entity = contactnn

    Dim create As New CreateRequest

    create.Target = mytarget

    Dim myresponse As New CreateResponse

    myresponse = service.Execute(create)





    Next indice



    Thanks for your help.



    Wednesday, March 12, 2008 10:08 AM


  • Hi esR,

    to create N:N relationship you need to use AssociateEntities message not Create.

    Code Snippet

    Dim request As Mew AssociateEntitiesRequest

    request.Moniker1 = New Moniker;
    request.Moniker1.Id = contactid;
    request.Moniker1.Name = EntityName.contact.ToString()

    request.Moniker2 = new Moniker;
    request.Moniker2.Id = account_detail.accountid.Value
    request.Moniker2.Name = EntityName.account.ToString()

    request.RelationshipName = "new_contact_account"


    Note: you do realise that you're pulling back and establishing relationship with all accounts from the database?

    Hope this helps

    Thursday, March 13, 2008 12:33 AM