Preventing fields from Editing Once Record is in Workflow
I like this problem because it involves a few components to make it work.
The business problem that is being solved here is if the user would like a field or fields to not be editable once the work order, in this case, enters workflow. This example focuses on the WORKTYPE field, but this script could easily be expanded to include other fields.
The components you will need for a successful script are:
- A list of fields that will be made read only once record is in workflow.
- A list of viable statuses for the workflow to know when the records should be evaluated.
- A SQL query to determine if record is on workflow.
- Setting fields to be read only if evaluation is true.
Creating a list of fields to be made read only
You can create a list of fields to be read only by using an array. Here I only have one field but using an array gives you the flexibility to quickly add additional fields.
#List of fields that will be read only readOnlyFields = ['WORKTYPE'] #Example with additional fields readOnlyFields = ['WORKTYPE', 'DESCRIPTION']
Obtain list of viable workflow statuses
In this scenario we care about records that are actively in workflow. So we get a list of statuses using the workflow status domain.
# Get a list of valid workflow assignment statuses statusList = mbo.getTranslator().toExternalList("WFASGNSTATUS", "ACTIVE")
Query to determine if record Is actively in workflow
Construct a query to determine if the record is in workflow.
# Construct a where clause to find the existing workflow assignment # for this work order. Ensure that one exists before proceeding. whereClause = SqlFormat(mbo.getUserInfo(), "ownertable = :1 and ownerid = " + woId + " and assignstatus in (" + statusList + ")") whereClause.setObject(1, "WFASSIGNMENT", "OWNERTABLE", "WORKORDER")
Setting field(s) flag to read only
If it is determined that the record is in workflow then we set the fields listed in the array, readOnlyFields to read only.
if not wfAssignmentSet.isEmpty(): logger.error("WF Assignments assignments for this work order") # Make fields read only if the work order is in workflow # There will be more read only fields. Fields can also be made # required using the same principle. mbo.setFieldFlag(readOnlyFields, MboConstants.READONLY, True)
Steps to create automation script
Create a new automation script with Object Launch Point. We called it WOPREVEDIT in this example.
Create a launch point with Initialize Value selected.
Here is the full automation script.
#Script: WORKORDER.INIT #Make fields read only if record is in workflow #Launch Point: WORKORDER.INIT #Launch Point Type: OBJECT #Launch Point Object: WORKORDER #Language: Python #Author: A3J Group from psdi.mbo import SqlFormat from psdi.server import MXServer from psdi.util.logging import MXLoggerFactory from psdi.mbo import MboConstants logger = MXLoggerFactory.getLogger("maximo.script.woinit") # Get the associated work order wo = mbo.getString("WONUM") woId = mbo.getLong("WORKORDERID") #woId = str(woId) #woId = woId.replace(',','') #Logging logger.info("Value for woId is " + str(woId)) #List of fields that will be read only. Add additional fields with a comma. readOnlyFields = ['WORKTYPE'] # Get a list of valid workflow assignment statuses statusList = mbo.getTranslator().toExternalList("WFASGNSTATUS", "ACTIVE") # Construct a where clause to find the existing workflow assignment # for this work order. Ensure that one exists before proceeding. whereClause = SqlFormat(mbo.getUserInfo(), "ownertable = :1 and ownerid = " + str(woId) + " and assignstatus in (" + statusList + ")") whereClause.setObject(1, "WFASSIGNMENT", "OWNERTABLE", "WORKORDER") # Get record set and filter it by the where clause wfAssignmentSet = mbo.getMboSet("$WOAPPR", "WFASSIGNMENT", whereClause.format()) wfAssignmentSet.reset() if not wfAssignmentSet.isEmpty(): logger.info("Found WF Assignments for this WO – settings fields readonly") # Make fields read only if the work order is in workflow # There will be more read only fields. Fields can also be made # required using the same principle. mbo.setFieldFlag(readOnlyFields, MboConstants.READONLY, True) if mbo.getThisMboSet().getParentApp() == "WOTRACK": woOwner = mbo.getOwner()
One of our missions A3J Group is to share knowledge whenever we can. We hope this helps you in your day!