In the last post, we talked about action script. Today, let's focus on the other two types of scripts: condition and validation scripts.
Unlike action scripts, both condition and validation scripts only serve workflow transitions. The workflow transition properties is the only place to setup the references to them. [Administration -> Workspace Manager -> workspace –> Workflow Editor -> double click transition]
Condition scripts
- Condition scripts are read-only scripts. The changes made in code won't be saved.
- A condition script needs to return a Boolean value (true or false) using predefined function returnValue().
- The returned value is used to tell whether an outgoing workflow transition from current state is available to the user to perform or not, but it is not the only prerequisite to make a transition available. The user must also have the workflow permission of the transition.
- Condition scripts are triggered whenever PLM needs to determine if a workflow transition is available to a user.
- Each transition can only have one condition script.
- Each condition script can be referred by multiple transitions from different workspaces.
Condition script examples
Example 1: Transition is only available to the owner of item.
// get current item's owner.
var owner = item.master.owner;
// if the owner's id is equal to the current user's id, then return true; otherwise false.
returnValue(owner.id === userID ? true : false);
Example 2: Transition is only available to the users in "Admin Group"
// Security.inGroup(userId, groupName) is built-in function checking if certain user is in certain group.
var isCurrentUserInAdminGroup = Security.inGroup(userID, 'Admin Group');
// return the checking result
returnValue(isCurrentUserInAdminGroup);
Validation script
- Validation scripts are also read-only. The changes made in code won't be saved.
- A validation script is used to validate certain prerequisites before allowing transitioning to the next workflow state.
- A validation script should return an array of failing messages using predefined function returnValue().
- The transitioning will be allowed if an empty array is received; otherwise, the user won't be allowed to perform the transitioning and the returned messages will be shown to the user.
- Each transition can only have one validation script.
- Each validation script can be referred by multiple transitions from different workspaces.
Validation script example
Example: Item's "name" field must not be empty, and item must have an attachment at least.
var messages = [];
if (item.NAME === null) { // name field must not be empty
messages.push("Production's name is required to complete the transition");
}
if (item.attachments.length === 0) { // must have one attachment at least
messages.push("At least one attachment is required to complete the transition");
}
returnValue(messages);
Scripts in workflow transition lifecycle
Here is how all 3 script types are used in a workflow transition:
- Every condition script on the outgoing transitions of current workflow state runs to determine which transitions are available for the current user.
- User chooses an available transition to perform workflow action.
- The validation script for that transition runs to validate all the prerequisites.
- If all the prerequisites are met, the action script on that transition runs.
- The transition is completed.
-- Michal