In my instance, I have a custom-entity with field "status." The field is a dropdown with the following values: "Draft," "Active," "Running Out," and "Inactive." I want to ensure that status transitions follow a specific order, namely from "Draft" to "Active" to "Running Out" to "Inactive."
I already defined Condition options for the frontend to not show the prior options, but it is still possible to update to a prior status, which i dont want to be.
To achieve this, I intended to use a Before-Save script. The script should check the old and new status values and prevent the save operation if the transition is not in the correct order.
Pseudocode Solution
Here's a pseudocode representation of the solution I've devised:
βis this possible?
β
I already defined Condition options for the frontend to not show the prior options, but it is still possible to update to a prior status, which i dont want to be.
To achieve this, I intended to use a Before-Save script. The script should check the old and new status values and prevent the save operation if the transition is not in the correct order.
Pseudocode Solution
Here's a pseudocode representation of the solution I've devised:
HTML Code:
// Get the old and new statuses
oldStatus = entity.get("oldStatus")
newStatus = entity.get("status")
// Define the allowed status order
allowedStatusOrder = ["Draft", "Active", "Running Out", "Inactive"]
// Check if the transition is allowed
if oldStatus is not null and newStatus is not null:
oldStatusIndex = allowedStatusOrder.indexOf(oldStatus)
newStatusIndex = allowedStatusOrder.indexOf(newStatus)
// Check if the new status is in the correct order
if newStatusIndex > oldStatusIndex:
// The transition is allowed, save the changes
return
else:
// The transition is not allowed, display an error message
throwError("Invalid status transition. You can only transition in the order of 'Draft' to 'Active' to 'Running Out' to 'Inactive'.")
β

Comment