API $expand and &count

2019-07-28 08:51发布

问题:

Is it possible to use $expand but instead of returning a collection of objects, just return the count of objects?

For example, get an account and a count of its annotations in a single WebApi call

I've tried a few things.

  • Obvious attempt: accounts(6CDEEB72-2AC8-E711-A825-000D3AE0A7F8)?$select=name&$expand=Account_Annotation($count=true) returns all fields of all Annotations but doesn't count anything.

  • Next I tried accounts(6CDEEB72-2AC8-E711-A825-000D3AE0A7F8)?$select=name&$expand=Account_Annotation($select=annotationid&$count=true) returns an error: "Found an unbalanced bracket expression". I think this is related to the & symbol in the $expand

  • I found a non-crm blog that said this could be resolved with a ; but when I tried accounts(6CDEEB72-2AC8-E711-A825-000D3AE0A7F8)?$select=name&$expand=Account_Annotation($select=annotationid;$count=true) it doesn't give an error, but the $count instruction seems to be ignored

  • A crazy attempt of accounts(6CDEEB72-2AC8-E711-A825-000D3AE0A7F8)?$select=name&$count=Account_Annotation($select=annotationid) returns a "not a valid count" error

I'm guessing that this is not a valid combination, but I thought I would post here in case someone else has achieved this successfully.

回答1:

I got this working, but I'm not sure it's what you are looking for exactly, it only works if I put the count before expand:

api/data/v9.0/cmnt_salesexample?
$select=endcustomeraccountid&$count=true&$expand=endcustomerid($select=accountid)

output:

{
"@odata.context": "https://xyz",
"@odata.count": 5000,
"value": [
 {
  "@odata.etag": "W/\"3560581422\"",
  "endcustomerid": "54321"
},
{
  "@odata.etag": "W/\"3510396844\"",
  "endcustomerid": "12345"  
},
...