Step 2: Deriving the Value of the Field
You can implement the deriving of a field value from the
RSSVSetup
DAC and the copying of it to the
ARPayment
DAC by using one of the following:
- The FieldDefaulting event
- The PXDefault attribute
To populate the UsrPrepaymentPercent
field of the
ARPayment
extension when a payment is created, you can use
the FieldDefaulting event. Do the following:
- Create an extension of the
ARPaymentEntry
graph, as shown in the following code.You learned the name of the graph to extend in Instruction 1 of the previous step.
namespace PhoneRepairShop { public class ARPaymentEntry_Extension : PXGraphExtension<ARPaymentEntry> { } }
- Add the following
using
directives.using PX.Data; using PX.Data.BQL.Fluent; using PX.Objects.AR;
- Use Acuminator to suppress the PX1016 error in a comment. In this course, for simplicity, the extension is always active.
- Define the
FieldDefaulting
event handler for theUsrPrepaymentPercent
field of theARPayment
extension, as shown in the following code.public virtual void _(Events.FieldDefaulting<ARPayment, ARPaymentExt.usrPrepaymentPercent> e) { ARPayment payment = (ARPayment)e.Row; RSSVSetup setupRecord = SelectFrom<RSSVSetup>.View.Select(Base); if (setupRecord != null) { e.NewValue = setupRecord.PrepaymentPercent; } }
In the code above, you have selected the record with the repair work order preferences and assigned the
PrepaymentPercent
field value to theUsrPrepaymentPercent
field of theARPayment
DAC. You have checked for the null value ofsetupRecord
so that the NullReferenceException exception is not thrown if the data on the form has not been filled in yet.Note: In the event handler, specifyARRegisterExt.usrPrepaymentPercent
instead ofARPaymentExt.usrPrepaymentPercent
if theusrPrepaymentPercent
field belongs to theARRegisterExt
DAC extension.
Another way to derive the default value is to use the
PXDefault attribute, which performs the same logic. If
you use this approach, the PXDefault attribute for the
UsrPrepaymentPercent
field should look as follows.
[PXDefault(typeof(Select<RSSVSetup>), SourceField = typeof(RSSVSetup.prepaymentPercent),
PersistingCheck = PXPersistingCheck.Nothing)]
This approach provides the following advantages:
- You do not need to create a graph extension.
- Your logic is written in declarative style.