使用Web API向CRM自定义实体插入含Lookup字段记录时遇Bad Request错误
Let's break down the issue and fix that bad request error you're seeing. The core problem here is how you're formatting the lookup field bindings in your JSON payload.
What Was Wrong with Your Original Payload
You tried using new_contactid_contact@odata.bind and new_moveid_new_move@odata.bind, but that's not the correct format for Web API lookup bindings. The Web API expects you to use the navigation property name (not a modified version with entity names) followed by @odata.bind.
Correct JSON Payload
From the OData metadata you shared, the navigation properties for your lookups are new_contactid and new_moveid (check the <NavigationProperty> elements). Here's the corrected payload:
{ "new_transactionnumber":"114", "new_transactionamount":650, "new_transactiondate":"2018-01-29T15:01:00.000Z", "new_contactid@odata.bind":"/contacts(afb9e006-6be5-e711-80ed-bef806786223)", "new_moveid@odata.bind":"/new_moves(896f2dcd-b2fa-e711-80ef-bef806786223)" }
Why This Works
Looking at your OData metadata:
- The
<NavigationProperty Name="new_contactid" Type="mscrm.contact"...>explicitly defines thatnew_contactidis the navigation property linking to the contact entity. The@odata.bindsuffix tells the Web API to establish this relationship using the specified contact record GUID. - The
_new_contactid_valueproperty you see is a read-only computed field (marked with<Annotation Term="Org.OData.Core.V1.Computed" Bool="true" />), so you can't use it to set the lookup value—you must use the navigation property binding instead.
Additional Checks to Ensure Success
- Verify that the GUIDs for the contact and new_move records are valid and exist in your CRM instance.
- Make sure your POST request is targeting the correct endpoint (e.g.,
/api/data/v9.2/new_transactions—adjust the API version and entity set name as needed). - Ensure your request headers include
Content-Type: application/jsonand a valid authorization token.
内容的提问来源于stack exchange,提问作者I Love Stackoverflow

