How to create data for a child class from another data class using an operation
This ‘How-to’ provides information on how to use an operation to create data for a child data class from a different data class. For example, from a data class that is not a parent of the child class. Operations are similar to methods.
The following development process demonstrates how to add data to the child class (MYCLASS1.CL1) of a data class (MYCLASS1) from a different data class (MYCLASS2), by adding a simple script to create an operation on (MYCLASS1) which is called from (MYCLASS2):
Open your data class (MYCLASS1).
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 automaticallygenerate the keys ( Keys block).
Enter parameters (MYPARAM1) which need to be passed ( Parameter definitions block).
Select the General tab >Scripts block, and set the following field values:
Type: Select a type appropriate 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.
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 use the key (Methods tab > Keys block) which is passed to read the current context from 'MYCLASS1' and then update the property (PROP1) using the parameter 'MYPARAM1' which is passed to it.
$Structure MYCLASS1###########################################METHODS ###########################################$METHODSCase CURPTHWhen ""Case ACTIONWhen "MYMETHOD" Gosub MYMETHOD # Update MYPROP1 method Endcase EndcaseReturn# Update MYPROP1 using MYPRAM1 which is passed# KEY1 and MYPARAM1 are passed and used in this operation$MYMETHODLocal Integer NEWLINE# Use standard method CRUD to read MYCLASS1ASTATUS=fmet this.AREAD(MYCLASS1)If ASTATUS<>[V]CST_AOKARET_VALUE= CST_AFALSEReturnElseARET_VALUE= CST_ATRUE# create a new line for MYCLASS1.CL1 at last positionNEWLINE=fmet this.ADDLINE("CL1",[V]CST_ALASTPOS)If NEWLINE=[V]CST_ANOTDEFINED # check status after creating lineARET_VALUE = CST_AFALSEEndifIf ARET_VALUE = CST_ATRUE# update CL1 with values passed this.CL1(NEWLINE).MYPROP1 = MYPARAM1ASTATUS=fmet this.AUPDATEIf ASTATUS<>[V]CST_AOKARET_VALUE= CST_AFALSEEndifEndifEndifReturn
6. Once the operation has been created, the operation must be called from the data class 'MYCLASS2' or from any other data class. Add a similar code to the following. This calls 'MYMETHOD' which is defined above.############################################### Data Class - MYCLASS2 ##############################################################################################METHODS ###############################################$METHODSCase CURPROWhen ""Case ACTIONWhen "AINSERT_AFTER" : Gosub AINSERT_AFTEREndcaseEndcaseReturn#Action After insert$AINSERT_AFTER# Declaration new instance of MYCLASS2 data classLocal Instance MYCLASS Using C_MYCLASS2MYCLASS = NewInstance C_MYCLASS2 AllocGroup NullASTATUS=fmet MYCLASS.MYMETHOD(KEYID,MYPARM1)If ASTATUS<>[V]CST_ATRUEASTATUS = fmet THIS.ASETERROR ("", "Not updated.", [V]CST_AERROR) Endif# Free instancesFreeGroup MYCLASSReturn
7. Save your script.8. Save and validate your data class.