My use case is that when making a bill, the client wants to be able to specify a portion of the bill amount to which no discount is applied. (eg the total bill is $125, and the customer gets a 10% discount for paying in cash on delivery, but the discount does not apply to shipping costs. So, they want to say: "$25 of this is not discountable")
Then, I want to override the discount amount field based on this custom field.
I have added a data field to my APInvoice DAC via the Customization editor, and then activated it in my C# code as an extension:
namespace AcmeCorp.DAC
{
[PXCacheName("APInvoice")]
public class APInvoiceExt : PXCacheExtension<APInvoice>
{
public static bool IsActive() { return true; }
// Auto-Generated from ERP Customization Editor
#region UsrAmountExcludedFromDiscount
[PXDBDouble()]
[PXUIField(DisplayName = "Amt Excluded Discount")]
public virtual double? UsrAmountExcludedFromDiscount { get; set; }
public abstract class usrAmountExcludedFromDiscount : PX.Data.BQL.BqlDouble.Field<usrAmountExcludedFromDiscount> { }
#endregion
}
}
Now I want to check the value in that field to know how to adjust the discount field. How do I get this value?
[EDIT] This allows me to get the extension, but the value is null, even though I have checked the record, and there is a value present there.
I set up a using statement first:
using APInvoiceExtension = Acme.DAC.APInvoiceExt;
And then I can use that in the GetExtension() method:
protected virtual void _(Events.RowSelected<APInvoice> e)
{
if (e.Row == null) return;
double? Amt2Exclude = null;
APInvoice apInvoice = e.Row as APInvoice;
var chkRef = apInvoice.RefNbr;
var ext = PXCache<APInvoice>.GetExtension<APInvoiceExtension>(apInvoice);
if (ext != null)
{
Amt2Exclude = ext.UsrAmountExcludedFromDiscount;
}
}
The RefNbr has a value just fine. But, as I said the value for UsrAmountExcludedFromDiscount is null.
There's a few things I'm kind of confused about, like your DAC has the namespace AcmeCorp.DAC in the code example but you have a using statement that uses a different namespace. I would also probably use a decimal over a double for your data type in this instance, and in any instance where you are working with currencies.
For retrieving an extension, you can simply do the following.
protected virtual void _(Events.RowSelected<APInvoice> eventHandler)
{
APInvoice row = eventHandler.Row;
if (row is null) return;
APInvoiceExtension rowExt = row.GetExtension<APInvoiceExtension>();
if (rowExt != null)
{
excludedAmount = rowExt.UsrAmountExcludedFromDiscount;
}
}
I would probably avoid renaming the extension from APInvoiceExt to APInvoiceExtension on your using statement, but the above code should work to retrieve the extension. If you still have a null value in your extension, I would say that's most likely because a value never gets assigned to it, and you may need to post more of the code for us to evaluate what's actually going on here.