Script Assistance: Pseudocode for Status Transition Validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • arrestthepresident
    Member
    • Jun 2021
    • 43

    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?
  • shalmaxb
    Senior Member
    • Mar 2015
    • 1606

    #2
    Perhaps with the array formula:


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

    Comment

    • rabii
      Active Community Member
      • Jun 2016
      • 1250

      #3
      Originally posted by arrestthepresident
      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

      • arrestthepresident
        Member
        • Jun 2021
        • 43

        #4
        Originally posted by shalmaxb
        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

        • arrestthepresident
          Member
          • Jun 2021
          • 43

          #5
          Originally posted by rabii

          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

          • rabii
            Active Community Member
            • Jun 2016
            • 1250

            #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

            • arrestthepresident
              Member
              • Jun 2021
              • 43

              #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...