Set an organization address as customer PreferredBillingAddress/PreferredShippingAddress not working

Vote:
 

Hi

I am working with Commerce 12.15, I am trying to set the PreferredBillingAddress/PreferredShippingAddress for a customer. The problem I am facing is that when I try and set an organization address as a PreferredBillingAddress/PreferredShippingAddress, it gives me null. For example: 

The code below:

if (ddlMakeThisPrimary == "billing")
{
contact.PreferredBillingAddress = address;
contact.PreferredBillingAddressId = address.AddressId;
}
else if (ddlMakeThisPrimary == "shipping")
{
contact.PreferredShippingAddress = address;
contact.PreferredShippingAddressId = address.AddressId;
}

The lines in bold return a null even though an address is present. This is happening only when I try to assign an orgnization address as a PreferredBillingAddress/PreferredShippingAddress.

#201878
Edited, Apr 01, 2020 15:14
Vote:
 

Are you sure that "address" is not null?

I had set those addresses working fine. The code I used is this:

public void SetPreferredBillingAddress(string addressId)
{
	var currentContact = CustomerContext.Current.CurrentContact;
	var customerAddress = GetAddress(currentContact, addressId);
	if (customerAddress == null)
	{
		return;
	}
	currentContact.PreferredBillingAddress = customerAddress;
	currentContact.SaveChanges();
}

public void SetPreferredShippingAddress(string addressId)
{
	var currentContact = CustomerContext.Current.CurrentContact;
	var customerAddress = GetAddress(currentContact, addressId);
	if (customerAddress == null)
	{
		return;
	}
	currentContact.PreferredShippingAddress = customerAddress;
	currentContact.SaveChanges();
}

private CustomerAddress GetAddress(CustomerContact contact, string addressId)
{
	return contact.ContactAddresses.FirstOrDefault(x => x.Name == addressId);
}

Could it be that "address" in your case is not stored in "contact.ContactAddresses"?

#202016
Edited, Mar 12, 2019 6:56
Vote:
 

Hi @Maris, thank you for your response. Yes, since that address is an organization address, it is tied to the organization and not the contact. But I dont think this will work when your trying to set an organization address as the Preferred Shipping/Billing address for a contact. Infact if you see the else statement down below I am trying to forcefully save the org address as the contacts preferred billing address. That as well fails, and it gives me a null. I think Epi is only looking at the contact Id in the Cls_Address table while setting the preferred addresses and not looking into Organization Id.

            Organization org = new Organization();
            org.Created = DateTime.UtcNow;
            org.Name = "Test Orgnaization";
            org.OrgCustomerGroup = "1";
            org.OrganizationType = Organization.eOrganizationType.Organization.ToString();
            Organization savedOrg = org.SaveChanges();

            CustomerAddress customerAddress = CustomerAddress.CreateInstance();
            customerAddress.OrganizationId = org.PrimaryKeyId;
            customerAddress.AddressType = CustomerAddressTypeEnum.Shipping;
            customerAddress.OrganizationName = savedOrg.Name;
            customerAddress.Line1 = "101 Random St";
            customerAddress.Line2 = string.Empty;
            customerAddress.CountryName = "UNITED STATES";
            customerAddress.City = "Las Vegas";
            customerAddress.RegionName = "Nevada";
            customerAddress.PostalCode = "88901";
            customerAddress.DaytimePhoneNumber = "9876543210";
            customerAddress.IsDefault = true;
            BusinessManager.Create(customerAddress);

            CustomerContact customer = CustomerContact.CreateInstance();
            customer.AcceptMarketingEmail = true;
            customer.BirthDate = DateTime.Now;
            customer.FirstName = "John";
            customer.LastName = "Doe";
            customer.Email = "jdoe@mailinator.com";
            customer.OwnerId = savedOrg.PrimaryKeyId;
            customer.RegistrationSource = Constants.RegistrationSource.RegularRegistration;
            BusinessManager.Create(customer);

            var x = customer.ContactOrganization.Addresses.FirstOrDefault().AddressId;
            CustomerAddress address = customer.ContactAddresses.Where(xx => xx.OrganizationId == x).FirstOrDefault();
            if (address != null)
            {
                customer.PreferredShippingAddress = address;
                customer.SaveChanges();
            }
            else
            {
                customer.PreferredBillingAddress = savedOrg.Addresses.FirstOrDefault();
                customer.SaveChanges();
            }
#202093
Edited, Mar 13, 2019 19:18
Vote:
 

You are right. Actually what I see is, there is no relation in Organization address and contact address. To add organization's address as contact's preferred address, we will need to add that to contact address first. Correct me if I am wrong.

#219261
Apr 01, 2020 4:00
Siddharth Gupta - Apr 02, 2020 1:57
Yes your right, you will need to add the address directly on the contact and then set that as a preferred address.
Vote:
 

Just one to note that this is duplication

contact.PreferredBillingAddress = address;
contact.PreferredBillingAddressId = address.AddressId;

either of them is enough (they are fairly equivalent) 

#219672
Apr 01, 2020 9:53
Siddharth Gupta - Apr 02, 2020 1:58
Made a note of this Sir :)
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.