Set a field as required after save

This entry is part of the Maximo Java Development series.

You may have noticed that there are some fields that cannot be modified once an object is saved. An example of this behavior is the ASSET.ASSETNUM field. When you create a new asset you can set it's ID but once you have saved it it cannot be modified.
In this little post I'll show you how to implement this behavior for other fields.

Let's pretend we want to avoid any modification to the original asset serial number once the asset is created.
The Mbo.toBeAdded() method allows to detect if the current Mbo has been already saved or not. The Mbo.setFieldFlag() method allows to set a field as required. You just have to override the init() method of the Mbo put the appropriate logic.

public void init() throws MXException
{
  super.init();

  if (!toBeAdded())
    setFieldFlag("SERIALNUM", READONLY, true);
}


The Mbo.setFieldFlag() method allows to set many fields in one shot so a very common technique is to define two arrays of strings to define the lists of attributes to be set as readonly. Look at the following example.

static String alwaysReadOnlyFields[] = {"ORGID", "SITEID"};
static String readOnlyAfterSaveFields[] = {"SERIALNUM", "PRIORITY"};

public void init() throws MXException
{
  super.init();

  setFieldFlag(alwaysReadOnlyFields, READONLY, true);

  if (!toBeAdded())
    setFieldFlag(readOnlyAfterSaveFields, READONLY, true);
}

Labels: ,