This question is related to Microsoft Dynamics CRM 2015, that I'm calling through API.
I create contact entity:
POST [organization URI]/api/data/contacts
Content-Type: application/json; charset=utf-8
Accept: application/json
{
"emailaddress1": "myemail@example.com",
}
It works, I see new record, after I log into the panel. And I can call it through the API:
[organization URI]/api/data/contacts(f76e4e7c-ea61-e511-80fd-3863bb342b00)
{
"@odata.context":"[organization URI]/api/data/$metadata#contacts/$entity",
"@odata.etag":"W/\"460199\"",
...
"contactid":"f76e4e7c-ea61-e511-80fd-3863bb342b00",
"emailaddress1":"myemail@example.com",
....
}
Next thing I want to do, is to add annotation record associated with that contact. Following the guide I call:
POST [organization URI]/api/data/annotations
Content-Type: application/json; charset=utf-8
Accept: application/json
{
"notetext": "TEST",
'contact@odata.bind': 'contacts(f76e4e7c-ea61-e511-80fd-3863bb342b00)'
}
But it returns 400 error:
An undeclared property 'contact' which only has property annotations in the payload but no property value was found in the payload. In OData, only declared navigation properties and declared named streams can be represented as properties without values.
When I call:
POST [organization URI]/api/data/annotations
Content-Type: application/json; charset=utf-8
Accept: application/json
{
"notetext": "TEST",
}
New entity is created, but without a relation to contact.
How to properly compose this POST request? What am I missing here?
I suspect, that contact@odata.bind
should be presented somehow different, I've tried contactid@odata.bind
, object@odata.bind
, objectid@odata.bind
- but no effects.
Any ideas?
You can use following.
In most of the record you will get _contactid_value as a parameter name. So you have to pass like contactid_entityname@odata.bind as a parameter and in the value you have to pass 'EntitySetName' which would be contacts and GUID. '/EntitysetName(GUID)' So the value will be '/contacts(f76e4e7c-ea61-e511-80fd-3863bb342b00)'
I'm using this C# Code for creating and linking (the Task.Await stuff is not very clever, so... be careful) :
The JSON is looking like this:
might be a bit late for this, but the answer in the following link explains how the binding works really well.
basically, you need to use the field schema name with the suffix @odata.bind and the value being "/entityschemaname(recordGUID)" good to remember that the entityschemaname needs to have an 's' and the recordGUID should not have the curly brackets.
for more information follow this link below where i got this information from
'An undeclared property' when trying to create record via Web API
Instead of using
objectid@odata.bind
, you have to useobjectid_contact@odata.bind
. This results are in:To get the list of properties, look under the single-valued navigation properties in the documentation.
Part 1:
MSDN Reference: Deep Insert
Below code is to create Account (1), create + associate Primary contact (2), create & Associate Opportunity (3) and create + associate Task (4)
Part 2:
Associating annotation to contact uses the below syntax.
Refer SO link & blog
Part 3:
Answer to your comment on another answer about
annotation_id_from_first_request
:To get the created record Id in response from last request, you can parse like below:
You can read more
MSDN Reference: Create with data returned
Update:
If anyone looking for working payload sample to deep insert a record + annotation, the below is from my project:
This answer applies for web api usage:
If the references property has been defined using uppercase letters, you have to use uppercase letters in the property on update and insert. Look at the Schema name in the property list of the primary entity.
Lets say you have an entity called
myprefix_entity
with a reference to the account entity, and you named itAccount
, and the schema name becamemyprefix_AccountId
, you would have to refer it as:The uppercase A and the uppercase I in
myprefix_AccountId
matters, if that is how the schema name has been defined.