Solved

FieldUpdated event not updating the field properly

  • 15 April 2022
  • 5 replies
  • 535 views

Userlevel 2
Badge

Hi there,

 

I have a DAC called VendorAccountMapping and tried to the field of AP Account in Bills and Adjustments screen via this code:

    public class APInvoiceEntry_Extension : PXGraphExtension<APInvoiceEntry>
{
//protected void APInvoice_VendorID_FieldUpdated(PXCache cache, PXFieldUpdatedEventArgs e)
protected void _(Events.FieldUpdated<APInvoice, APInvoice.vendorID> e)
{
APInvoice row = (APInvoice)e.Row;

VendorAccountMapping vendorAccountMapping = SelectFrom<VendorAccountMapping>.Where<VendorAccountMapping.vendorID.IsEqual<@P.AsInt>.And<VendorAccountMapping.branchID.IsEqual<@P.AsInt>>>.View.Select(this.Base, row.VendorID, row.BranchID);
if (vendorAccountMapping != null)
{
row.APAccountID = vendorAccountMapping.APAcctID;
}
}
}

 

Weirdly enough, the above piece of code does not work but I have tried the similar piece of code as shown below for Checks and Payments screen and it works without any issues:

    public class APPaymentEntry_Extension : PXGraphExtension<APPaymentEntry>
{
//protected void APPayment_VendorID_FieldUpdated(PXCache cache, PXFieldUpdatedEventArgs e)
protected void _(Events.FieldUpdated<APPayment, APPayment.vendorID> e)
{
APPayment row = (APPayment)e.Row;
VendorAccountMapping vendorAccountMapping = SelectFrom<VendorAccountMapping>.Where<VendorAccountMapping.vendorID.IsEqual<@P.AsInt>.And<VendorAccountMapping.branchID.IsEqual<@P.AsInt>>>.View.Select(this.Base, row.VendorID, row.BranchID);
if (vendorAccountMapping != null)
{
row.APAccountID = vendorAccountMapping.APAcctID;
}
}
}

 

Can anyone assist on why it behaves differently for APInvoice?

icon

Best answer by Shawn Burt 15 April 2022, 16:37

View original

5 replies

Userlevel 5
Badge +1

Not 100% sure but I think your issue is that vendor id is part of the key for the record. You may need to trigger on rowupdated or rowupdating instead

Userlevel 2
Badge
    public class APInvoiceEntry_Extension : PXGraphExtension<APInvoiceEntry>
{
protected void APInvoice_VendorID_RowUpdated(PXCache cache, PXRowUpdatedEventArgs e)
//protected void _(Events.FieldUpdated<APInvoice, APInvoice.vendorID> e)
{
APInvoice row = (APInvoice)e.Row;

VendorAccountMapping vendorAccountMapping = SelectFrom<VendorAccountMapping>.Where<VendorAccountMapping.vendorID.IsEqual<@P.AsInt>.And<VendorAccountMapping.branchID.IsEqual<@P.AsInt>>>.View.Select(this.Base, row.VendorID, row.BranchID);
if (vendorAccountMapping != null)
{
row.APAccountID = vendorAccountMapping.APAcctID;
}
}
}

 

I amended the code as above but was greeted with this error:

 

Userlevel 5
Badge +1

try this:

    public class APInvoiceEntry_Extension : PXGraphExtension<APInvoiceEntry>
{
//protected void APInvoice_VendorID_FieldUpdated(PXCache cache, PXFieldUpdatedEventArgs e)
protected void _(Events.RowUpdated<APInvoice> e)
{
if (e.Row != null)
{
APInvoice row = e.Row;
if (row.VendorID != e.OldRow?.VendorID)
{
VendorAccountMapping vendorAccountMapping = SelectFrom<VendorAccountMapping>.Where<VendorAccountMapping.vendorID.IsEqual<@P.AsInt>.And<VendorAccountMapping.branchID.IsEqual<@P.AsInt>>>.View.Select(this.Base, row.VendorID, row.BranchID);
if (vendorAccountMapping != null)
{
row.APAccountID = vendorAccountMapping.APAcctID;
}
}
}
}
}

 

Userlevel 5
Badge +1

I just noticed that the BranchID is in your search for VendorAccountMapping. You might either drop the IF comparing the VendorID or add an OrElse to it for BranchID so it only fires if the Vendor or Branch change.

Userlevel 2
Badge

Hi @Shawn Burt , your suggested code works like a charm! Both VendorID and BranchID is needed since the DAC is being populated in such a way that one vendor may be linked to different APAcctID for different BranchID.

 

But it still puzzled me as to why the same piece of code didn’t work for APInvoiceEntry but it works flawlessly in APPaymentEntry. Looking into VS shows that row.APAccountID is being pointed to APRegister.APAccountID instead of APInvoice.APAccountID and initially I suspected that this was the root cause of it.

 

Anyway, thanks a lot for solving this!

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved