This article is obsolete. Please refer to this one.
If you are a creating custom objects and applications in TPAE you may
need at some point to create a child table like the Subassemblies in the Assets application.
Let's pretend we have a parent table called TB1 and a child table called TB2.
- TB1: Parent table
- TB1ID: Identifier of records in TB1
- ...
- TB2: Child table
- TB2ID: Identifier of records in TB2
- TB1ID: Reference to the parent record in TB1 table
- ...
The fundamental concept is to have a field in the child table that points to a specific row in the parent table. In our example such field is TB2.TB1ID.
In order to have the child table to behave correctly is to 'link' in some way the child rows to the parent object. If not doing this the child records will disappear as soon as you save the record.
Basic method using Database Configuration and Application Designer
First of all you need a relationship from the TB1 table to the TB2 records. In Database Configuration define the following relationship in object TB1:
- Relationship: TB2
- Child Object: TB2
- Where Clause: TB1ID=:TB1ID
In the Application Designer you have to use this relationship in the child table.
The last important step is to initialize the TB2.TB1ID field on child records. To achieve this, add a Default Value control with the following configuration:
- Attribute: TB1ID
- From Data Source ID: results_showlist
- From Attribute: TB1ID
This will set the 'link' between parent and child tables.
Alternative method using Java
An alternative method is to initialize this link in the child Mbo using Java. Here is how the child Mbo class should look like.
public class Tb2Mbo extends Mbo implements MboRemote
{
public Tb2Mbo(MboSet ms) throws MXException, RemoteException
{
super(ms);
}
public void add() throws MXException, RemoteException
{
super.add();
MboRemote ownerMbo = getOwner();
if(ownerMbo != null)
{
String tb1id = ownerMbo.getString("tb1id");
setValue("tb1id", tb1id, NOACCESSCHECK|NOVALIDATION_AND_NOACTION);
}
}
}
Labels: advanced, application, java