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
ARRegister DAC by using one of the following:
- The FieldDefaulting event
- The PXDefault attribute
To populate the UsrPrepaymentPercent field of the
ARRegister DAC extension when a payment is created, you
will use the FieldDefaulting event. Do the following:
- Create an extension of the
ARPaymentEntrygraph, as shown in the following code.You learned the name of the graph to extend in Instruction 1 of the previous step.public class ARPaymentEntry_Extension : PXGraphExtension<ARPaymentEntry> { } - Add the following
usingdirectives.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
FieldDefaultingevent handler for theUsrPrepaymentPercentfield of theARRegisterextension, as shown in the following code.public virtual void _(Events.FieldDefaulting<ARPayment, ARRegisterExt.usrPrepaymentPercent> e) { 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
PrepaymentPercentfield value to theUsrPrepaymentPercentfield of theARRegisterDAC. You have checked for the null value ofsetupRecordso that the NullReferenceException exception is not thrown if the data on the form has not been filled in yet.
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.
