private void RetrieveAuditHistory(RetrieveAuditHistoryMode retrieveMode)
        {
            if (Service == null)
            {
                DialogResult dialogResult = MessageBox.Show(MessageMustBeConnectedToOrganization,
                                                            TitleNoFetchXML,
                                                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (string.IsNullOrEmpty(txtFetchXML.Text))
            {
                DialogResult dialogResult = MessageBox.Show(MessageFetchXMLRequired,
                                                            TitleNoFetchXML,
                                                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            currentEntitySelected = (cmbEntities.SelectedItem as ComboBoxEntities).LogicalName;
            ComboBoxEntityField cmbPrimaryKeySelectedItem = cmbPrimaryKey.SelectedItem as ComboBoxEntityField;
            ComboBoxEntityField cmbField = cmbFields.SelectedItem as ComboBoxEntityField;


            WorkAsync(new WorkAsyncInfo
            {
                Message = MessageValidatingFetchXML,
                Work    = (w, ev) =>
                {
                    try
                    {
                        string newFetch = FetchXMLHelper.AddAttributeFilter(txtFetchXML.Text, cmbPrimaryKeySelectedItem.Value);

                        fetchExpressionRecordsToExtract = new FetchExpression(newFetch);
                        recordsExtracted = Service.RetrieveMultiple(fetchExpressionRecordsToExtract);

                        if (recordsExtracted != null && !recordsExtracted.EntityName.Equals(currentEntitySelected))
                        {
                            ev.Result = string.Format(MessageMismatchEntitySelectedAndFetchXML, recordsExtracted.EntityName, currentEntitySelected);
                            return;
                        }
                        if (recordsExtracted.Entities.Count == 0)
                        {
                            ev.Result = MessageNoAuditHistoryForSelectedRecords;
                            return;
                        }
                        if (recordsExtracted.Entities.Count == 5000)
                        {
                            ev.Result = MessageToMuchRecords;

                            return;
                        }


                        ev.Result = recordsExtracted;
                    }
                    catch (Exception ex)
                    {
                        ev.Result = ErrorMessageFetchXML + ex.Message;
                    }
                },
                ProgressChanged = ev =>
                {
                    // If progress has to be notified to user, use the following method:
                    //SetWorkingMessage("Message to display");
                },
                PostWorkCallBack = ev =>
                {
                    if (ev.Result != null && ev.Result.GetType().Equals(typeof(String)))
                    {
                        DialogResult dialogResult = MessageBox.Show(ev.Result.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        var wsmDialog = new FormExtractData(Service, ev.Result as EntityCollection);
                        wsmDialog.Show();
                        wsmDialog.RetriveAuditHistoryForRecords(cmbPrimaryKeySelectedItem.Value, rdAllFields.Checked, cmbField.Value);

                        if (retrieveMode == RetrieveAuditHistoryMode.Preview)
                        {
                            wsmDialog.OnExtractCompleted += WsmDialog_OnExtractCompleted;
                        }
                        else
                        {
                            wsmDialog.OnExtractCompleted += WsmDialog_OnExtractCompletedSave;
                        }
                    }
                },
                AsyncArgument = null,
                IsCancelable  = true,
                MessageWidth  = 340,
                MessageHeight = 150
            });
        }
        private void RetrieveAuditHistory(RetrieveAuditHistoryMode retrieveMode)
        {
            if (Service == null)
            {
                DialogResult dialogResult = MessageBox.Show(MessageMustBeConnectedToOrganization,
                                                            TitleNoFetchXML,
                                                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (string.IsNullOrEmpty(txtFetchXML.Text))
            {
                DialogResult dialogResult = MessageBox.Show(MessageFetchXMLRequired,
                                                            TitleNoFetchXML,
                                                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            currentEntitySelected = (cmbEntities.SelectedItem as ComboBoxEntities).LogicalName;
            ComboBoxEntityField cmbPrimaryKeySelectedItem = cmbPrimaryKey.SelectedItem as ComboBoxEntityField;
            ComboBoxEntityField cmbField = cmbFields.SelectedItem as ComboBoxEntityField;


            WorkAsync(new WorkAsyncInfo
            {
                Message = MessageValidatingFetchXML,
                Work    = (w, ev) =>
                {
                    try
                    {
                        recordsExtracted = new List <Entity>();
                        bool moreRecords = false;
                        int page         = 1;
                        string cookie    = string.Empty;
                        string newFetch  = FetchXMLHelper.AddAttributeFilter(txtFetchXML.Text, cmbPrimaryKeySelectedItem.Value);

                        do
                        {
                            var xml = FetchXMLHelper.AddCookie(newFetch, cookie, page);
                            EntityCollection collection = Service.RetrieveMultiple(new FetchExpression(xml));

                            if (collection != null && !collection.EntityName.Equals(currentEntitySelected))
                            {
                                ev.Result = string.Format(MessageMismatchEntitySelectedAndFetchXML, collection.EntityName, currentEntitySelected);
                                return;
                            }
                            if (page == 1 && collection.Entities.Count == 0)
                            {
                                ev.Result = MessageNoAuditHistoryForSelectedRecords;
                                return;
                            }
                            if (collection.Entities.Count > 0)
                            {
                                recordsExtracted.AddRange(collection.Entities);
                                moreRecords = collection.MoreRecords;

                                if (moreRecords)
                                {
                                    page++;
                                    cookie = System.Security.SecurityElement.Escape(collection.PagingCookie);
                                }
                            }
                        } while (moreRecords);

                        ev.Result = recordsExtracted;
                    }
                    catch (Exception ex)
                    {
                        ev.Result = ErrorMessageFetchXML + ex.Message;
                    }
                },
                ProgressChanged = ev =>
                {
                    // If progress has to be notified to user, use the following method:
                    //SetWorkingMessage("Message to display");
                },
                PostWorkCallBack = ev =>
                {
                    if (ev.Result != null && ev.Result.GetType().Equals(typeof(String)))
                    {
                        DialogResult dialogResult = MessageBox.Show(ev.Result.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    else
                    {
                        var wsmDialog = new FormExtractData(Service, ev.Result as List <Entity>);
                        wsmDialog.Show();
                        wsmDialog.RetrieveAuditHistoryForRecords(cmbPrimaryKeySelectedItem.Value, rdAllFields.Checked, cmbField.Value);

                        if (retrieveMode == RetrieveAuditHistoryMode.Preview)
                        {
                            wsmDialog.OnExtractCompleted += WsmDialog_OnExtractCompleted;
                        }
                        else
                        {
                            wsmDialog.OnExtractCompleted += WsmDialog_OnExtractCompletedSave;
                        }
                    }
                },
                AsyncArgument = null,
                IsCancelable  = true,
                MessageWidth  = 340,
                MessageHeight = 150
            });
        }