- A Collection of Maximo Automation Script Examples
- Prevent completion / closure of work orders with open labor transactions
- Automatic Status Changes for Service Requests (SR) with Automation Script
- Import Classes and Packages using Javascript Language and Automation Scripts
- Automation Script: params variable not working with JavaScript language
- How to Rollup Cost with the Automation Script
- Disable Maximo business rule with an Automation Script
- Your first Automation Script in Maximo 7.5
There are many organizations that want to automatically change the status of an SR to in progress or resolved whenever one or more workorders have been started or completed. In this tutorial, we will create an automation script that will automatically change the status of the SR when a related work order is changed to in progress. Also, if an SR has multiple workorders, change the status of the SR to RESOLVED when all related workorders are completed.
In this first half of this tutorial, we will setup a script that will change the status of the SR from QUEUED or NEW to INPROG whenever a workorder status is changed to INPRG.
Step 1:
Create a new automation script with an attribute launch point on the STATUS field on the WORKORDER object. First Go To -> System Configuration -> Platform Configuration -> Automation Scripts. Click Select Action -> Create -> Script with Attribute Launch Point. In this step, set the name of the launch point to SRSTATUS with a description of ‘SR Status Change’. Then set the object to WORKORDER and attribute equal to STATUS. Then click Next.
Step 2:
Set the script name to ‘SRSTATUS’ and the description of ‘SR Status Change’ (You can change these to whatever you want, but for simplicity sake, we just name it the same as the launch point). Then set the language to ‘javascript’. This is my preferred language and you can use any one you want, but the code below is for javascript. Then click Next.
Step 3:
Copy and paste this code:
importClass(Packages.psdi.server.MXServer); // Check if this workorder is INPRG status if (mbo.getString("STATUS") == 'INPRG') { // Find the related SR if any var sr = mbo.getMboSet("ORIGTICKET").getMbo(0); // If SR is in QUEUED or NEW status if (sr != null && (sr.getString("STATUS") == "QUEUED" || sr.getString("STATUS") == "NEW")) { // Change SR status to INPROG sr.changeStatus("INPROG", MXServer.getMXServer().getDate(), null); } }
And click Create.
Let’s analyse this code. First we check if the current MBO (in this case, it’s the work order MBO) status is equal t0 INPRG. If so, find the related SR if any and if the SR status is equal to QUEUED or NEW, then change the status to INPROG. Simple enough, how let’s test it.
Step 4:
Now the script is created, we can test it out. Find a work order in APPR status that also has an SR related to it and change the work order status to INPRG. You should get a dialog like so:
So now, when any workorder status is changed to INPRG, the related SR will also be changed. But what about when an SR has multiple work orders and when all the work orders are completed, can we change the SR to RESOLVED? Yes we can.
Step 5:
First we have to edit the existing script we just created. Find the SRSTATUS script and edit the code. The complete code should look like this:
importClass(Packages.psdi.server.MXServer); // Check if this workorder is INPRG status if (mbo.getString("STATUS") == 'INPRG') { // Find the related SR if any var sr = mbo.getMboSet("ORIGTICKET").getMbo(0); // If SR is in QUEUED or NEW status if (sr != null && (sr.getString("STATUS") == "QUEUED" || sr.getString("STATUS") == "NEW")) { // Change SR status to INPROG sr.changeStatus("INPROG", MXServer.getMXServer().getDate(), null); } } // Check if this workorder is COMP status if (mbo.getString("STATUS") == 'COMP') { // Find the related SR if any var sr = mbo.getMboSet("ORIGTICKET").getMbo(0); // If SR is in INPROG status if (sr != null && sr.getString("STATUS") == "INPROG") { // Get all workorders var woSet = sr.getMboSet("WORKORDER"); // Get a list of records not in CAN, COMP, CLOSE woSet.setQbe("STATUS", "WAPPR,APPR,WPCOND,INPRG,WMATL,WSCH"); woSet.setQbe("WORKORDERID", "!=" + mbo.getUniqueIDValue()); woSet.reset(); // If list is empty, it means all the workorders are 'completed', then set SR to RESOLVED if (woSet.isEmpty()) sr.changeStatus("RESOLVED", MXServer.getMXServer().getDate(), null); } }
Let’s analyse this code. You can see we added another section to check when the work order status is COMP, then find the related SR. If the SR is currently INPROG, then check to see if there is a list of workorders associated with it. Then check if all the work orders are completed and if so, set the SR to RESOLVED.
Step 6:
Now you can test this and now the status should be changed to RESOLVED when all work orders are completed.
There are somethings to consider with this. In this script, we are checking if the status is COMP so when the last work order to be completed is changed, it will change the status of the SR. But if the last work order status is changed to CAN or CLOSE, then the last section of the script won’t execute. This is something to consider and you can simply resolve that by added another condition to the last “if” statement.
UPDATE:
Here is an updated code based on the comments below… Thanks guys!
importClass(Packages.psdi.server.MXServer); importClass(Packages.psdi.util.MXException); //Check if this workorder is INPRG status if (mbo.getString("STATUS") == 'INPRG') { //Find the related SR if any var sr = mbo.getMboSet("ORIGTICKET").getMbo(0); //If SR is in QUEUED or NEW status if (sr != null && (sr.getString("STATUS") == "QUEUED" || sr.getString("STATUS") == "NEW")) { //Change SR status to INPROG sr.changeStatus("INPROG", MXServer.getMXServer().getDate(), null); } } //Check if this workorder is COMP status if (mbo.getString("STATUS") == 'COMP') { //Find the related SR if any var sr = mbo.getMboSet("ORIGTICKET").getMbo(0); //If SR is in INPROG status if (sr != null && sr.getString("STATUS") == "INPROG") { //Get all workorders var woSet = sr.getMboSet("WORKORDER"); //If more more than one WO exists for an sr if (woSet.count() != 1) { //Get a list of records not in CAN, COMP, CLOSE woSet.setQbe("STATUS", "DRAFT,WAPPR,APPR,WPCOND,INPRG,WMATL,WSCH"); woSet.setQbe("WORKORDERID", "!=" + mbo.getUniqueIDValue()); woSet.reset(); //If list is empty, it means all the workorders are 'completed', then set SR to RESOLVED if (woSet.isEmpty()) { sr.changeStatus("RESOLVED", MXServer.getMXServer().getDate(), null); } } } } //Check if this workorder is CLOSE status if (mbo.getString("STATUS") == 'CLOSE') { //Find the related SR if any var sr = mbo.getMboSet("ORIGTICKET").getMbo(0); //If SR is in RESOLVED status if (sr != null && sr.getString("STATUS") == "RESOLVED") { //Get all workorders var woSet = sr.getMboSet("WORKORDER"); //If more more than one WO exists for an sr if (woSet.count() != 1) { //Get all workorders var woSet = sr.getMboSet("WORKORDER"); //Get a list of records not in CAN, CLOSE woSet.setQbe("STATUS", "DRAFT,WAPPR,APPR,WPCOND,INPRG,WMATL,WSCH,COMP"); woSet.setQbe("WORKORDERID", "!=" + mbo.getUniqueIDValue()); woSet.reset(); //If list is empty, it means all the workorders are 'completed', then set SR to CLOSED if (woSet.isEmpty()) { sr.changeStatus("CLOSED", MXServer.getMXServer().getDate(), null); } } } }