A Division of Technology Associates International Corporation
Maximo Blog

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.

 

Comments

dairywhizz77
Posted on May 28, 2009

have u got an example of the xml/screen file?

Michael Chrisman
Posted on June 04, 2009

dairywhizz77

If you look at the Asset Spec tab you can see an example of the screen/xml.

Mike

Geoffrey
Posted on September 11, 2009

You don’t even need a class extension to do this, you can always use the default value control on your sub table, and default your related fields to be the value from an attribute in the parent object.

Murilo Cruz
Posted on March 08, 2010

@Geoffrey
Geoffrey, where can I find more detail about what you are saying?

thanks

Geoffrey
Posted on March 23, 2010

In application designer, click toggle show all controls (on select action menu).

On your child table, drag a “default value” control onto the table.  You should then see the words Default Value right above the table you dragged it on. Default value controls are only visible if you click toggle show all controls, otherwise when you drag it on the table, it will see to just disappear.

Select the default value control you just added and click properties and fill them out as follows:

Attribute - This is your foreign key, enter the attribute of your child table that relates to the attribute on your parent table.

Value - Leave this blank.
From Data Source ID - This is the data source ID of your parent record.  If you are creating a child table on an existing Maximo application, this would be MAINRECORD.  If this table is a child of another table, enter the DATA SOURCE ID of the other table (this can be found be viewing the properties of the parent table in application designer).

From Attribute - This is the attribute from the parent table that relates to the Attribute in the child table.

Default Type: Query

Keep in mind you can add more than one default value to a child table.

As an example, lets say I wanted to create a custom table as a child of the WORKORDER table in the out of the box work order application.

In application designer, I would add a Table Control, set the parent data source id of the table to MAINRECORD and enter the relationship name (which must be set up in Database Config first)

Then add a default value control on that table control with the following properties:

Attribute: WORKORDERID (assuming I added this as an attribute of the child table, I could have called this PARENTWOID)
VALUE: <blank>
From Data Source ID: MAINRECORD
From Attribute: WORKORDERID
Default Type: Query

The relationship is what allows me to see records already saved on the database, the default value control will set WORKORDERID on my child table equal to the WORKORDERID of the parent table WORKORDER. 

My relationship would be from WORKORDER to my CHILDTABLE with a where clause of WORKORDERID = :WORKORDERID.

If you want I can email you screen shots if you need more clarification.

Murilo Cruz
Posted on March 24, 2010

@Geoffrey

Thanks!
It works perfectly

Geoffrey
Posted on March 24, 2010

Awesome!  I was not sure how familiar you were with what I was talking about, so I tried to be as detailed as possible.

Glad everything worked out.

Murilo Cruz
Posted on March 24, 2010

Actually.. I’m not so familiar with Maximo.. but I’m trying to discover how to do things without Java and putting some data directly in the database.

And after started to read the Default Value and other similar controls I’ve discovered about the Data Source.

Combining the Data Source with Default Value, I can do lots of things that I made customizing Java.

But the Data source’s Listeners property is a check box in the maximo version that I’m using…. (actually.. a check box that doesn’t checks when I click! lol)

And without this.. I get back to Java code to update controls when the data source changes

As this seems to me to be a bug, I’ll try to talk with IBM about.

Maybe with the new Maximo version, this bug is fixed, or the listener field just works at older versions.

Thanks a lot for the help.

Murilo Cruz
Posted on March 26, 2010

I’ve found a way to don’t need to use the Listeners property. But I don’t know if it works in all cases….

If the control that fires the change on the mbo has a property CHANGE EVENT, st it to RESET CHILDREN.
With this, all the beans (as the components on the screen and data sources) will be updated, doing the work that the listeners property would do.

good luck for who will follow my tip… that god lights you in this dark path called MAXIMO…
hahahahaha

Post a Comment

Remember my personal information.
Notify me of follow-up comments?

We don't know if you're a human. Confirm below: