Search code examples
c#acumatica

APInvoice FieldUpdating Events Not firing - Acumatica


EDIT:

After I discovered the solution, I greatly simplified this question because I put a lot of things in the original version that -- as it turns out -- had nothing to do with the issue. I think this is more valuable for posterity with this stripped-down version:

/END EDIT

I have a modified APInvoice Graph. Here is my extension class declaration:

public class APInvoiceExtended : PXCacheExtension<APInvoice>, IBqlTable
{
    public static bool IsActive()  { return true; }
    public static Type GetUsrAmountExcludedType()
    {
        return typeof(APInvoiceExtended.usrAmountExcludedFromDiscount);
    }
    // Auto-Generated from ERP Customization Editor
    #region UsrAmountExcludedFromDiscount
    [PXDBDecimal]
    [PXUIField(DisplayName = "Amt Excl. Discount", Enabled = true, 
               IsReadOnly = false)]
    public virtual decimal? UsrAmountExcludedFromDiscount { get; set; }
    public abstract class usrAmountExcludedFromDiscount : PX.Data.BQL.BqlDecimal.Field<usrAmountExcludedFromDiscount> { }
    #endregion

I am changing the default behavior of the Discount calculations by allowing my user to specify a portion of the Invoice that is not subject to any discount.

From Use Case Description:

If the terms call for a 5% discount on an Invoice that is $125, but $25 of the cost was for shipping -- my user wants to exclude that from the discountable amount. So, in this case instead of offering a discount of $6.25, they want to exclude the shipping, and have a discount of $5 offered.

I have a custom field called "usrAmountExcludedFromDiscount ".

I have a subclass of the TermsID Attribute as Hugues Beauséjour described in this SO Post Override curyOrigDiscAmt

Everything works fine.

But, my user wants to go back and change the adjusted discount amount.

The problem is that the system does not respond to any event at the field level.

My field Override Code: (I tried many different events, all reacted the same)

    protected virtual void _(Events.FieldVerifying<APInvoiceExtension.usrAmountExcludedFromDiscount> e)
    {
        if (e.Row == null) return;
        APInvoice apInvoice = e.Row as APInvoice;
        var oldValue = e.OldValue as Decimal?;
        var newValue = e.NewValue as Decimal?;
        if (oldValue == newValue) return;
        var Cache = e.Cache;
        var Graph = e.Cache.Graph;
...

Etc... There is more code there, but the issue is it isn't fired and doesn't run.

How can I trigger a recalc when my custom field, usrAmountExcludedFromDiscount is changed by the user?


Solution

  • OK, it turns out my issue wasn't with the C# code, but with the ASPX code.

    When I went back to examine everything, I noticed this in my customization editCustom SS (NOTE: This screenshot is AFTER I changed it to TRUE. By default it is set to FALSE (ie checkbox is off) AND the checkbox is grayed out, so you can't change it!)

    Clicking on the checkbox shows this little description at the bottom:

    Help Msg

    I see no way of turning it ON if it is OFF. (You can do the opposite.) I don't know how this is supposed to get set, but apparently by default the checkbox is off (false).

    So, when the field gets updated, there is no commit callback, and no field events fired for it! (not SELECTING, SELECTED, VERIFYING, UPDATING, or UPDATED).

    I had to edit the ASPX, search for the field, and manually enter the setting and set it as TRUE:

    Edit ASPX

    Then, I "Generated new ASPX", published the project, and now my code actually fires when an update happens. (I ended up adding it to the TermsID subclass, so everything is done in one place.)

    Works like I always thought it should now!

    EDIT: Turns out, Acumatica has a strange idea about Design Language... As mentioned, the checkbox for "Commit Changes" is not able to be changed from ON to OFF. (You can change it from OFF it ON.)

    But -- Good News -- there is a way to change the field value.

    Changing Value

    If you click into the value column, the field changes to a combo box... the drop down option will magically appear and you can set the value to TRUE from there. Not intuitive, but at least it is possible.

    So, at least you can change the value without resorting to editing the ASPX code.

    Hope this saves someone from days of searching...