Changing two lists of things, if they meet reciprocal conditions

I’m not able to figure out how to do this:

  • I have a table of inventories.
  • I have a table cities.
  • I have a table of people.

There’s many people per city, and many inventories per city. Here we limit to one type of item (box), so effectively one inventory.

Goal: When triggered, I want to removed 1 item from the inventory per people related to that inventory, only if there’s enough for everyone.

Adding a diagram to explain more easily:

So I did:

Things to change:
Make change to a list of inventories > search(inventories, where item is box):

Variable to change:
Quantity = This inventory’s quantity - search(population, where population’s city is this inventory’s city):count.

Condition for change:
…well that’s where I’m stuck. I cannot reference the “this inventory” as above, if I could I would do something like, only when this inventory’s quantity >= search(population, where population’s city is this inventory’s city):count.

Still I have only resolved a part of that problem, because then I need to move the quantity to each people as well.

I tried so many things: comparing two lists of inventories, comparing a list of inventory against a list of people, i even tried to bring a list of cities and work from there, I tried using :intersect, :filter, :group_by etc. I’m just out of options. This stuff is really intuitive. :sob:

I think in pseudo-code I need to change a list of inventories I = {ia … in}, where I is findAll(inventory).filtered(quantity ≥ findAll(people).filtered(city = i.city).count())

I’m trying to understand the Bubblogic here :thinking:

How do I achieve my goal here? Help.

1 Like

Are you trying to do this on one city at a time or run a workflow that loops through each city making these changes?

Hey Eli,

I’m trying to loop over each city.

However, even if I have to do one at a time, I am unable to do simple stuff like subtract the inventory’s quantity of as many people are in that inventory’s city [or pseudo code if any use to translate my intent: “inventory.quantity = inventory.quantity - search(persons).filter(city = inventory.city).count()”]

Tell me if I’m wrong but my understanding of what you want to do is this. Say the city is Cleveland.

If Cleveland’s count of boxes in inventory is 10 and the count of people is 8 then you want to change the box inventory like this (10-8=2) so Cleveland now shows 2 boxes in stock and each of those 8 people have one box added to their count.

If Cleveland’s count of boxes is 4 and the count of people is 9, do nothing.

This can be done but you will need two API Workflows:

  1. update_box_qty
  2. update_person_qty

Here goes.

You need to Schedule an API Workflow on a list and pass all the cities through. Set it up like this.

The search for boxes and persons both have constraints of City = This City

Then in your API Workflows tab, set up the update_box_qty endpoint with these Key’s

  • City = City thing
  • Boxes = number
  • Persons = number

Since we are passing these count of boxes and persons with each City, we can add a simple condition that looks like this
Boxes >= Persons

This makes sure that the workflow only continues on Cities that meet the condition of having the same or more boxes than people.

In this workflow, you have two steps.

Step one will make a change to Boxes using the Key values we passed through and change the QTY to Boxes minus Persons.

Step two will Schedule API Workflow on a list of Persons and should be set up like this.

The second API endpoint will be set up with a single key Person = Person thing and no conditions

With a single step in the workflow


Your data structure may be slightly different and you may want to look at recursively looping through your cities or people depending on how large your lists are but the basic workflow structure will work.

1 Like

Wow! At first I was bummed I didn’t get any replies on this thread but I just got served! This is a very detailed and complete answer Eli. Thank you so much for your time.

I haven’t looked at API workflows before and didn’t realize they were required for this purpose. So I’ll do a bit of API workflow 101 first.

I think I just realize that standard workflows are running on client side. This means using API workflows run on server side?

Glad to help :slight_smile:

That is correct. Learning to incorporate API workflows adds a ton of power to your app as well as enabling you to offload workflows from the client side to speed up the UX.

Good luck!

1 Like

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