public override ConversionResult <BAccount> Convert(AccountConversionOptions options = null)
        {
            // do nothing if account already exist
            if (ExistingAccount.SelectSingle() is BAccount account)
            {
                //PXTrace.WriteVerbose($"Using existing account: {account.AcctCD}.");
                return(new ConversionResult <BAccount>
                {
                    Converted = false,
                    Entity = account,
                });
            }

            return(base.Convert(options));
        }
        protected override BAccount CreateMaster(BusinessAccountMaint graph, AccountConversionOptions config)
        {
            var param      = AccountInfo.Current;
            var document   = Documents.Current;
            var docContact = Contacts.Current ?? Contacts.SelectSingle();
            var docAddress = Addresses.Current ?? Addresses.SelectSingle();

            object cd = param.BAccountID;

            graph.BAccount.Cache.RaiseFieldUpdating <BAccount.acctCD>(null, ref cd);

            BAccount account = graph.BAccount.Insert(new BAccount
            {
                AcctCD           = (string)cd,
                AcctName         = param.AccountName,
                Type             = BAccountType.ProspectType,
                ParentBAccountID = document.ParentBAccountID,
                CampaignSourceID = document.CampaignID,
            });

            account.ClassID = param.AccountClass;             // In case of (param.AccountClass == null) constructor fills ClassID with default value, so we have to set this directly.

            CRCustomerClass ocls = PXSelect <
                CRCustomerClass,
                Where <
                    CRCustomerClass.cRCustomerClassID, Equal <Required <CRCustomerClass.cRCustomerClassID> > > >
                                   .SelectSingleBound(graph, null, account.ClassID);

            if (ocls?.DefaultOwner == CRDefaultOwnerAttribute.Source)
            {
                account.WorkgroupID = document.WorkgroupID;
                account.OwnerID     = document.OwnerID;
            }

            account = graph.BAccount.Update(account);

            if (param.LinkContactToAccount == true)
            {
                // in case of opportunity
                Contact contact = PXSelect <Contact, Where <Contact.contactID, Equal <Required <CROpportunity.contactID> > > > .Select(graph, document.RefContactID);

                if (contact != null)
                {
                    graph.Answers.CopyAttributes(account, contact);
                    contact.BAccountID = account.BAccountID;
                    graph.Contacts.Update(contact);
                }
            }


            var defContact = graph.DefContact.SelectSingle()
                             ?? throw new InvalidOperationException("Cannot get Contact for Business Account.");    // just to ensure

            MapContact(docContact, account, ref defContact);
            MapConsentable(docContact, defContact);
            defContact = graph.DefContact.Update(defContact);

            var defAddress = graph.AddressCurrent.SelectSingle()
                             ?? throw new InvalidOperationException("Cannot get Address for Business Account.");    // just to ensure

            MapAddress(docAddress, account, ref defAddress);
            defAddress = graph.AddressCurrent.Update(defAddress);

            CR.Location location = graph.DefLocation.Select();
            location.DefAddressID = defAddress.AddressID;
            location.CTaxZoneID   = document.TaxZoneID;           // Saving tax zone before ReverseDocumentUpdate() removes it
            graph.DefLocation.Update(location);

            ReverseDocumentUpdate(graph, account);

            FillRelations(graph.Relations, account);

            FillAttributes(graph.Answers, account);

            TransferActivities(graph, account);

            // Copy Note text and Files references
            CRSetup setup = PXSetupOptional <CRSetup> .Select(graph);

            PXNoteAttribute.CopyNoteAndFiles(graph.Caches <TMain>(), GetMain(document), graph.CurrentBAccount.Cache, account, setup);

            return(account);
        }