Auto-populating relationship attributes in a parent/child table relationship
May 27, 2009 in Maximo, Maximo 6, Maximo 7, Maximo: Programming by Michael Chrisman 9 Comments
Although it is easy to create MBOs (tables) that have a parent/child relationship in Maximo, getting the relationship fields to auto-populate when doing data entry on a screen requires a little coding. Let’s go over how.
In this example, let’s say we created our own child table of ASSET. We will call this MBO MYASSETCHILD. Since ASSETNUM and SITEID are the logical keys to ASSET, we add these attributes to MYASSETCHILD (of course we add other attributes too). Now we create a relationship from ASSET to MYASSETCHILD where: ASSETNUM = :ASSETNUM and SITEID = :SITEID. (Of course we would then run ConfigDB.) At this point we can add our child MBO to the Asset screen and the system will properly display the child records for a selected asset. The problem is that when you try to add a new record from the screen, it will not populate the ASSETNUM and SITEID from the ASSET on the new MYASSETCHILD record. You could add these two fields to the screen for the MYASSETCHILD record and make users re-enter this data, but that would really suck. What you have to do is create a custom MBO class to grab the values from the parent (asset) record and populate them on the current record.
The first step is to create a custom MBO class. Since our example had us creating a new MBO, we will be extending the psdi.mbo.custapp.CustomMbo class. In the non-Set file, we need to override the add() method. Then we get the parent MBO. This allows us access to all of its values.
1: package mycompany.app.asset;
2:
3: import java.rmi.RemoteException;
4:
5: import psdi.mbo.MboRemote;
6: import psdi.mbo.MboSet;
7: import psdi.util.MXException;
8:
9: public class CustMyAssetChild extends psdi.mbo.custapp.CustomMbo implements CustMyAssetChildRemote
10: {
11: public CustMyAssetChild(MboSet mboSet) throws MXException, RemoteException
12: {
13: super(mboSet);
14: }
15:
16: @Override
17: public void add() throws MXException, RemoteException
18: {
19: try
20: {
21: MboRemote parentMbo = this.getOwner();
22:
23: this.setValue("ASSETNUM", parentMbo.getString("ASSETNUM"));
24: this.setValue("SITEID", parentMbo.getString("SITEID"));
25: }
26: catch ( Exception e)
27: {
28: //
29: }
30: super.add();
31: }
32: }
Of course, the question is what happens if someone tries to load data into the MBO without having a parent Asset? Well that is why I placed the code in a try/catch. If the getOwner() fails, then I just ignore the error. (I know it is not very clean, but it works.)
Now compile (don’t forget to RMIC). You will need to rebuild/deploy the ear. Now in database configurations, change the class for MYASSETCHILD to mycompany.app.asset.CustMyAssetChildSet. Now run configDB.
Now try your screen. When you add a new record to MYASSETASSET, Maximo will now auto-populated the AssetNum and SiteID fields for you.
have u got an example of the xml/screen file?