Skip to main content
Lesson 5

Deriving the Value of a Custom Field from

Back to T190: QuickStartInCustomization

Lesson 5: Deriving the Value of a Custom Field from

Another Entity

In Acumatica ERP, a user can create a payment for an invoice by clicking the Pay button or command on the Invoices (SO303000) form. When the payment is created, it is opened on the Payments and Applications (AR302000) form. The workflow for the Battery Replacement service involves one payment, which is made upon completion of the work. Conversely, the workflow for the Liquid Damage service involves both a prepayment before the repair work is assigned and a final payment aer the work is complete. For the creation of the prepayment, you now need to have the default prepayment percentage for the payment displayed on the Payments and Applications (AR302000) form to facilitate the entry of the prepayment amount, and to make it possible for a user to change that percentage for the current payment. To do that, you need to derive the value of the Prepayment Percent element on the Repair Work Order Preferences (RS101000) form and assign it to the corresponding custom field of the Payments and Applications (AR302000) form. You will perform these tasks in this lesson.

Lesson Objectives

In this lesson, you will learn how to do derive the value for a custom field from another form.

Step 5.1: Adding a Custom Field to the Payments and Applications Form—Self-

Guided Exercise

To display and modify the prepayment percentage on the Payments and Applications (AR302000) form, you need to add the Prepayment Percent box to the form. Complete the following general tasks:

  1. By using the Element Inspector, learn the name of the DAC and graph that define the Summary area of the Payments and Applications (AR302000) form. In this case, the DAC is ARPayment and the graph is ARPaymentEntry. You need the DAC name to know which database table and DAC to extend. You will need the graph name in the next step.
  2. Add a column named UsrPrepaymentPercent to the ARPayment table with the same parameters as are specified for the PrepaymentPercent field of the RSSVSetup table. The data type of the column is decimal(9, 6).
  3. Create an extension of the ARPayment DAC, and add the UsrPrepaymentPercent field to the extension.
                  If you create a DAC extension by using the Customization Project Editor, it creates an extension
                  of the base DAC. So in this case, the system will create an extension of the ARRegister DAC
                  because the ARRegister DAC is the base DAC for the ARPayment DAC.
    
       The code for the field is shown below.
    
                   #region UsrPrepaymentPercent
                   [PXDBDecimal()]
                   [PXDefault(TypeCode.Decimal, "0.0",
                       PersistingCheck = PXPersistingCheck.Nothing)]
                   [PXUIField(DisplayName = "Prepayment Percent")]
                   public Decimal? UsrPrepaymentPercent { get; set; }
                   public abstract class usrPrepaymentPercent :
    

Lesson 5: Deriving the Value of a Custom Field from Another Entity | 49

                    PX.Data.BQL.BqlDecimal.Field<usrPrepaymentPercent>
                { }
                #endregion

4. Add a box for the UsrPrepaymentPercent field to the Summary area of the Payments and Applications (AR302000) form. On the Customized Screens page, the location of the element appears as shown in the following screenshot.

     Figure: Prepayment Percent element

5. Correct the width for the Prepayment Percent label. To do this, in the Column element that is the parent to the Prepayment Percent element, for the LabelsWidth property, specify the M value. 6. Publish the customization project.

Step 5.2: Deriving the Default Value of the PrepaymentPercent 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 DAC extension when a payment is created, you will 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.
            public class ARPaymentEntry_Extension : PXGraphExtension<ARPaymentEntry>
            {
            }
    

Lesson 5: Deriving the Value of a Custom Field from Another Entity | 50 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)
                 {
                     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.

                 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.
            You need to specify the SourceField parameter if the field names are not identical.
    

Step 5.3: Testing the Deriving of the Field Value

To test that the value of the Prepayment Percent box on the Payments and Applications (AR302000) form is correctly specified for payments, rebuild the Visual Studio project and do the following:

  1. On the Invoices (SO303000) form, open the INV000049 invoice, which you created in Step 4.3: Testing the Creation of an Invoice
  2. Release the invoice by doing the following: a. Type 40 in the Amount box of the Summary area. Lesson 5: Deriving the Value of a Custom Field from Another Entity | 51
      b. On the form toolbar, click Remove Hold.
      c. Release the invoice by clicking Release on the form toolbar.
    
  3. On the More menu (under Processing), click Pay. The Payments and Applications (AR302000) form opens. In the Summary area, notice that the Prepayment Percent box has the 10.00 value (see the following screenshot), which has been copied from the Repair Work Order Preferences (RS101000) form.
      Figure: The Prepayment Percent box
    
  4. Save the payment.

Lesson Summary

In this lesson, you created a custom field on the Payments and Applications (AR302000) form and learned how to assign its default value, which is derived from another entity. To assign a default value for a custom entity, you have done the following:

  1. Defined the extension of a graph in which the field is initialized
  2. In the graph extension, defined the FieldDefaulting event handler for the custom field

The following diagram shows the changes that you have performed in this lesson. Lesson 5: Deriving the Value of a Custom Field from Another Entity | 52 Lesson 6: Debugging Customization Code | 53