Announcement

Collapse
No announcement yet.

Script Assistance: Pseudocode for Status Transition Validation

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Script Assistance: Pseudocode for Status Transition Validation

    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:

    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'.")
    ​is this possible?

  • #2
    Perhaps with the array formula:


    you could remove options by condition. Though I did not test it.

    Comment


    • #3
      Originally posted by arrestthepresident View Post
      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:

      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'.")
      ​is this possible?
      I think you could achieve this using dynamic logic (Conditional options) you can instruct the field to show only certain values when certain value is chosen e.g when field is Draft then show only draft and active status. see attached screenshots.

      I hope this helps
      Cheers
      Attached Files
      Rabii
      Web Dev

      Comment


      • #4
        Originally posted by shalmaxb View Post
        Perhaps with the array formula:


        you could remove options by condition. Though I did not test it.
        Problem is that i dont know how to access the old value at the update to compare it. i maybe need to have a temporary field for that.

        Comment


        • #5
          Originally posted by rabii View Post

          I think you could achieve this using dynamic logic (Conditional options) you can instruct the field to show only certain values when certain value is chosen e.g when field is Draft then show only draft and active status. see attached screenshots.

          I hope this helps
          Cheers
          thanks mate! but as i said, i already did this and this only sets the view in the front-end but you can always update the status in the wrong order via mass-update, api or kanban.

          ...

          but thank you!

          Comment


          • #6
            Hi,

            Didn't know that, probably a hook would be perfect for this or maybe Error save handler. Only issues is that mass update you need to create a custom logic a custom mass update action for your entity.

            Rabii
            Web Dev

            Comment


            • #7
              I have the solution. So i used an API Before Save Script and did this:

              HTML Code:
              // Get the new status
              $newStatus = entity\attribute("status"); // Assuming "status" is the field name for the new status
              
              // Get the previous status from the attributeFetched
              $oldStatus = entity\attributeFetched("status");
              
              // Define the allowed status order
              $allowedStatusOrder = list();
              $allowedStatusOrder = array\push($allowedStatusOrder, "Draft");
              $allowedStatusOrder = array\push($allowedStatusOrder, "Active");
              $allowedStatusOrder = array\push($allowedStatusOrder, "Running Out");
              $allowedStatusOrder = array\push($allowedStatusOrder, "Inactive");
              
              // Check if the new status is in the allowed order
              if (!array\includes($allowedStatusOrder, $newStatus)) {
              recordService\throwBadRequest("Invalid status change. New status is not allowed.");
              }
              
              // Check if the status change is in the allowed direction
              if (array\indexOf($allowedStatusOrder, $newStatus) < array\indexOf($allowedStatusOrder, $oldStatus)) {
              recordService\throwBadRequest("Invalid status change direction. Status can only be updated in one direction.");
              }​
              ...and this works perfect. Thankll you all though. :-)

              Comment

              Working...
              X