1
votes

According to this article there is a serious bug in how the paging cookie is managed in Microsoft CRM. In short, the bug appears when trying to retrieve more than 5000 records in a one-to-many relationship. The author states that that since the parent entity does not contain a unique guid, the paging cookie will also not be unqiue, which might lead to that there are plenty of records that will go missing when retriving the next page of records.

Has anyone experienced this bug? How did you get around it?

One way is of course to turn the query around to use the child entity as the primary entity. In this way will every record will be unique. But this is not an optimal solution either since I have to manually change it back in code.

Another way that I have noticed is that when i´m specifying the order on the child entity like this: linkedEntity.Orders.Add(OrderType.Ascending), then the paging cookie will always be empty. This removes the problem that records go missing, but as I understand, this leads to a lot of overhead in SQL.

The best way would of course be if there is any way to make the paging cookie contain both the parent and child record. But I have not found any way yet to do that. Any idea?

1

1 Answers

3
votes

I have experienced this.

I worked around it by simply not using the paging cookie, you can still page correctly without it.

For example:

int fetchCount = 3;
int pageNumber = 1;
int recordCount = 0;

QueryExpression pagequery = new QueryExpression("account");

while (true)
{
    EntityCollection results = _serviceProxy.RetrieveMultiple(pagequery);

    if (results.Entities != null)
    {
        foreach (Account acct in results.Entities)
        {
            Console.WriteLine(acct.Id);
        }
    }

    if (results.MoreRecords)
    {
        pagequery.PageInfo.PageNumber++;

        //pagequery.PageInfo.PagingCookie = results.PagingCookie; <-- Don't add the paging cookie here
    }
    else
    {
        break;
    }
}