Getting Total Record Count in CRM 2011

Many a times I run into this particular situation and somehow I observe people still retrieve all records and then loop through to get the total number of records. It is very easy to get it using the code given below.

QueryExpression query = new QueryExpression("account");
            query.ColumnSet = new ColumnSet(true);
            query.Criteria.AddCondition(new ConditionExpression("accountid", ConditionOperator.NotNull));
            query.PageInfo = new PagingInfo();
            query.PageInfo.Count = 1;
            query.PageInfo.PageNumber = 1;
            query.PageInfo.ReturnTotalRecordCount = true;

            EntityCollection result = this.ServiceProxy.RetrieveMultiple(query);
            int totalRecords = result.TotalRecordCount;

3 thoughts on “Getting Total Record Count in CRM 2011”

  1. What if there are more than 5000 records to retrieve.
    Then the ReturnTotalRecordCount will be 5000 and we still don't know how many records there are exactly.

Leave a Reply

Your email address will not be published. Required fields are marked *