Search code examples
dynamics-crm-2016http-patch

Trouble updating a field of type lookup with Microsoft CRM 2016 Web API


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 = {"[email protected]" : "https://crmnaorgXXXX.crm.dynamics.com/api/data/v8.0/new_locations(1234578-a588-e511-8105-c4346bace18c)"}
payload = {"[email protected]" : "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.


Solution

  • 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