Trigger age to update on birthday

I’m capturing the User’s DOB on signup though am converting this into an age to help with search speed. We can build a scheduled workflow to update the age once a year but I was wondering whether anyone has figured out a way to use a backend trigger to do something like this? i.e.

When User’s DOB’s day&month are equal to current date’s day&month → Age+1

Any advice appreciated

Hey,
Database triggers works only when some data changed, so regular workflows that will “re-count age”(safer option then + 1) is the only way to go for you.

2 Likes

As you yourself mentioned, in my opinion, this seems to be the best solution: You can also, as soon as the birthdate is saved, schedule a new workflow for the day of the birthday. Then, increment the age field by +1, and immediately schedule an action for the following year. This way, you create a loop that updates the field every year.

Thanks Martijn, that’s what I was originally thinking too. Just curious if there’s a way to do it without having lots of scheduled workflows

It is also possible to execute this action when a user logs in, of course. As soon as they are logged in, you will then have up-to-date data. This way, you avoid using unnecessary workloads. However, it also depends somewhat on how your app is structured.

1 Like

How is checking/changing the date of birth on every login less workload than doing it once a year?

Depends on the app and user use of course. The math makes the choice :blush:

I think scheduling a backend workflow to run in 1 year and add 1 to the age is your better option.

BUT I also assume that there is no way for the user to CHANGE their DOB, right?

IF they can, then you have a little can of worms you need to manage. Otherwise, the age increment will be inaccurate.

6-year-old Timmy walked into a pub, and asked for a beer. The publican asked for ID, and then agreed. Which day of the year is his birthday? :grin:

I always try to leave computed values out of the database and have them calculated when needed. Of course, if age is stored it makes charts easier to do.

2 Likes

What do you mean by help with search speed? How are you doing it now?

We’re not calculating the age at the moment, we’re using their DOB when filtering by age.

They were able to change their age, but with this update I’m thinking of disabling that (unless there’s a good reason not to…).

If you’re using the DOB as a search constraint then it won’t make any speed difference since it’s all done in Bubble’s backend.

If you are doing the age filtering using :filtered then there will be a huge difference depending on the returned data size and shifting your age filtering to the search constraint will then make a difference.

That’s why you are re-calculating age each time dob changes instead of incrementing it by 1. No headache.

OK, that would work.

BUT, what happens if I log in Thursday (which will re-calculate my age) and stay logged in until Sunday. Friday is my birthday. That means I have all day Friday, Saturday and Sunday where the system has my incorrect age because I never logged back out and logged back in to trigger the re-calculation of my age.

I assume you mean “change their age” to mean “change their DOB”.

I think you need to let them change their DOB. Why? Well, I know way too many people who will give a fake DOB just to get into the app to try it, thinking they don’t want to give the app too much personal info yet. Then when they feel comfortable with the app and they know an accurate DOB means something important (like free coffee on your birthday) they’ll update the DOB to the correct value.

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