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:

  1. 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>
      {
      }
    }
  2. Add the following using directives.
    using PX.Data;
    using PX.Data.BQL.Fluent;
    using PX.Objects.AR;
  3. Use Acuminator to suppress the PX1016 error in a comment. In this course, for simplicity, the extension is always active.
  4. Define the FieldDefaulting event handler for the UsrPrepaymentPercent field of the ARPayment 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 the UsrPrepaymentPercent field of the ARPayment DAC. You have checked for the null value of setupRecord 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, specify ARRegisterExt.usrPrepaymentPercent instead of ARPaymentExt.usrPrepaymentPercent if the usrPrepaymentPercent field belongs to the ARRegisterExt 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.
Note: You need to specify the SourceField parameter if the field names are not identical.