public static Entity GetContactEntityForCustomerPayload(Customer customer, ITracingService trace)
        {
            if (trace == null)
            {
                throw new InvalidPluginExecutionException("Tracing service is null;");
            }
            trace.Trace("Contact populate fields - start");
            if (customer == null)
            {
                throw new InvalidPluginExecutionException("Customer payload is null.");
            }
            if (customer.CustomerIdentifier == null)
            {
                throw new InvalidPluginExecutionException("Customer Identifier could not be retrieved from payload.");
            }
            if (string.IsNullOrWhiteSpace(customer.CustomerIdentifier.CustomerId))
            {
                throw new InvalidPluginExecutionException("Customer Identifier could not be retrieved from payload.");
            }

            Entity contact        = new Entity(EntityName.Contact);
            var    sourceSystemID = (!string.IsNullOrWhiteSpace(customer.CustomerIdentifier.CustomerId)) ?
                                    customer.CustomerIdentifier.CustomerId : string.Empty;

            contact[Attributes.Contact.SourceSystemId]          = sourceSystemID;
            contact[Attributes.Contact.DuplicateSourceSystemId] = sourceSystemID;
            var sourceMarket = (!string.IsNullOrWhiteSpace(customer.CustomerIdentifier.SourceMarket))
                               ? new EntityReference(EntityName.Country,
                                                     new Guid(customer.CustomerIdentifier.SourceMarket)) : null;

            contact[Attributes.Contact.SourceMarketId] = sourceMarket;
            PopulateIdentityInformation(contact, customer.CustomerIdentity, trace);
            PopulateAddress(contact, customer.Address, trace);
            PopulateEmail(contact, customer.Email, trace);
            PopulatePhone(contact, customer.Phone, trace);
            PopulatePermission(contact, customer.Permissions, trace);
            if (customer.CustomerGeneral != null)
            {
                contact[Attributes.Contact.StatusCode] = CommonXrm.GetCustomerStatus(customer.CustomerGeneral.CustomerStatus);
            }
            if (customer.Additional != null)
            {
                contact[Attributes.Contact.Segment] = CommonXrm.GetSegment(customer.Additional.Segment);
                DateTime?dateOfDeath = null;
                if (!string.IsNullOrWhiteSpace(customer.Additional.DateOfDeath))
                {
                    dateOfDeath = Convert.ToDateTime(customer.Additional.DateOfDeath);
                }
                contact[Attributes.Contact.DateOfDeath] = dateOfDeath;
            }
            trace.Trace("Contact populate fields - end");
            return(contact);
        }
        public static Entity GetContactEntityForBookingPayload(Customer customer, ITracingService trace)
        {
            if (trace == null)
            {
                throw new InvalidPluginExecutionException("Tracing service is null;");
            }
            trace.Trace("Contact populate fields - start");
            if (customer == null)
            {
                return(null);
            }


            Entity contact = null;

            if (customer.CustomerIdentifier == null || string.IsNullOrWhiteSpace(customer.CustomerIdentifier.CustomerId))
            {
                contact = new Entity(EntityName.Contact);
            }
            else
            {
                contact = new Entity(EntityName.Contact, Attributes.Contact.SourceSystemId
                                     , customer.CustomerIdentifier.CustomerId);
            }

            PopulateIdentityInformation(contact, customer.CustomerIdentity, trace);

            if (customer.Additional != null)
            {
                trace.Trace("Contact populate Additional details - start");
                contact[Attributes.Contact.Segment] = CommonXrm.GetSegment(customer.Additional.Segment);

                if (!string.IsNullOrWhiteSpace(customer.Additional.DateOfDeath))
                {
                    contact[Attributes.Contact.DateOfDeath] = Convert.ToDateTime(customer.Additional.DateOfDeath);
                }
                else
                {
                    contact[Attributes.Contact.DateOfDeath] = null;
                }
                trace.Trace("Contact populate Additional details - end");
            }

            PopulateAddress(contact, customer.Address, trace);
            PopulatePhone(contact, customer.Phone, trace);
            PopulateEmail(contact, customer.Email, trace);
            contact[Attributes.Contact.SourceMarketId] = (customer.CustomerIdentifier != null &&
                                                          !string.IsNullOrWhiteSpace(customer.CustomerIdentifier.SourceMarket))
                                                                ?new EntityReference(EntityName.Country
                                                                                     , new Guid(customer.CustomerIdentifier.SourceMarket))
                                                                : null;

            contact[Attributes.Contact.SourceSystemId] = (customer.CustomerIdentifier != null &&
                                                          !string.IsNullOrWhiteSpace(customer.CustomerIdentifier.CustomerId))
                                                                ?customer.CustomerIdentifier.CustomerId
                                                                : string.Empty;
            if (customer.CustomerIdentifier != null)
            {
                if (!string.IsNullOrWhiteSpace(customer.Owner))
                {
                    contact[Attributes.Booking.Owner] = new EntityReference(EntityName.Team, new Guid(customer.Owner));
                }
            }

            if (customer.CustomerGeneral != null)
            {
                contact[Attributes.Contact.StateCode]  = new OptionSetValue((int)Statecode.Active);
                contact[Attributes.Contact.StatusCode] = CommonXrm.GetCustomerStatus(customer.CustomerGeneral.CustomerStatus);
            }

            trace.Trace("Contact populate fields - end");

            return(contact);
        }