Skip to main content

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?

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


    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:

 


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;
}
}
}
}
}

 


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.


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