Understanding Transactions
When you are adding or updating records in Maximo via code (e.g. Java, Autoscript, etc), you need to understand how Maximo transactions work: you need to know whether you are adding something in the current transaction (so when the user clicks ‘save’, you record will be saved) or whether you are creating a new transaction (and you have to save it via code).
We recently had this requirement: when a user tries to save an asset with a location, the system must go out and look at the location for other assets to determine if there are other assets in the same location. If there are no other assets attached to the location, then add a dummy asset and attach it to the asset also. (Yes there was a functional reason for this!)
This is a really good example to show both ways of working with transactions in Maximo. For the new “dummy” asset, we can either attach it to the current save so when the save process finishes, it will also save our new asset. This has a benefit of, should something go wrong on the save, then our changes are also not saved. Here is the code snippet (auto script):
...
# get Locatoins MBO Set. this is the key our transaction
locationMboSet = mbo.getMboSet("LOCATIONS");
# get current location record
curLocation = locationMboSet.getMbo(0);
# get all assets for the current location
assetMboSet = curLocation.getMboSet("ASSETS");
# don't look at the current record (in case the users is updating the record).
assetMboSet.setWhere("assetnum != " + mbo.getString("ASSETNUM") + "'");
assetMboset.reset();
#check for no records
if (assetMboSet.count() == 0)
{
# add dummy asset
dummyAsset = assetMboSet.add();
dummyAsset.setValue("assetnum", "DUMMY"+ mbo.getString("assetuid").substring(7));
dummyAsset.setValue("description","DUMMY place holder Asset do not use");
...
In this example, the way in which we got the Locations MBO Set is very important. You will notice that we used the current Mbo (the getMboSet() method). By doing this, it keeps that record set as part of the current transaction in Maximo. So later in the code, when we add the new Asset, it is also part of the same transaction, and Maximo will save or roll it back with the main record that is being added/updated. Doing it this way, you would never want to perform a save via code as it would cause an error to the user when the save tried to happen within Maximo – the dreaded “record has been updated by another user”.
Alternatively, had I used this line of code to get the locations MBO Set:
...
locationMboSet = MXServer.getMXServer().getMboSet("LOCATIONS",...);
...
Maximo would have created a new transaction for the locations MBO set. That means that for this code to work, I would have to add a locationMboSet.save(). Without the save(), I would lose my new asset record. In this case, since we have done our save, the new asset would exist before the user has officially saved theirs. This transaction completes before the Maximo transaction does.
Both approaches have their place, and we have used both. It is important to understand how to use each and what that means to your code.