0

I'm using the ODataLib (http://odata.github.io) and the Client Generated Library to access Microsoft CRM's OData API (v9.0).

I'm trying to update the entity navigation property value (the GUID), but the update doesn't seem to do anything (no calls are made).

If I try to update the navigation property's value directly, I get an error saying that "CRM do not support direct update of Entity Reference properties, Use Navigation properties instead".

The entity is basically the middle entity in N:N relationship.

Basically what I'm doing in code is (semi pseudo-code):

Account a = _dao.GetAccount();

// This gets the dataservicecollection that tracks the changes
DataServiceCollection<MyRelationEntity> rel = _dao.GetMyRelationEntity();

rel.AccountId = a;

_dao.SaveChanges(SaveChangesOptions.PostOnlySetProperties);

Should I be using the AddLink, UpdateLink or something similar? They don't seem to do anything also.

I apologize if the terminology is not correct; I'm quite new to CRM.

kor_
  • 1,510
  • 1
  • 16
  • 36

1 Answers1

2

I know nothing about ODataLib, but from a Dynamics 365 perspective the operation you're looking for is Associate.

Here's a pseudo-code example of a D365 Web API call to associate an opportunity to an account. Notice that the URI contains the accountId and the relationship name, while the body contains the opportunity's URI.

POST [Organization URI]/api/data/v9.0/accounts(00000000-0000-0000-0000-000000000002)/opportunity_customer_accounts/$ref HTTP/1.1   
Content-Type: application/json   
Accept: application/json   
OData-MaxVersion: 4.0   
OData-Version: 4.0  

{  
"@odata.id":"[Organization URI]/api/data/v9.0/opportunities(00000000-0000-0000-0000-000000000001)"  
}  

This article has more info.

And, when working with the D365 Web API, I find Jason Lattimer's RESTBuilder to be an indispensable tool.

Aron
  • 3,877
  • 3
  • 14
  • 21