Recursive Workflow for Referral Networks

Hello Bubble Community,

I’m working on a project that involves building a referral network system within Bubble.io, where each user can refer new users, creating a chain of referrals. I’m looking for advice on how to implement a recursive workflow or logic that allows for the following functionality:

  1. Network Structure: Each user (referred to as a “leader”) can refer new users. These new users, in turn, can become leaders of their own referral chains. This creates a hierarchical structure of referrals.
  2. Counting Referrals: I need a mechanism where each leader counts not only their direct referrals but also indirect referrals (i.e., referrals made by their referrals, and so on down the chain). For example, if “Leader 1” refers “Person A”, and then “Person A” refers “Person B” and “Person C”, “Leader 1” should have a total referral count that includes “Person A”, “Person B”, and “Person C”.
  3. Recursive Update: The challenge is to update the referral counts (or lists) for each leader up the chain every time a new user is added, dynamically adjusting as the network grows.

Does anyone have experience with implementing such recursive or cascading updates in Bubble.io? Any insights, examples, or resources on managing hierarchical data structures and recursive logic within the platform would be greatly appreciated.

Thank you in advance for your help!

Well, the task is not that simple for bubble engine. It’s definitely not possible in a way that Oracle or MSSQL allows you to do, but you can try caching total ref count at each level.
Each time a new ref is added, you would need to recursively recalculate total ref count using scheduled API (preferred) or database triggers (I faced a problem with it once, but in your car it shouldn’t be critical), traversing up the hierarchy and sorting total count in a dedicated field.
There’s no way to query or count the whole hierarchy at once as you would do with Oracle.