We are using https://github.com/dangoscomb/pydynamics in Python to connect to our Dynamics 365 org.
We can create, select, update our entity sets.
import pydynamics
from pydynamics.client import Client
from pydynamics.querybuilder import QueryBuilder
token = tokenres.json()['access_token']
pdc = Client(token, endpoint=CREDENTIALS['resource'] + CREDENTIALS['api_base_url'] )
#======update contact (works fine) =========
q = QueryBuilder('contacts').guid('2bcd88c7-c295-ea11-a811-000d3a6546e3').\
data({'firstname': 'ChangedFirstName'})
pdc.update(q)
For associating entities we followed: https://docs.microsoft.com/en-us/powerapps/developer/common-data-service/webapi/associate-disassociate-entities-using-web-api
POST [Organization URI]/api/data/v9.0/accounts(00000000-0000-0000-0000-000000000002)/opportunity_customer_accounts/$ref HTTP/1.1
Content-Type: application/json
Accept: application/json
OData-MaxVersion: 4.0
OData-Version: 4.0
{
"@odata.id":"[Organization URI]/api/data/v9.0/opportunities(00000000-0000-0000-0000-000000000001)"
}
If we do it accordingly, we get errors that the json is invalid!
print(js)
{'@odata.id': '<ORG>/api/data/v9.1/opportunities(4585df90-4aa3-ea11-a812-000d3a6546e3)'}
import requests
query = 'contacts(ab906e8c-4aa3-ea11-a812-000d3a65473c)/opportunity_customer_contacts/$ref'
resp=requests.post(endpoint + query, headers=headers, data=js)
resp.json()
{'error': {'code': '0x0',
'message': 'An error occurred while validating input parameters: Microsoft.OData.ODataException: Invalid JSON. A token was not recognized in the JSON content.\r\n
Might there be a problem with this "@odata.id"?
The solution was simple. Microsofts documentation is not very precise. They write
{
"customerid_account@odata.bind":"[Organization URI]/api/data/v9.0/accounts(00000000-0000-0000-0000-000000000002)"
}
It does NOT need the "[Organization URI]/api/data/v9.0" Part.
Solution:
q = QueryBuilder('opportunities').guid(opport_guid).\
data({ "customerid_contact@odata.bind": f"/contacts({contact_guid})" })
DynamicsClient.update(q)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.