Fetch the total number of elements in dynamics web api using oData query

Hi,
How do i just fetch the number of accounts created after a given date using the dynamics 365 web apis...
The normal way to do this would be https://directi123.crm8.dynamics.com/api/data/v9.0/accounts/?$filter=createdon gt 2017-04-01&$count=true
But the problem with this is it returns all the data. I only need the count.. And dont need any of the data...I can although restrict it to just one element by specifying $select...
Is there any way to use the filter element also and to just get the total count...

Thanks
Rohit BajajAsked:
Who is Participating?
 
Chinmay PatelConnect With a Mentor Enterprise ArchitectCommented:
Hi Rohit,

Personally, I am not a big fan of WebAPI end point just yet (I use them in most of my engagements nowadays that's a different story and maybe I am biased towards FetchXML as it was my go to solution for more than a decade) so I have a combined solution that uses both FetchXML and WebAPI.

Take this fetch xml definition and modify it as per your requirements
<fetch distinct='false' mapping='logical' aggregate='true'> 
    <entity name='account'> 
       <attribute name='name' alias='count' aggregate='count'/> 
<filter type="and">
      <condition attribute="createdon" operator="on-or-after" value="YOURDATEHERE" />
    </filter>
   </entity> 
</fetch>"

Open in new window


UrlEncode the above XML

And then execute it using
https://directi123.crm8.dynamics.com/api/data/v9.0/accounts/?fetchXml=YOURURLENCODEDFETCHHERE

Open in new window


Let me know how it goes.

Regards,
Chinmay.
0
 
Chinmay PatelEnterprise ArchitectCommented:
Author has not come back with the comment. The solution given works and gets desired results.
0
All Courses

From novice to tech pro — start learning today.