John and the apples

John (data type User) has 1 apple (number data field) but he needs to complete 11 apples
(so he is missing 10 apples).

Store A (data type Store) has 2 apples.

Store B has 5 apples.

Store C has 8 apples.

I need to build a workflow where after clicking the “Take apples from stores” button, John ends up with 11 apples in total (taken from any of the stores in any order).

How can I build this workflow using dynamic numbers so that I can run different scenarios with different number of apples?

Quite a few ways to do this. Without knowing much about your use-case, which i doubt has much to do with apples, i feel like the best solution i can point towards is the simplest solution

A simple recursive workflow would do the trick.
The button would start a backend workflow which takes John’s missing apples as an input, and has the following steps;

  1. Make change to Store;
    Data source= Do a search for Stores with constraint Apples>0,
    Change Apples = This store’s apples - 1

  2. Make change to Current User (John)
    Change, Apples = This user’s apples + 1

  3. Schedule API Workflow (This Backend workflow @ current time+a second or two, with input = Missing Apples-1 ) but only on the condition that Missing Apples > 1

This recursive workflow will reliably finish the task, but it takes quite a few seconds to finish.

With a few well-placed custom states in the front-end you can trick your users into thinking that the task finished immediately. However this becomes a terrible way of doing things if you are working with hundreds or thousands of your supposed apples.
You can make the backend workflow a bit smarter and work with more than 1 apple per recursion if you are dealing with many apples.
Also gotta add some extra constraints if you aren’t sure that the stores will always have enough apples to supply to John.

Hope this helps.

1 Like

Thanks! I’ll give this a try

This topic was automatically closed after 70 days. New replies are no longer allowed.