How to change the properties of one data class from another data class using a standard method

This ‘How-to’ provides information on how to use a standard method to change the properties (fields) of one data class from a different data class.

Methods are similar to operations:

The following development process demonstrates how to add a simple script to a data class (MYCLASS2) to update the properties (MYPROP1) of a different data class (MYCLASS1).

  1. Open your data class (MYCLASS2).

  2. Select the Methods tab > Methods block, and set the following field values:

    Code: Enter a new method code (MYMETH1).

    Description: Enter a description for the method.

    Return type: Select an appropriate return type.

    Operation: Select this option (yes). This will automatically generate the keys ( Keys block).

  3. Enter parameters (MYPARAM1), which need to be passed ( Parameter definitions block).

  4. Select the General tab > Scripts block, and set the following field values:

    Type: Select an appropriate type for your script.

    Scripts: Accept the displayed default code. This is automatically generated using the class code and script type. Alternatively, enter a unique code for your script.

    Running order: Accept the displayed numeric value. This is automatically generated to control the order in which the '$PROPERTIES' and '$METHODS' labels are called in the event. Alternatively, enter the running order code for your script.

  5. To add your script, right-click the Scripts code and select Processing editor from the selection menu. Enter the source code (see below):

    Note: This example will update the property (MYPROP1) in data class 'MYCLASS1' from the data class 'MYCLASS2'.

# Structure MYCLASS2

##########################################
#             METHODS                    #
##########################################
$METHODS
  Case CURPTH
    When ""
        Case ACTION
           When "AINSERT_AFTER" Gosub AINSERT_AFTER  # Update after insert
        Endcase  
  Endcase
Return

# Update properties in MYCLASS1 using MYCLASS2

$AINSERT_AFTER

    # Declaration new instance of MYCLASS1 data class
    Local Instance MYCLASS Using C_MYCLASS1
    MYCLASS = NewInstance C_MYCLASS1 AllocGroup Null
    ASTATUS=fmet MYCLASS.AREAD(this.KEY_VALUE)
    If ASTATUS<>[V]CST_AOK
      ASTATUS = fmet THIS.ASETERROR ("", "Not updated.", [V]CST_AERROR)  
      ARET_VALUE= CST_AFALSE
    Else
    # change property values and update
    MYCLASS.MYPROP1 = this.MYPROP2
    ASTATUS=fmet MYCLASS.AUPDATE
        If ASTATUS<>[V]CST_AOK
          ASTATUS = fmet THIS.ASETERROR ("", "Not updated.", [V]CST_AERROR) 
       ARET_VALUE= CST_AFALSE
    Else
       ARET_VALUE= CST_ATRUE
      Endif
    Endif

    # Free instances
    FreeGroup MYCLASS
Return
  6.  Save your script.

  7.  Save and validate your data class.