private void tsbGenerate_Click(object sender, EventArgs e)
        {
            #region # Varibale declaration #
            EntityCollection entityColl = null;
            CreateExcelDoc   excell_app = null;
            int  progressCounter        = 0;
            bool createJob = false;
            #endregion # Varibale declaration #

            if (txtFilePath.Text.Length == 0)
            {
                MessageBox.Show(this, "Please select file location.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (this.lvAttributes.CheckedItems == null || this.lvAttributes.CheckedItems.Count <= 0)
            {
                MessageBox.Show(this, "Please select Attributes.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (this.cmbDuplicateDetectionJobs.SelectedItem == null && ApplicationSetting.ExistingJobIds != null && ApplicationSetting.ExistingJobIds.Count > 0)
            {
                // MessageBox.Show(this, "Please select existing Job to run.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                DialogResult result = MessageBox.Show("Please select existing Job to run. If you want to run Job for all records click Yes, else click No button.", "Confirmation", MessageBoxButtons.YesNoCancel);
                if (result == DialogResult.Yes)
                {
                    createJob = true;
                }
                else if (result == DialogResult.No)
                {
                    return;
                }
            }
            else if (ApplicationSetting.ExistingJobIds.Count <= 0)
            {
                DialogResult result = MessageBox.Show("If you want to run Job for all records click Yes, else click No button.", "Confirmation", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    createJob = true;
                }
                else if (result == DialogResult.No)
                {
                    return;
                }
            }

            GetAttributesList();

            WorkAsync(new WorkAsyncInfo
            {
                Message       = string.Concat("Duplicate ", ApplicationSetting.SelectedEntity.DisplayName, " detection started......"),
                AsyncArgument = null,
                Work          = (bw, evt) =>
                {
                    #region # Bulk Detection #
                    Guid jobId          = Guid.Empty;
                    bool isJobCompleted = false;
                    if (createJob == true)
                    {
                        BulkDetectDuplicatesRequest request = new BulkDetectDuplicatesRequest()
                        {
                            JobName = string.Format("{0} {1} {2}", "Detect duplicate", ApplicationSetting.SelectedEntity.DisplayName, DateTime.Now),
                            Query   = new QueryExpression()
                            {
                                EntityName = ApplicationSetting.SelectedEntity.LogicalName,
                                ColumnSet  = new ColumnSet(true)
                            },

                            RecurrencePattern   = String.Empty,
                            RecurrenceStartTime = DateTime.Now,
                            ToRecipients        = new Guid[0],
                            CCRecipients        = new Guid[0]
                        };

                        BulkDetectDuplicatesResponse _response = (BulkDetectDuplicatesResponse)Service.Execute(request);
                        jobId = _response.JobId;
                        UpdateJobStatus(jobId);
                        isJobCompleted = IsJobCompleted(jobId);
                    }
                    else
                    {
                        this.Invoke(new Action(() => { jobId = new Guid(cmbDuplicateDetectionJobs.SelectedValue.ToString()); }));

                        isJobCompleted = true;
                    }

                    #endregion # Bulk Detection #

                    if (jobId != Guid.Empty && isJobCompleted)
                    {
                        ApplicationSetting.JobId.Add(jobId);

                        entityColl = GetDuplicateRecords(jobId);
                        ApplicationSetting.DuplicateCollection = new EntityCollection();
                        ApplicationSetting.DuplicateRecords    = new List <DuplicateRecords>();

                        if (entityColl != null && entityColl.Entities.Count > 0)
                        {
                            this.Invoke(new Action(() =>
                            {
                                btnMergeDuplicates.Enabled     = true;
                                rbCreatedOn.Enabled            = true;
                                rbModifiedOn.Enabled           = true;
                                rbNoOfActivities.Enabled       = true;
                                rbnNoOfCompletedFields.Enabled = true;
                            }));

                            excell_app = new CreateExcelDoc(txtFilePath.Text.Trim());

                            //GenerateHeader(excell_app);

                            bw.ReportProgress(100, string.Concat("Duplicate ", ApplicationSetting.SelectedEntity.DisplayName, " found (", entityColl.Entities.Count, ")"));

                            int row = 1;

                            bw.ReportProgress(0, "Retrieving duplicate records........");
                            int rowCount = 0;
                            IEnumerable <System.Linq.IGrouping <object, Entity> > entityCollGroupBy = entityColl.Entities.GroupBy(ent => ent.Attributes[ApplicationSetting.SelectedEntity.PrimaryIdAttribute]);
                            List <Guid> chidList = new List <Guid>();
                            foreach (IEnumerable <Entity> elements in entityCollGroupBy)
                            {
                                foreach (Entity entity in elements)
                                {
                                    bw.ReportProgress(progressCounter * 100 / entityColl.Entities.Count(), string.Concat("Generating duplicate report........"));
                                    if (entity != null)
                                    {
                                        if (!DoesItContain(ApplicationSetting.DuplicateRecords, entity.Id))
                                        {
                                            rowCount++;
                                            DuplicateRecords rec = new DuplicateRecords();
                                            rec.MasterId         = entity.Id;
                                            if (elements != null && elements.Count() > 0)
                                            {
                                                var activityGroupBy = elements.Where(cnt => cnt.Attributes.Contains("Activities.activitytypecode") && cnt.Attributes["Activities.activitytypecode"] != null);
                                                entity.Attributes["ActivitiesCount"] = activityGroupBy != null ? activityGroupBy.Count() : 0;
                                            }
                                            else
                                            {
                                                entity.Attributes["ActivitiesCount"] = 0;
                                            }
                                            rec.MasterEntity = entity;

                                            Entity duplicateTemp = new Entity(ApplicationSetting.SelectedEntity.LogicalName);
                                            duplicateTemp.Id     = entity.Id;
                                            var re = new RetrieveDuplicatesRequest
                                            {
                                                BusinessEntity     = duplicateTemp,
                                                MatchingEntityName = duplicateTemp.LogicalName,
                                                PagingInfo         = new PagingInfo()
                                                {
                                                    PageNumber = 1, Count = 500
                                                }
                                            };

                                            var response = (RetrieveDuplicatesResponse)Service.Execute(re);
                                            if (response != null && response.DuplicateCollection != null)
                                            {
                                                List <Entity> childRecords = new List <Entity>();
                                                rec.ChildIds = new List <Guid>();
                                                foreach (Entity duplicate in response.DuplicateCollection.Entities)
                                                {
                                                    if (!DoesItContain(ApplicationSetting.DuplicateRecords, duplicate.Id))
                                                    {
                                                        rowCount++;
                                                        rec.ChildIds.Add(duplicate.Id);
                                                        chidList.Add(duplicate.Id);
                                                        childRecords.Add(duplicate);
                                                    }
                                                }
                                                rec.ChildEntity = childRecords;
                                            }
                                            ApplicationSetting.DuplicateRecords.Add(rec);
                                        }
                                        progressCounter++;

                                        break;// Break every time...Row is repeating for every activity
                                    }
                                }
                            }

                            bw.ReportProgress(0, "Generating Duplicate report........");
                            progressCounter = 0;
                            List <DuplicateRecords> DuplicateRecords = ApplicationSetting.DuplicateRecords;
                            EntityCollection childs = new EntityCollection();
                            if (chidList != null && chidList.Count() > 0)
                            {
                                childs = GetRelatedDuplicateRecords(chidList.ToArray());
                            }

                            int columnCount = ApplicationSetting.AttributesToDisplayName.Count() + 2;
                            var data        = new object[rowCount + 1, columnCount];

                            GenerateHeader(excell_app, ref data);

                            for (int indexofRecord = 0; indexofRecord < DuplicateRecords.Count(); indexofRecord++)
                            {
                                bw.ReportProgress(progressCounter * 100 / DuplicateRecords.Count(), string.Concat("Generating Duplicate report........"));
                                ApplicationSetting.DuplicateRecords[indexofRecord].MasterEntity = GetExcelPopulated(DuplicateRecords[indexofRecord].MasterEntity, 0, excell_app, ref row, null, Convert.ToInt32(DuplicateRecords[indexofRecord].MasterEntity.Attributes["ActivitiesCount"]), true, ref data);

                                for (int indexOfChild = 0; indexOfChild < DuplicateRecords[indexofRecord].ChildEntity.Count(); indexOfChild++)
                                {
                                    if (DuplicateRecords[indexofRecord].ChildEntity.ElementAt(indexOfChild) != null)
                                    {
                                        var activities = childs != null && childs.Entities.Count > 0 ? (childs.Entities.Where(i => ((EntityReference)i.Attributes["regardingobjectid"]).Id == DuplicateRecords[indexofRecord].ChildEntity.ElementAt(indexOfChild).Id).Count()) : 0;
                                        ApplicationSetting.DuplicateRecords[indexofRecord].ChildEntity[indexOfChild] = GetExcelPopulated(DuplicateRecords[indexofRecord].ChildEntity.ElementAt(indexOfChild), 0, excell_app, ref row, null, activities, false, ref data);
                                    }
                                }
                                progressCounter++;
                            }

                            excell_app.WriteArray(rowCount + 1, columnCount, data);
                            bw.ReportProgress(100, "Duplicate detection completed........");

                            excell_app.worksheet.SaveAs(txtFilePath.Text);

                            this.Invoke(new Action(() => { MessageBox.Show(this, "Duplicate Detection Completed."); }));

                            this.Invoke(new Action(() =>
                            {
                                excell_app.app.DisplayAlerts  = true;
                                excell_app.app.Calculation    = Microsoft.Office.Interop.Excel.XlCalculation.xlCalculationAutomatic;
                                excell_app.app.ScreenUpdating = true;
                                excell_app.app.Visible        = true;
                            }));

                            excell_app.ReleaseObject();
                            excell_app = null;

                            this.Invoke(new Action(() => { txtFilePath.Text = ""; }));
                        }
                        else
                        {
                            this.Invoke(new Action(() => { MessageBox.Show(this, "No Duplicates found."); }));
                        }

                        cmbDuplicateDetectionJobs.DataSource = null;
                        // Bind the Jobs
                        GetExistingJobs();
                    }
                },
                PostWorkCallBack = evt =>
                {
                    if (evt.Error != null)
                    {
                        this.Invoke(new Action(() => { MessageBox.Show(this, "An error occured " + evt.Error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }));
                    }
                },
                ProgressChanged = evt => { SetWorkingMessage(string.Format("{0}%\r\n{1}", evt.ProgressPercentage, evt.UserState)); }
            });
        }
        /// <summary>
        /// Perform Merge operation
        /// </summary>
        private void MergeOperation()
        {
            #region # Varibale declaration #
            int progressCounter = 0;
            #endregion # Varibale declaration #

            if (ApplicationSetting.AttributesSchemaList == null || ApplicationSetting.AttributesSchemaList.Count <= 0)
            {
                MessageBox.Show(this, "Please select Attributes.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            WorkAsync(new WorkAsyncInfo
            {
                Message       = string.Concat("Duplicate ", ApplicationSetting.SelectedEntity.DisplayName, " sorting started......"),
                AsyncArgument = null,
                Work          = (bw, evt) =>
                {
                    #region Duplicate Collection #
                    List <DuplicateRecords> duplicateRecords = new List <DuplicateRecords>();
                    foreach (DuplicateRecords record in ApplicationSetting.DuplicateRecords)
                    {
                        List <Entity> duplicatesCollection = new List <Entity>();

                        bw.ReportProgress(progressCounter * 100 / ApplicationSetting.DuplicateRecords.Count(), string.Concat("Generating duplicate report........"));
                        duplicatesCollection.AddRange(record.ChildEntity);

                        duplicatesCollection.Add(record.MasterEntity);

                        #region # Sort Master and Child #
                        if (duplicatesCollection != null && duplicatesCollection.Count() > 0)
                        {
                            IEnumerable <Entity> sortedRecord = null;
                            if (rbCreatedOn.Checked)
                            {
                                DateTime dateSortedFound = duplicatesCollection.Min(c => (DateTime)c.Attributes["createdon"]);
                                sortedRecord             = duplicatesCollection.Where(c => (DateTime)c.Attributes["createdon"] == dateSortedFound);
                            }
                            else if (rbModifiedOn.Checked)
                            {
                                DateTime dateSortedFound = duplicatesCollection.Max(c => (DateTime)c.Attributes["modifiedon"]);
                                sortedRecord             = duplicatesCollection.Where(c => (DateTime)c.Attributes["modifiedon"] == dateSortedFound);
                            }
                            else if (rbNoOfActivities.Checked)
                            {
                                int activitiesCount = duplicatesCollection.Max(c => (int)c.Attributes["ActivitiesCount"]);
                                sortedRecord        = duplicatesCollection.Where(c => (int)c.Attributes["ActivitiesCount"] == activitiesCount);
                            }
                            else if (rbnNoOfCompletedFields.Checked)
                            {
                                int fieldsCount = duplicatesCollection.Max(c => (int)c.Attributes["CountOfCompletedFields"]);
                                sortedRecord    = duplicatesCollection.Where(c => (int)c.Attributes["CountOfCompletedFields"] == fieldsCount);
                            }

                            // If only one record no need to query
                            if (sortedRecord != null && sortedRecord.Count() > 0)
                            {
                                Entity recordFound         = sortedRecord.FirstOrDefault();
                                List <Entity> childRecords = duplicatesCollection.Where(c => c.Id != recordFound.Id).ToList();
                                List <Entity> childs       = new List <Entity>();
                                if (recordFound != null && recordFound.Attributes.Contains("statecode") && ((OptionSetValue)recordFound.Attributes["statecode"]).Value == 0)
                                {
                                    if (childRecords != null && childRecords.Count() > 0)
                                    {
                                        List <Guid> ids = new List <Guid>();
                                        foreach (var childId in childRecords)
                                        {
                                            if (childId.Attributes.Contains("statecode") && ((OptionSetValue)childId.Attributes["statecode"]).Value == 0)
                                            {
                                                ids.Add(childId.Id);
                                                childs.Add(childId);
                                            }
                                        }
                                        duplicateRecords.Add(new DuplicateRecords {
                                            MasterId = recordFound.Id, ChildIds = ids, ChildEntity = childs, MasterEntity = recordFound
                                        });
                                    }
                                }
                            }
                        }
                        #endregion # Sort Master and Child #

                        progressCounter++;
                    }
                    #endregion Duplicate Collection #

                    progressCounter = 0;
                    bw.ReportProgress(1, "Duplicate merge started........");

                    #region # Merge #
                    for (int i = 0; i < duplicateRecords.Count(); i++)
                    {
                        bw.ReportProgress(progressCounter * 100 / ApplicationSetting.DuplicateRecords.Count(), string.Concat("Duplicate merge started........"));
                        DuplicateRecords duplicate = duplicateRecords[i];
                        MergeRecords(duplicate.MasterEntity, duplicate.ChildEntity);
                        progressCounter++;
                    }
                    this.Invoke(new Action(() => { btnMergeDuplicates.Enabled = false; }));

                    bw.ReportProgress(100, "Duplicate merge completed........");

                    this.Invoke(new Action(() => { MessageBox.Show(this, "Duplicate merge completed."); }));
                    #endregion # Merge #
                },
                PostWorkCallBack = evt =>
                {
                    if (evt.Error != null)
                    {
                        this.Invoke(new Action(() => { MessageBox.Show(this, "An error occured " + evt.Error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }));
                    }
                },
                ProgressChanged = evt => { SetWorkingMessage(string.Format("{0}%\r\n{1}", evt.ProgressPercentage, evt.UserState)); }
            });
        }