Search This Blog

Friday, January 24, 2020

MAXIMO:MBO transactions and significance of save() method , getting MboSet from relationship vs MXServer


****Maximo Transactions - significance of Save , MXServer getMboSet ****

Today I am going to explain about when you are adding or updating records in Maximo via code (e.g. Java, Autoscript etc.), then you need to understand how maximo handles transactions.

We will need to consider , when you are adding something in the current transaction (so when the user clicks 'save', you record will be saved) OR when you have created a new transaction (and you have to save it via code).

Lets talk about below scenario I have recently worked to meet business requirement:
It was required by business , When a user tries to save an asset with a location, the system must go out and look at the location for other assets. 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 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 (Autoscript):
###########################################################################################################################################################################
#
# Version       Date         Author                 Request #                Remarks
#   1.0       DD|MM|YYYY   Prashant Bavane    Asset and Location linking    Associate additional Dummy asset to location when no asset exists , during creation of asset
#
###########################################################################################################################################################################
from psdi.server import MXServer
from psdi.mbo import Mbo
from psdi.mbo import MboRemote
from psdi.mbo import MboSet
from psdi.mbo import MboSetRemote
## get Locatoins MBO Set. this is the key our transaction  
locationMboSet = mbo.getMboSet("LOCATION")  
##get current location record  
curLocation = locationMboSet.getMbo(0)
##get all assets for the current location  
assetMboSet = curLocation.getMboSet("ASSET")
##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")[:7]) 
 dummyAsset.setValue("description","DUMMY place holder Asset do not use")

 


In this example how we got the Locations MBO Set is very important. You will notice that I 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 I 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").


However, 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. The other thing is that since I have done my save, the new asset would exist before the user has officially saved theirs. (This transaction completes before the Maximo transaction does.)


Both methods have their place and I have used both. It is important to understand how to use each and what that means to your code.





Cheers!!!

No comments: