How to select the closest Data Type record to a target value?

Hi everyone,

I’m trying to build a simple recommendation feature and I’m stuck on the formula.

What I have:

  • Current health points = 50

  • Target points = 120

  • A Data Type called ConnectionType with a number field Base Points. It currently has these records:

    • Message → 30

    • Call → 60

    • In-Person → 120

    • Visit → 180

Goal:
I want to automatically select one record from the ConnectionType Data Type that, when added to the current points (50), gets as close as possible to the target (120).

In the example above, adding “Call” (60) gives 110 — which is the closest to 120.

Question:
What is the cleanest way in Bubble to do this?

I need a formula (or workflow) that:

  1. Looks at all ConnectionType records

  2. Calculates (current health + Base Points) for each

  3. Finds the one with the smallest difference to the target (120)

  4. Returns that specific ConnectionType record

Any help or example would be greatly appreciated!
Thank you!

1 Like

Probably a backend workflow with a series of custom workflows with return data. I’d imagine you can have a series of custom workflows each with parameters of the ConnectionType which will have actions to add the ConnectionType number to the current healthpoints and return the differences between the added values and the target…this will then allow you to see the difference between all, to find which is ‘closest’…then you’d have to decide on logic to use to select which is closer when two are each 10 to figure which ConnectionType is greater than the other if your point system is such that it is possible.