After spending much more time on this than I was hoping, here’s my end result:
- Override / extend ‘PrepareInvoice’ on SOOrderEntry, adding event handlers on the SOInvoiceEntry graph for ARTran RowInserted and ARInvoice RowPersisting. This allows me to handle events that originate from the SOOrderEntry graph that actually take place in the SOInvoiceEntry graph (code block 1)
- I can get the information I need from the inserted (but not yet persisted) ARTran, which I can then set in the RowInserting event of ARInvoice
This all was required because I needed to set a value of an ARInvoice originating from an SOOrder but that had not yet been saved, and therefore couldn’t be found.
public delegate IEnumerable PrepareInvoiceDelegate(PXAdapter adapter);
[PXOverride]
public IEnumerable PrepareInvoice(PXAdapter adapter, PrepareInvoiceDelegate baseMethod)
{
// Adds handlers for SOInvoiceEntry events which orginate from SOOrderEntry
PXGraph.InstanceCreated.AddHandler<SOInvoiceEntry>((graph) =>
{
graph.RowPersisting.AddHandler<ARInvoice>((cache, e) =>
{
var invoice = e.Row as ARInvoice;
if (invoice is null) return;
string invoiceStyle = InvoiceStyle;
cache.SetValue<DSAssignInvoiceStyle_SOOrderType_Ext.usrDSInvoiceStyle>(invoice, invoiceStyle);
cache.Update(invoice);
});
graph.RowInserted.AddHandler<ARTran>((cache, e) =>
{
var tran = e.Row as ARTran;
if (tran is null) return;
var invoice = graph.Caches[typeof(ARInvoice)].Current as ARInvoice;
string invoiceStyle = GetInvoiceStyle(tran.SOOrderType);
InvoiceStyle = invoiceStyle;
});
});
return baseMethod(adapter);
}