acumatica Modifications to Contact and Address Info through Code Override Bill-To Contact and Bill-To Address Info for a Customer


Example

When you need to override Bill-To Contact and Bill-To Address info for a Customer, the very first step is to set correct values for the IsBillContSameAsMain and IsBillSameAsMain properties of the Customer DAC. Don't forget to invoke Update() method on the Customer cache right after you updated IsBillContSameAsMain or IsBillSameAsMain property to commit the current Same as Main field value into the cache.

Your next step is to invoke Select() method on the BillContact and BillAddress data views prior to assigning any field values. It is also recommended to assign the result of Select() method to the BillContact and BillAddress data views' Current property to guarantee that your code modifies the current record in Contact and Address PXCache respectively.

public class CustomerMaintExt : PXGraphExtension<CustomerMaint>
{
    public PXAction<Customer> UpdateBillingAddress;
    [PXButton(CommitChanges = true)]
    [PXUIField(DisplayName = "Update Bill-To Info")]
    protected void updateBillingAddress()
    {
        Customer currentCustomer = Base.BAccount.Current;

        if (currentCustomer.IsBillContSameAsMain != true)
        {
            currentCustomer.IsBillContSameAsMain = true;
            Base.BAccount.Update(currentCustomer);
        }
        else
        {
            currentCustomer.IsBillContSameAsMain = false;
            Base.BAccount.Update(currentCustomer);

            Contact billContact = Base.BillContact.Current = Base.BillContact.Select();
            billContact.FullName = "ABC Holdings Inc";
            billContact.Phone1 = "+1 (212) 532-9574";
            Base.BillContact.Update(billContact);
        }

        if (currentCustomer.IsBillSameAsMain != true)
        {
            currentCustomer.IsBillSameAsMain = true;
            Base.CurrentCustomer.Update(currentCustomer);
        }
        else
        {
            currentCustomer.IsBillSameAsMain = false;
            Base.CurrentCustomer.Update(currentCustomer);

            Address billAddress = Base.BillAddress.Current = Base.BillAddress.Select();
            billAddress.AddressLine1 = "65 Broadway";
            billAddress.AddressLine2 = "Office Suite 187";
            billAddress.City = "New York";
            billAddress.CountryID = "US";
            billAddress = Base.BillAddress.Update(billAddress);
            billAddress.State = "NY";
            billAddress.PostalCode = "10004";
            Base.BillAddress.Update(billAddress);
        }

        Base.Actions.PressSave();
    }
}