我们的CRM实体中有大量的记录。我试图在fetch xml的聚合计数的帮助下获取记录的总数。但它有50000条记录的限制。我认为有一种方法可以在On-premise CRM中更改该设置。但我不想改变它。
以前我们使用分页方法来获取总计数(每次5000次)。但这需要很多时间
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)
{
}
}
但这需要很多时间。因此我将其更改为聚合计数方法 - 像这样更改了Fetchxml -
<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'/>.....
像这样的代码
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 ;
现在,如果记录超过50000,它会给出异常。
那么有没有办法在聚合计数的帮助下一次获取50000条记录并循环通过它来获取总计数?
FetchXML聚合的限制是我们所面临的挑战。我想一劳永逸地解决这个问题,所以我构建了一个名为AggX的在线工具,可以在任意数量的行上运行聚合。它目前免费使用。
您可以在https://aggx.meta.tools查看,请注意它只适用于Dynamics 365 Online。此外,请注意,如果您有大量行需要超过5分钟才能运行,则应监视AggX以避免在几分钟的空闲时间后自动将其注销。
如果您的系统是本地的,或者您想编写自己的代码来进行聚合,则可以设计一种算法将数据拆分为少于50,000行的块。然后,您可以在每个块上运行FetchXML聚合并对结果求和。这就是AggX引擎的工作原理。