private List <BrokerDetails> MapBrokers(IEnumerable <Entity> brokers, string feedType)
        {
            List <BrokerDetails> detailsList = new List <BrokerDetails>();

            var uniqueBrokers = brokers.Select(c => c.GetAttributeValue <string>("new_brokercode")).Distinct();

            foreach (var bro in uniqueBrokers)
            {
                if (bro == null)
                {
                    continue;
                }
                var broker  = brokers.Where(c => c.GetAttributeValue <string>("new_brokercode") == bro.ToString()).FirstOrDefault();
                var company = new Company(_svc, broker);

                BrokerDetails details        = new BrokerDetails();
                var           address        = broker.GetAttributeValue <EntityReference>("new_address");
                Address       addressDetails = null;
                if (address != null)
                {
                    addressDetails            = new Address(_svc, _svc.Retrieve(address.LogicalName, address.Id, new ColumnSet(true)));
                    details.Address           = addressDetails.street1;
                    details.Address2          = addressDetails.street2;
                    details.City              = addressDetails.City;
                    details.County            = addressDetails.County;
                    details.Country           = addressDetails.Country;
                    details.PostCode          = addressDetails.PostalCode;
                    details.AddressIdentifier = addressDetails.AddressIdentifier;
                }
                details.CRMCompanyCode  = company.CompanyCode;
                details.Name            = company.Name;
                details.MainContactName = company.MainContactName;
                details.PhoneNumber     = company.MainPhone;
                details.MobileNumber    = company.MobilePhone;
                details.CurrencyCode    = company.CurrencyCode;
                details.Email           = company.Email;
                details.HomePage        = company.HomePage;
                details.FeedType        = feedType;
                details.RecordType      = company.StateCode;

                detailsList.Add(details);
            }

            //Uncomment the below code when we Go Live
            //UpdateNavSentDates(_svc, brokers);

            return(detailsList);
        }
        public static void UpdateBroker(this IOrganizationService svc, BrokerDetails details, Entity retrievedBroker)
        {
            Entity broker = new Entity("account");

            broker.Id               = retrievedBroker.Id;
            broker["name"]          = details.Name;
            broker["telephone1"]    = details.PhoneNumber;
            broker["telephone2"]    = details.MobileNumber;
            broker["emailaddress1"] = details.Email;
            broker["websiteurl"]    = details.HomePage;
            broker["new_alreadytransferredtoaccounting"] = true;

            svc.Update(broker);
            broker["new_transferredtoaccountingon"] = DateTime.Now;
            svc.Update(broker);
        }
        public static void CreateBroker(this IOrganizationService svc, BrokerDetails details, Entity address)
        {
            Entity broker = new Entity("account");

            broker["name"]          = details.Name;
            broker["telephone1"]    = details.PhoneNumber;
            broker["telephone2"]    = details.MobileNumber;
            broker["emailaddress1"] = details.Email;
            broker["websiteurl"]    = details.HomePage;
            broker["new_transferredtoaccountingon"]      = DateTime.Now;
            broker["new_navcompanycode"]                 = details.NavCode;
            broker["new_alreadytransferredtoaccounting"] = true;

            Entity currency = null;

            if (details.CurrencyCode != null)
            {
                currency = svc.RetrieveCurrency(details.CurrencyCode);
            }
            if (currency != null)
            {
                broker["transactioncurrencyid"] = currency.ToEntityReference();
            }

            if (address != null)
            {
                broker["new_address"]    = address.ToEntityReference();
                broker["new_countryid"]  = address.GetAttributeValue <EntityReference>("new_country");
                broker["new_postalcode"] = address.GetAttributeValue <EntityReference>("new_postalcode");
            }

            //if(details.PostCode != null && details.PostCode != "")
            //{
            //    var postCode = svc.RetrieveCRMEntityByName(details.PostCode, "new_postalcode"); //RetrieveCustomEntityByName(details.PostCode, "new_postalcode");
            //    var manualAddress = svc.CreateManualAddress(details);
            //}

            svc.Create(broker);
        }