microsoft-dynamicsdataversepower-platform

How do I POST data to dataverse lookup column?


The code looks like that:

jsonlistdf  = {'column1': '0000098778', 'lookupcolumn': '5187', 'column2': 'Lodon', 'column3': '8000'}

   conn = http.client.HTTPSConnection("{}.api.crm4.dynamics.com".format(url))

headers = {
    'Authorization': 'Bearer ' +accesstoken,
    'OData-MaxVersion': '4.0',  
    'OData-Version': '4.0',  
    'Accept': 'application/json',
    'Content-Type': 'application/json; charset=utf-8',
    'Prefer': 'return=representation'

}

for i in jsonlistdf:
  data = json.dumps(i, default=str)
  response = conn.request("POST", "/api/data/v9.2/tablewithlookupcolumn", data, headers)
  res = conn.getresponse()
  data = res.read()
  print(res.status, res.reason)

When I remove the lookupcolumn from the data, it will be posted to dataverse without any problems.

I have understood from the documentation that i need to use the GUI from the lookup column table. The lookup column table is account table. In account table there is the accountid_guid. So how should I use it and where ?


Solution

  • So it goes like that:

    The look up table is connected with some other table so you can't directly post any messages there, what you can do is get the GUID from the other dataverse table.

    Then you create a away to connect your data here is an example:

    In my case the source table row GUID which needs to match with other table is "436047e5-c37a-ed11-81ad-000d3a46f784"

    conn = http.client.HTTPSConnection("{}.api.crm4.dynamics.com".format(url))
        
    headers = {
        'Authorization': 'Bearer ' +accesstoken,
        'OData-MaxVersion': '4.0',  
        'OData-Version': '4.0',  
        'Accept': 'application/json',
        'Content-Type': 'application/json; charset=utf-8',
        'Prefer': 'return=representation',
        'If-Match': '*'
    
    }
    
    
    payload = "{\"kgt_loc\":\"m1,k2,uN1,k9\",\"kgt_main\":\"0000,0000,\"}"
    #payload = "{\"kgt_month\":\"February\"}"
    
    
    #kgt_accountsegmentid is in the url
    reponse = conn.request("PATCH", "/api/data/v9.2/kgt_lookuptable(436047e5-c37a-ed11-81ad-000d3a46f784)", payload, headers)
    res = conn.getresponse()
    data = res.read()
    print(data)
    print(res.status, res.reason)
    

    This works well to update the tables