简体   繁体   中英

How do I create a Many to Many Relationship in CRM 2013 using Web Services

I have a many to many custom relationship company_a_c between Accounts and Contacts in CRM 2013. I am now trying to add a relationship via the oData (REST) web services, but have run into some difficulties. I created a standard c# proxy class to the oData web services.

Attempt 1 (create relationship object):

var crmRelationship = new company_a_c();
crmRelationship.accountid = account.AccountId;
crmRelationship.contactid = contact.ContactId;
crmService.AddTocompany_a_cSet(crmRelationship);
crmService.SaveChanges();

Result: Error "The 'Create' method does not support entities of type CRM 2013 'company_a_c'

Attempt 2 (create via objects):

account.company_A_C.Add(contact);
crmService.UpdateObject(account);
crmService.SaveChanges();

Result: No error thrown, but relationship not created

I'm sure I'm going about this the wrong way (new to oData) so any help as to the right way to setup these type of relationships would be appreciated.

try the following:

EntityReferenceCollection entityCollection = new EntityReferenceCollection();
entityCollection.Add(new EntityReference(Contact.EntityLogicalName, contact.ContactId));
Relationship crmRelationship = new Relationship(company_a_c.EntityLogicalName);
crmService.Associate(Account.EntityLogicalName, account.AccountId, crmRelationship, entityCollection);

Looks like SetLink was the wrong one, but AddLink was what I should have been after.

crmService.AddLink(account, "company_a_c", contact);
crmService.SaveChanges();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM