private void tsbCreateRecords_Click(object sender, EventArgs e)
        {
            if (lvOptionSets.CheckedItems.Count == 0)
                return;

            var mm = new MetadataManager(Service);
            var entityExists = mm.EntityExists("gap_powerbioptionsetref");
            if (!entityExists)
            {
                var message = "'Power BI Option-Set Xref' entity does not exist on your organization. Would you like to create it? If you answer 'no', the process will be aborted";
                if (MessageBox.Show(this, message, "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
                {
                    return;
                }
            }

            var settings = new Settings
            {
                CreateEntity = !entityExists,
                OptionSets = lvOptionSets.CheckedItems.Cast<ListViewItem>().Select(o => (AttributeMetadata)o.Tag).ToList(),
                AllMetadata = emc
            };

            tsbCreateRecords.Enabled = false;
            tsbDeleteEntity.Enabled = false;
            tsbLoadEntities.Enabled = false;

            WorkAsync(new WorkAsyncInfo
            {
                Message = "Initializing...",
                AsyncArgument = settings,
                Work = (bw, evt) =>
                {
                    if (((Settings)evt.Argument).CreateEntity)
                    {
                        var organization = Service.RetrieveMultiple(new QueryExpression("organization") { ColumnSet = new ColumnSet("languagecode") }).Entities.First();

                        bw.ReportProgress(0, "Creating entity...");
                        mm.CreatePowerBiOptionSetRefEntity(organization.GetAttributeValue<int>("languagecode"), bw);
                    }

                    bw.ReportProgress(0, "Processing Option Sets ...");

                    CreateRecords(settings);
                },
                ProgressChanged = evt =>
                {
                    SetWorkingMessage(evt.UserState.ToString());
                },
                PostWorkCallBack = evt =>
                {
                    tsbCreateRecords.Enabled = true;
                    tsbDeleteEntity.Enabled = true;
                    tsbLoadEntities.Enabled = true;
                }
            });
        }
        private void CreateRecords(Settings settings)
        {
            foreach (var optionSet in settings.OptionSets)
            {
                OptionMetadataCollection omc = null;

                if (optionSet is PicklistAttributeMetadata)
                {
                    omc = ((PicklistAttributeMetadata)optionSet).OptionSet.Options;
                }
                else if (optionSet is StateAttributeMetadata)
                {
                    omc = ((StateAttributeMetadata)optionSet).OptionSet.Options;
                }
                else
                {
                    omc = ((StatusAttributeMetadata)optionSet).OptionSet.Options;
                }

                foreach (OptionMetadata option in omc)
                {
                    foreach (LocalizedLabel label in option.Label.LocalizedLabels)
                    {
                        bool exists = true;
                        var record = GetRecord(settings.AllMetadata.First(e => e.LogicalName == optionSet.EntityLogicalName).SchemaName, optionSet.SchemaName, option.Value.Value, label.LanguageCode);
                        if (record == null)
                        {
                            record = new Entity("gap_powerbioptionsetref");
                            exists = false;
                        }
                        else
                        {
                            // The label did not change, no need to update
                            if (record.GetAttributeValue<string>("gap_label") == label.Label)
                            {
                                continue;
                            }
                        }

                        record["gap_entityname"] = settings.AllMetadata.First(e => e.LogicalName == optionSet.EntityLogicalName).DisplayName.UserLocalizedLabel.Label;
                        record["gap_entityschemaname"] = settings.AllMetadata.First(e => e.LogicalName == optionSet.EntityLogicalName).SchemaName;
                        record["gap_optionsetschemaname"] = optionSet.SchemaName;
                        record["gap_value"] = option.Value;
                        record["gap_language"] = label.LanguageCode;
                        record["gap_label"] = label.Label;

                        if (exists)
                        {
                            Service.Update(record);
                        }
                        else
                        {
                            Service.Create(record);
                        }
                    }
                }
            }
        }