Tuesday, February 25, 2014

MS CRM 4.0/2011 : How to get more than 5000 record from CRM database.

Sometimes you required to retrieve all the entity record from database using FETCH and QUERY EXPRESSION but CRM limitation to not retrieve more than 5000 records. The way to retrieve use the below line of code:

public static List<DynamicEntity> GetAllEntityRecords(CrmService service, string entityName, List<ConditionExpression> conditions, ColumnSet columns)
        {
            List<DynamicEntity> records = null;
            try
            {
                QueryExpression query = new QueryExpression();
                query.EntityName = entityName;

                if (columns != null)
                    query.ColumnSet = columns;
                else
                    query.ColumnSet = new AllColumns();

                if (conditions != null && conditions.Any())
                {
                    query.Criteria = new FilterExpression();
                    query.Criteria.FilterOperator = LogicalOperator.And;

                    foreach (var cond in conditions)
                    {
                        query.Criteria.AddCondition(cond);
                    }
                }
                query.PageInfo = new PagingInfo();
                query.PageInfo.Count = 100;
                query.PageInfo.PageNumber = 1;
                query.PageInfo.PagingCookie = null;

                RetrieveMultipleRequest retrieve = new RetrieveMultipleRequest();
                retrieve.Query = query;
                retrieve.ReturnDynamicEntities = true;

                records = new List<DynamicEntity>();

                while (true)
                {

                    BusinessEntityCollection results = ((RetrieveMultipleResponse)service.Execute(retrieve)).BusinessEntityCollection;
                    if (results.BusinessEntities != null)
                    {
                        foreach (DynamicEntity de in results.BusinessEntities)
                            records.Add(de);
                    }

                    // Check for morerecords, if it returns true.
                    if (results.MoreRecords)
                    {
                        // Increment the page number to retrieve the next page.
                        query.PageInfo.PageNumber++;
                        // Set the paging cookie to the paging cookie returned from current results.
                        query.PageInfo.PagingCookie = results.PagingCookie;
                    }
                    else
                    {
                        // If no more records are in the result nodes, exit the loop.
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return records;
        }

No comments:

Split the String values with a special character in MS Flow to convert this into Array

 Many times we have a requirement to prepare the Mailing address for some of the documents, suppose there are Address Line1, Address Line2, ...