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.iohttps://opqo.io------------------------------