2

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 ?

Ondrej Svejdar
  • 21,349
  • 5
  • 54
  • 89
  • are you sure count can't handle more than 50k records? Cuz 50 is like nothing to a DB – Steve May 11 '18 at 15:40
  • @Steve - yup it is nothing DB-wise, but this is the limitation of FetchXml - I actually hit this error; also check out https://community.dynamics.com/crm/f/117/t/155623 – Ondrej Svejdar May 11 '18 at 15:43
  • @Steve There's a limit for Dynamics CRM. You can crank up this limit for an on-premise system but if it's a cloud instance you have no access to this setting. – Filburt May 11 '18 at 15:45

1 Answers1

3

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.