Maximo

Maximo

Come for answers, stay for best practices. All we're missing is you.

 View Only
  • 1.  Adding a route stop via automation script

    Posted 03/25/24 10:45 AM

    Hi all,

    I'm new here so just a quick introduction! I started my role as Maximo Administrator just over 12 months ago and feel like I have a decent(ish) understanding of Maximo and the way my company uses Maximo.

    I've come across an issue with an automation script where I want to add a new route stop (I just constantly get the error "BMXAA3614E - Need a route.")

    I created a new column in the ASSET table where the user can select a new parent asset.

    I'm then getting info about this new asset, including route, stop sequence, etc, then i am calculating the next available stop sequence value.

    I wish to do an .add() to the route that the new parent asset is in, and set the values: ASSETNUM = the current record assetnum, ROUTE = the route the new parent is in, STOPSEQUENCE = the next available stop sequence integer i've calculated.

    However, I constantly get the error "BMXAA3614E - Need a route." and i'm really struggling to find a way around this - is anybody able to shed any light?

    Thanks in advance!



    ------------------------------
    Jordan Brocklehurst
    ------------------------------


  • 2.  RE: Adding a route stop via automation script

    Posted 03/25/24 11:37 AM

    Hi Jordan,

    First, you probably want to use the Move / Modify Asset to change the parent instead of a custom field as there is out of the box functionality to do that.

    Second, you are making the assumption that the parent asset only belongs to one route, so you will need to consider the case if it does belong to more than one route how you are going to handle that.

    With that said, here is a script to do what you are asking for. It has a launch point on save for Asset and checks if the parent field has been modified and then adds the asset to the route that the parent belongs to. Note that this has a scriptConfig variable so if you are using the VS Code extension you can deploy it directly.  Documentation on the VS Code extension can be found here:

    https://marketplace.visualstudio.com/items?itemName=sharptree.maximo-script-deploy

    SqlFormat = Java.type("psdi.mbo.SqlFormat");
    MboConstants = Java.type("psdi.mbo.MboConstants");
    
    MXServer = Java.type("psdi.server.MXServer");
    
    main();
    
    function main() {
        // Basic sanity check that we have the implicit mbo variable and it is an asset MBO.
        if (typeof mbo !== "undefined" && mbo && mbo.isBasedOn("ASSET")) {
            // if the parent asset has been changed.
            if (mbo.isModified("PARENT")) {
                var parentRouteStopMboSet = mbo.getMboSet("$route_stop", "ROUTE_STOP", "assetnum=:parent and siteid=:siteid");
                // if there is one and only one route that contains the parent asset then proceed.
                if (parentRouteStopMboSet.count() == 1) {
                    parentRouteStop = parentRouteStopMboSet.moveFirst();
                    var parentRoute = parentRouteStop.getMboSet("ROUTES").moveFirst();
                    var childRouteStopSet = parentRoute.getMboSet("ROUTE_STOP");
    
                    // find the next sequence number
                    childRouteStopSet.setWhere("stopsequence is not null");
                    childRouteStopSet.setOrderBy("stopsequence desc");
    
                    var childRouteStop = childRouteStopSet.moveFirst();
    
                    var nextSequence = 1;
    
                    if (childRouteStop) {
                        nextSequence = childRouteStop.getInt("STOPSEQUENCE") + 1;
                    }
    
                    // check that the asset isn't already in the route. Use a separate MboSet instance as to not interfere with the current transaction.
                    var checkRouteStopSet;
    
                    try {
                        checkRouteStopSet = MXServer.getMXServer().getMboSet("ROUTE_STOP", mbo.getUserInfo());
                        // Using SqlFormat https://www.sharptree.io/blog/2022/2022-01-31-sql-format/                
                        var sqlf = new SqlFormat("route = :1 and assetnum = :2 and siteid = :3");
                        sqlf.setObject(1, "ROUTE_STOP", "ROUTE", parentRoute.getString("ROUTE"));
                        sqlf.setObject(2, "ROUTE_STOP", "ASSETNUM", mbo.getString("ASSETNUM"));
                        sqlf.setObject(3, "ROUTE_STOP", "SITEID", mbo.getString("SITEID"));
    
                        checkRouteStopSet.setWhere(sqlf.format());
                        checkRouteStopSet.reset();
    
                        if (checkRouteStopSet.isEmpty()) {
                            var newRouteStop = childRouteStopSet.add();
    
                            newRouteStop.setValue("ASSETNUM", mbo.getString("ASSETNUM"));
                            newRouteStop.setValue("STOPSEQUENCE", nextSequence);
                        }
                    } finally {
                        try {
                            // close the MboSet
                            // https://www.sharptree.io/blog/2021/2021-11-22-close-things/
                            if (checkRouteStopSet) {
                                checkRouteStopSet.close();
                                checkRouteStopSet.cleanup();
                            }
                        } catch (ignore) {}
                    }
                }
            }
        }
    }
    
    var scriptConfig = {
        "autoscript": "ADD.ASSET.TO.ROUTE",
        "description": "Add the asset to a route when the parent changes",
        "version": "1.0.0",
        "active": true,
        "logLevel": "ERROR",
        "scriptLaunchPoints": [
            {
                "launchPointName": "ADD.ASSET.TO.ROUTE",
                "launchPointType": "OBJECT",
                "active": true,
                "description": "Add the asset to a route when the parent changes",
                "objectName": "ASSET",
                "save": true,
                "add": true,
                "update": true,
                "delete": false,
                "beforeSave": true
            }
        ]
    };
    


    ------------------------------
    Jason VenHuizen
    https://sharptree.io
    https://opqo.io
    ------------------------------



  • 3.  RE: Adding a route stop via automation script

    Posted 03/25/24 02:54 PM

    Hi Jason,

    Thank you so much for your quick reply!

    Just to clarify - on a couple of your notes!

    Yep, I've previously used the move/modify asset function, but I've created a new tab for a security group that allows for a few different things including setting a new parent asset (in this instance, i'm actually displaying different info about the parent asset and route info before they pushbutton confirm they want to perform the action).

    Yes, I am assuming that the parent asset will only be in one route given the nature of these specific routes. I just checked via SQL and have actually found one instance where one of the parents appear in different routes, so thank you for raising this oversight!

    I'll have a look at implementing the required functionality using the script you've so kindly provided!

    Thanks again!



    ------------------------------
    Jordan Brocklehurst
    ------------------------------