我在SOOrder摘要上创建了一个自定义联系人字段,以便用户可以根据所选的客户选择客户联系人。
这是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 { }
现在,每当用户选择任何联系人时,我都会使用自定义联系人字段中所选联系人的电子邮件覆盖电子邮件,在SOOrder屏幕的"装运设置"选项卡上。我已经为它编写了下面的代码,但似乎它不起作用。
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();
}
}
这其中有两个问题。
-
它确实显示了我选择联系人时填充的电子邮件,但在保存时,它会覆盖客户的默认电子邮件。
-
如果我正在创建新的销售订单并选择联系人。保存时,如果我错过任何必填字段或任何其他错误,例如 Avalara 错误,那么它会再次覆盖客户的默认设置。
请指教。
要将客户联系电子邮件复制到送货地址电子邮件字段中,应按如下方式实现SOOrder_UsrCustContactID_FieldUpdated处理程序:
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;
}
}
}
请注意,在我们将 OverrideContact 设置为 true 后,在Shipping_Contact数据视图上调用了 Update 方法,然后从Shipping_Contact视图中进行其他选择。绝对必须再次调用 Select 方法,因为当 OverrideContact 设置为 true 时,系统会创建 SOShippingContact DAC 的新实例并将其插入缓存中。从缓存中获取新的 SOShippingContact 实例的最简单方法是通过Shipping_Contact数据视图上的 Select 方法。