We have huge number on records in our CRM entity. I am trying to fetch the total number of record with the help of aggregate count in fetch xml. But it has limitation of 50000 records. I think there is a way to change that setting in On-premise CRM. But i dont want to change that.
Previously we were using pagination method to fetch total count (5000 each time). But it takes a lot of time
public static int GetTotalRowCount(string fetchXml)
{
try
{
using (OrganizationServiceContext svcContext = new OrganizationServiceContext(ServerConnection.CrmService))
{
int totalCount = 0;
int fetchCount = 5000;
int pageNumber = 1;
string pagingCookie = null;
string xml = string.Empty;
RetrieveMultipleRequest fetchRequest1 = null;
EntityCollection entityCollection = null;
xml = CreateXml(fetchXml, pagingCookie, pageNumber, fetchCount);
fetchRequest1 = new RetrieveMultipleRequest
{
Query = new FetchExpression(xml)
};
entityCollection = ((RetrieveMultipleResponse)svcContext.Execute(fetchRequest1)).EntityCollection;
while (entityCollection.MoreRecords)
{
//moving to next page
pageNumber++;
xml = CreateXml(fetchXml, pagingCookie, pageNumber, fetchCount);
fetchRequest1 = new RetrieveMultipleRequest
{
Query = new FetchExpression(xml)
};
entityCollection = ((RetrieveMultipleResponse)svcContext.Execute(fetchRequest1)).EntityCollection;
totalCount = totalCount + entityCollection.Entities.Count;
}
return totalCount;
}
}
catch (Exception ex)
{
}
}
but it takes a lot of time. Hence i changed it to aggregate count method - Changed Fetchxml like this -
<fetch mapping='logical' output-format='xml-platform' no-lock='true' distinct='false' aggregate='true'>
<entity name='abc_data'>
<attribute name='abc_id' aggregate='count' alias='count'/>.....
code like this
int Count = 0;
FetchExpression fetch = new FetchExpression(fetchXml);
EntityCollection result = ServerConnection.CrmService.RetrieveMultiple(fetch);
if (result.Entities.Count > 0)
{
Entity entity = result.Entities[0];
AliasedValue value = (AliasedValue)entity["count"];
Count = (int)value.Value;
}
return Count ;
Now here it gives an exception if records are more than 50000.
So is there way to fetch 50000 record at a time with the help of aggregate count and loop through it to fetch total count?