简体   繁体   中英

Overriding Shipping Email on SOOrder screen on Shipment Settings tab

I have create a custom contact field on SOOrder summary, so that user can select the Customer Contact based on the Customer selected.

Here is the custom Contact field definition on SOOrder Extension-

 [PXDBInt] [PXUIField(DisplayName = "Contact", Visibility = PXUIVisibility.Visible)] [PXDefault(PersistingCheck = PXPersistingCheck.Nothing)] [PXSelector(typeof(Search2<Contact.contactID, LeftJoin<BAccount2, On<BAccount2.bAccountID, Equal<Contact.bAccountID>>>, Where<Contact.contactType, Equal<ContactTypesAttribute.person>, Or<Contact.contactType, Equal<ContactTypesAttribute.lead>>>>), DescriptionField = typeof(Contact.displayName), Filterable = true)] [PXRestrictor(typeof(Where<Contact.contactType, NotEqual<ContactTypesAttribute.bAccountProperty>, And<Where<BAccount2.bAccountID, Equal<Current<SOOrder.customerID>>, Or<Current<SOOrder.customerID>, IsNull>>>>), PX.Objects.CR.Messages.ContactBAccountDiff)] [PXRestrictor(typeof(Where2<Where2< Where<Contact.contactType, Equal<ContactTypesAttribute.person>, Or<Contact.contactType, Equal<ContactTypesAttribute.lead>>>, And< Where<BAccount2.type, IsNull, Or<BAccount2.type, Equal<BAccountType.customerType>, Or<BAccount2.type, Equal<BAccountType.prospectType>, Or<BAccount2.type, Equal<BAccountType.combinedType>>>>>>>, And<WhereEqualNotNull<BAccount2.bAccountID, SOOrder.customerID>>>), "Contact '{0}' ({1}) has sales order for another business account.", typeof(Contact.displayName), typeof(Contact.contactID))] [PXRestrictor(typeof(Where<Contact.isActive, Equal<True>>), PX.Objects.CR.Messages.ContactInactive, typeof(Contact.displayName))] [PXDBChildIdentity(typeof(Contact.contactID))] public virtual int? UsrCustContactID { get; set; } public abstract class usrCustContactID : IBqlField { } 

Now, I have override the Email with the email of the selected Contact from custom Contact field, on Shipment Settings tab of SOOrder screen whenever the user selects any Contact. I have written below code for it, but seems it is not working.

 protected virtual void SOOrder_UsrCustContactID_FieldUpdated(PXCache sender, PXFieldUpdatedEventArgs e) { SOOrderEntry graph = PXGraph.CreateInstance<SOOrderEntry>(); SOOrderExtension ext = PXCache<SOOrder>.GetExtension<SOOrderExtension>(Base.Document.Current); Contact custContact = PXSelect<Contact, Where<Contact.contactID, Equal<Required<Contact.contactID>>>>.Select(graph, ext.UsrCustContactID); if (custContact != null && !string.IsNullOrEmpty(custContact.EMail)) { graph.Document.Current = Base.Document.Current; Base.Shipping_Contact.Current.OverrideContact = true; Base.Shipping_Contact.Current.Email = custContact.EMail; graph.Shipping_Contact.Current = Base.Shipping_Contact.Current; graph.Shipping_Contact.Current.OverrideContact = true; graph.Shipping_Contact.Current.Email = custContact.EMail; Base.Shipping_Contact.Update(graph.Shipping_Contact.Current); graph.Actions.PressSave(); } } 

There are two issues in this.

  1. It does shows the email populated when I select the Contact but when I save, it overrides with the default one from Customer.

  2. If I am creating a new Sales Order and select the Contact. While saving if I miss any mandatory fields or any other error come such as Avalara error, then it again overrides the default one from Customer.

Please suggest.

To copy customer contact email into the shipping address email field, your SOOrder_UsrCustContactID_FieldUpdated handler should be implemented as follows:

public class SOOrderEntryExt : PXGraphExtension<SOOrderEntry>
{
    protected void SOOrder_UsrCustContactID_FieldUpdated(PXCache sender, PXFieldUpdatedEventArgs e)
    {
        var contact = PXSelectorAttribute.Select<SOOrderExtension.usrCustContactID>(
            sender, e.Row) as Contact;
        if (contact != null)
        {
            var shippingContact = (SOShippingContact)Base.Shipping_Contact.Select();
            shippingContact.OverrideContact = true;
            shippingContact = Base.Shipping_Contact.Update(shippingContact);
            shippingContact = (SOShippingContact)Base.Shipping_Contact.Select();
            shippingContact.Email = contact.EMail;
        }
    }
}

Notice, the Update method called on the Shipping_Contact data view after we set OverrideContact to true , followed by an additional select from the Shipping_Contact view. It is an absolute must to call the Select method second time because when OverrideContact is set to true, the system creates a new instance of the SOShippingContact DAC and inserts it into the cache. The easiest way to obtain the new SOShippingContact instance from the cache is via the Select method on the Shipping_Contact data view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM