How do I select Dynamics CRM reference attributes?

2019-08-04 12:15发布

问题:

Please read the follows and tell me what I did wrong.

The request I sent:

https://gravityrush.crm5.dynamics.com/api/data/v9.1/accounts?$expand=ownerid/$ref

The response I expected:

"ownerid":{
   "@odata.type":"#Microsoft.Dynamics.CRM.systemuser",
   "@odata.id":"https://gravityrush.crm5.dynamics.com/api/data/v9.0/systemusers(41041b99-e1ae-46c6-a10c-95ce77fc0b18)"
}

The response I received:

"ownerid":{
   "@odata.type":"#Microsoft.Dynamics.CRM.systemuser",
   "@odata.id":"https://gravityrush.crm5.dynamics.com/api/data/v9.1/owners()"
}

Thank you.

回答1:

I think it's possibly a limitation/bug related to this field type.

As you know, the ownerid field can point to a systemuser or team record, and if you take a look at the field definition you'll see that the relationship is actually to an entity called Owner, and that's why you're getting the owners() url:

"@odata.id":"/api/data/v9.1/owners()"

I tried to workaround this using the owninguser and owningteam fields, but it looks like there's an issue there as well because when I added the $ref I started to get nulls values for that field:

GET /api/data/v9.1/accounts?$select=accountid&$expand=owninguser($select=systemuserid)

"owninguser":{
   "systemuserid":"f30a6a42-3067-4c78-b348-9cbc56531f5e",
   "ownerid":"f30a6a42-3067-4c78-b348-9cbc56531f5e"
}


GET /api/data/v9.1/accounts?$select=accountid&$expand=owninguser/$ref
"owninguser":null

Just to be sure, I tried to do the same for a Customer field (kind of same principle where this field can point to a contact or account record) but I din't found this issue, you can get the reference to the Account/Contact using the following query:

GET /api/data/v9.1/incidents?$select=incidentid&$expand=customerid_account/$ref, customerid_contact/$ref


回答2:

  • Try to send API query as below and you shall have the OwnerId

https://orgurl/api/data/v8.2/accounts()?$select=_ownerid_value

You will get OwnerId of each account Record.