I am trying to stop an action from running based on a value within a table

Get Answers

I have a view with two buttons that initiate actions. I want to stop the action from running if that action has already been triggered.

The actions in the image below set a field within the table to yes or no. So if the Received action button is pressed then it sets the field to “YES”. if the Required button is pressed it sets the field to “NO”.

Is there a way either in javascript or MAKE.com (or even in knack which would be better) to still have the two buttons show on screen and when either of the buttons is pressed verify the database field to see if that button has already been pressed. If it has then halt the action from running if it has not then run the action?

I am no expert in javascript or Make so I don’t know how to get in front of the action being run.

Hopefully, I was clear on what I needed.

You could add a date/time field that captures when the button was pressed. Each of your two fields would need a separate date/time field that could be populated when the button is pressed using an action rule.

I’m not sure if your two buttons are action links or forms that update the record.

Either way, you could then stop the task, or the Make scenario, from running based on the Yes/No value and the date/time field not being blank. Essentially, you’re adding another criterion that only allows it to run when both the Yes and No date stamps are present.

It’s difficult to fully understand your setup and probably equally challenging to understand my reply. :wink:

I’m happy to take a look if you want to connect. I charge in 30-minute increments.

Thanks Carl for the advice but I am actually trying to stop the Action itself from runing not a task or form submission after that.

I have been able to get some javascript code to run during the on “click” event but I don’t know how to terminate an on click even before it processes the Action. I don’t even know if that is possible. It sounds like it is but i have not yet figured out the javascript for that yet.

Do you have much experiance with Javascript and on-click events???

I don’t, but my friends and fellow Knack colleagues @CSWinnall or @StephenChapman are coders so may jump in here.

Hi @Mikeacce,

For a code-based solution, here’s how you can stop the action, and then conditionally perform it.

action-link-interception

$(document).on('knack-view-render.any', function (event, view, data) {
  const $actionLinks = $('#' + view.key + ' a.kn-action-link span')
  
  $actionLinks.each(function () {
    $(this).on('click', async function (event) {
      // Stop the action
      event.preventDefault()
      event.stopPropagation()

      // Check the database
      const originalActionLinkText = $(this).text()
      $(this).text('Checking...').closest('a').addClass('is-disabled')
      
      const result = await checkDatabase()

      if (result) {
        // Manually perform the action
        event.target.parentElement.dispatchEvent(new Event(event.type, { bubbles: true }))
      } else {
        $(this).text(originalActionLinkText).closest('a').removeClass('is-disabled')
        alert('This action has already been completed')
      }
    })
  })

  // Simulate waiting for a database response
  async function checkDatabase() {
    return new Promise(resolve => setTimeout(resolve, 3000))
  }
})

For a no-code solution: Instead of an action link, use a page link to a modal that contains the action link. Then you can use a Page Rule to hide the link based on your conditions, or show a message such as “this action has already been performed”.