Search code examples
c#dynamics-crmfetchxml

Is there a way how to get total count of entities of given type in CRM without paging


Suppose I have query like

var countAlias = "entity_count";
var query = FormattableString.Invariant($@" 
  <fetch distinct='false' aggregate='true'> 
    <entity name='{entityName}'> 
      <attribute name='createdon' alias='{countAlias}' aggregate='count'/> 
    </entity> 
  </fetch>");

var response = organizationService.RetrieveMultiple(new FetchExpression(query));
var entity = response.Entities.First();
var count = (int)((AliasedValue)entity[countAlias]).Value;

This works nicely unless there is more than 50K records - then it hits the aggregation limit. Is there a way how to query total count of entities in system without paging ?


Solution

  • Unfortunately, short answer is No.

    Fetchxml is the only option to do aggregation like count, sum, etc - that too has 50k record limit due to performance consideration (upper limit can be bent for on-prem).

    Query expression, LINQ to CRM doesn’t support aggregation. That’s why we got paging cookie as a choice from MS to iterate & count the records.

    Alternatively, (unlikely) if you have reporting replication sql DB by pushing a data sync, Rollup count can be done with some jobs in timely manner.