How to code on trigger

Hi:

I started using Retool last week to build a mobile app for my company. I have all the screens into place and is filtering the data by screen, that works great and it was super quick. Where I have issues is the following.

They scan a label using the barcode component. After that the following needs to happen:

  1. Check the label exists
  2. Check the label belongs to the current "drop" they are doing
  3. Check if the label is part of a pallet

Once all of the above happen. I need to update the table where the label belongs too setting the fields unload=1, driver=currentdriver, delivered at=getDate(). If it belongs to a pallet it will do the same but lookup the pallet number and update the same information in that table.

I've gone through the documentation and tested a few things like creating a javascript query, but nothing. I do SQL development for a living so that's not an issue. Javascript I don't know because I've always worked in Visual Basic. But the logic I presented it exists now in our tablets and it works great. If someone could lead me into the right direction I'll appreciate it.

Andres

Hi @adominicci -- welcome to Retool!

It sounds like you are trying to trigger a series of queries once a barcode is scanned. If you have the queries already build to facilitate steps 1-3 that you listed the triggering process would look something like:

  1. User scans barcode -- the barcode component should have an event handler that you setup to trigger the next step (checking if the label exists).
  2. Check label exists query is triggered -- setup the success and failure handlers in this query to trigger the next step or to do something if the query fails... this should be how you trigger the follow-up queries as well.
  3. If everything has succeeded the way you want the last step should trigger your table update query.

Using event handlers is the most simple UI way to chain these events, but if you are going to be using any custom Javascript queries you would be triggering those in your code using yourQueryName.trigger() and using an async/await function to let the query complete its runtime before moving on.

Scanner Event Handler:

If there's maybe more to the issue you're having let us know with some screenshot examples of how you have the app setup (or an export of the App JSON for others to load and inspect)

1 Like

Thank you @pyrrho I'll give it a shot the way you have explained. :pray:

1 Like

@pyrrho where can I set this what you mentioned here

" 1. Check label exists query is triggered -- setup the success and failure handlers in this query to trigger the next step or to do something if the query fails... this should be how you trigger the follow-up queries as well."

Thank you.

These would be in the query setup:

I started looking around and had found them :slight_smile: . My concern is how many queries I'll end up calling. About 4-5 all together and if I go that route and I plan to use the offline mobile do I need to also load these queries before going offline?

Thanks

Andres

I haven't tested this process out specifically, but part of the query is the ability to cache the query results for a set period of time (Advanced tab of the query setup).

Depending on your use case (say, scanning barcodes in the field) maybe your app would need to run the queries while online but then still have access to the cached data later.

That would be a deal breaker. One of the issues we have is that a lot of times they won't have internet connection. :frowning:

If you're in an always offline situation at a certain point there really isn't a lot more to do, however, Retool does have the offline mode settings for mobile apps (docs: Configure offline mode | Retool Docs) so that you can build around times where connectivity is intermittent and let the user know when things have/haven't processed and are pending connectivity.