'An undeclared property' when trying to create record via Web API

你离开我真会死。 提交于 2019-12-01 17:58:40

问题


I am getting an error which I just cannot seem to debug. I am trying to create a custom activity entity via custom HTML/JavaScript web resource.

The user clicks a button and the following params:

var params = {
    'rob_faqid@odata.bind': '/rob_faqs(guid-here)',
    'rob_source': 180840000,
    'subject': 'Signpost',
    'actualstart': new Date(),
    'actualend': new Date()
};

Are passed to this URL:

https://dynamicsorg/api/data/v8.2/rob_quickactions/

With the following headers:

xhr.setRequestHeader('OData-MaxVersion', '4.0');
xhr.setRequestHeader('OData-Version', '4.0');
xhr.setRequestHeader('Accept', 'application/json');
xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
xhr.setRequestHeader('Prefer', 'return=representation');

This gives me a HTTP code of 400 (bad request) and this error message:

An undeclared property 'rob_faqid' 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.

Interestingly, I get this error whether I use an actual GUID or if I put some gibberish in there (suggesting it is not to do with the value being passed in).

I can create the records manually via the standard form.

I am using the odata.bind elsewhere within the same project with no errors.


回答1:


After a good night's sleep I realised my error. To set the value of a lookup field, you need to use the relationship scheme name, and not the property name.

Once I changed that, all worked fine.




回答2:


When you want to set the value of a lookup field during the creation or update of a (new) record via the web API, you have to use either the Schema Name or the Logical Name of the lookup followed by the bind annotation.

  • For default fields like primarycontactid the logical name has to be used (first column in the screenshot).
  • For custom fields like rob_FaqId the schema name has to be used (second column in the screenshot).
var params = {
    'rob_FaqId@odata.bind': '/rob_faqs(guid-here)',
    'rob_source': 180840000,
    'subject': 'Signpost',
    'actualstart': new Date(),
    'actualend': new Date()
};

Screenshot of a solution > entities > your entity > fields:

So the general structure to create a new record with an already set lookup field via the web API is this:

{
  "logicalorschemaName@odata.bind": "/relatedentitys(guid)" //don't forget the plural 's'
}

Or another example from the official documentation. How to create a new account record and directly assign an already existing contact as the primary contact.

var newAccountRecordObj = {
  "name": "Sample Account",
  "primarycontactid@odata.bind": "/contacts(00000000-0000-0000-0000-000000000001)"
}


来源:https://stackoverflow.com/questions/43970292/an-undeclared-property-when-trying-to-create-record-via-web-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!