0

I currently have some code that uses http patch to send Microsoft CRM data via the 2016 web api. When my payload includes a text or int datatype it works just fine, but when the payload includes a lookup record I can't get any response beyond 400 bad request.

Below are a few of the instances of payload that I've tried (with sensitive data altered)

payload = {"new_lastweblocation": "Midlothian" }
payload = {"new_location_transaction_LastWebLocationid@odata.bind" : "https://crmnaorgXXXX.crm.dynamics.com/api/data/v8.0/new_locations(1234578-a588-e511-8105-c4346bace18c)"}
payload = {"new_lastweblocation@odata.bind" : "https://crmnaorgXXXX.crm.dynamics.com/api/data/v8.0/new_locations(1234578-a588-e511-8105-c4346bace18c)"}

Essentially I've tried passing plaintext, a guid to the record, a guid to the relationship, a guid linked via odata.bind ... etc. Clearly my shotgun approach along with the 400 error means that I fundamentally misunderstand how entities are handled in the 2016 web api. Let me know if you have any suggestions.

Luis Gouveia
  • 8,334
  • 9
  • 46
  • 68
mucle6
  • 645
  • 1
  • 10
  • 24

2 Answers2

3

The approach listed on MSDN for associating entities on create also works when updating. I tested the following query in a 2016 demo environment without issues (where the guids have been replaced with existing account and contact guids, respectively):

PATCH [Organization URI]/api/data/v8.0/accounts/(00000000-0000-0000-0000-000000000001) HTTP/1.1
Content-Type: application/json; charset=utf-8
OData-MaxVersion: 4.0
OData-Version: 4.0
Accept: application/json

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

Could you start by verifying that this out-of-the-box use case works before debugging your specific issue with the lookup to a custom entity?

Henrik H
  • 5,747
  • 1
  • 21
  • 33
  • After reading the documentation again I tried a few things and ended up having to use a single-valued navigation property – mucle6 May 19 '16 at 14:01
2

I ended up using this request. The main issue that I was having is that I didn't know what the single-valued navigation property was. To find that I ended up using this request. I altered the select in the url to be select="*"

Original URL

GET [Organization URI]/api/data/v8.1/incidents(39dd0b31-ed8b-e511-80d2-00155d2a68d4)?$select=title,customerid_value&$expand=customerid_contact($select=fullname)

My URL

GET [Organization URI]/api/data/v8.1/incidents(39dd0b31-ed8b-e511-80d2-00155d2a68d4)?$select=*

When using the GET request to try and find the single-valued navigation property make sure to add 'Prefer':'odata.include-annotations"*". I couldn't get the preference header to pass until I put it before my authorization header.

Finally once I got a response from the get request I looked for the variable I was looking for with @Microsoft.Dynamics.CRM.associatednavigationproperty at the end of it and the used the value associated with that. In my case, the field name was new_lastweblocation but the single-value navigation property was new_LastWebLocation

Community
  • 1
  • 1
mucle6
  • 645
  • 1
  • 10
  • 24