Fetch more than 5000 records in one go…paging cookie way…but there’s a catch….

Hi Folks,

Hope some day or the other, every CRM Consultant will face this issue where they need to retrieve more than 5000 records in one go.

As every one in the internet suggests, paging cookie is the easiest way to achieve the same.

However here there is a catch…if you will use distinct=”false” no-lock=”true” along with your fetch query, you will never achieve the expected result and the timeout error keeps on popping up.

Pretty surely Microsoft have had documented this one, but I couldn’t find any relevant Microsoft article stating this. So thought of sharing this tip over my blog so that some day it will help some one. Here is the complete solution…

//Call the method to use Paging cookie and fetch multiple records in one go....
EntityCollection fetchDonationTransactionRecords = RetrieveAll(service, fetch);

/// <summary>
        /// RetrieveAll
        /// </summary>
        /// <param name="service"></param>
        /// <param name="fetchXml"></param>
        /// <returns></returns>
        public static EntityCollection RetrieveAll(this IOrganizationService service, string fetchXml)
        {
            EntityCollection finalCollection = new EntityCollection();
            int fetchCount = 5000;
            // Initialize the page number.
            int pageNumber = 1;
            // Specify the current paging cookie. For retrieving the first page, 
            // pagingCookie should be null.
            string pagingCookie = null;

            while (true)
            {
                // Build fetchXml string with the placeholders.
                string xml = CreateXml(fetchXml, pagingCookie, pageNumber, fetchCount);
                EntityCollection recordsCollection = new EntityCollection();
                FetchExpression expression = new FetchExpression(xml);
                recordsCollection = service.RetrieveMultiple(expression);

                // * Build up results here *

                // Check for morerecords, if it returns 1.
                if (recordsCollection.MoreRecords)
                {
                    // Increment the page number to retrieve the next page.
                    pageNumber++;
                    pagingCookie = recordsCollection.PagingCookie;

                    foreach (var record in recordsCollection.Entities)
                    {
                        finalCollection.Entities.Add(record);
                    }
                }
                else
                {
                    // If no more records in the result nodes, exit the loop. 
                    if (finalCollection.Entities.Count >= 5000)
                    {
                        foreach (var record in recordsCollection.Entities)
                        {
                            finalCollection.Entities.Add(record);
                        }
                    }
                    else
                    {
                        finalCollection = recordsCollection;
                    }
                    break;
                }
            }
            return finalCollection;
        }

Hope this helps…that’s it for today…be safe & happy CRM’ing….

Cheers,

PMDY

Get selected options from Multiselect Option set

Mandar Joshi's avatarcrm backlog

{QuickPost}

Recently had a requirement to get all selected option texts from a multi select option set. Getting selected values is straight forward but I wanted to get the Label of selected values. For normal option set we can get it easily by using FormattedValues but that does not work with Multiselect Option set.

For C#:

Then found an handy code by Ravi Kashyap that was posted on community here. I have just restructured the function to make it generic.

Now just call the getSelectedOptionSetText() function and pass Organization Service, entity name and MultiSelect optionset field name.

The function first retrieves all the labels from the Option Set and then filters it with selected option set values.

public string getSelectedOptionSetText(IOrganizationService service, Entity enRecord, string fieldName)
{
string selectedText = string.Empty;
// Get the Formatted Values of MultiSelect OptionSet
List multiSelectTextCollection = GetOptionSetTextCollection(service, enRecord.LogicalName, fieldName);

if (enRecord.Contains(fieldName))
{
OptionSetValueCollection multiSelectValueCollection…

View original post 135 more words