Back end workflow

Hello,

Back end database set up 


I have a database that is storing meals. I would like to create a back-end workflow that tallies up some of that data to be shown in a repeating group.

so the database is meals, and contains multiple meals with protein, carbs fat etc for that day.The one I want to create is a day summary with the total protein, total fat, etc for that day to be displayed on the cards in the RG.

I am having so much trouble getting the data to populate in the database. I am definitely missing something.

My user_day_summary is getting called, and it makes a database entry but only with 1/2 of the data. can you spot what I am missing?


Looking at your setup, I can see the workflow is creating Day Summary entries but only populating some fields. The issue is in how your workflow is structured - you’ve got Step 1 creating the Day Summary only when it doesn’t exist, and Step 2 updating it only when it does exist, but Step 3 (calculate percentages) is trying to reference data that might not be fully set yet.

The problem is that when Step 1 creates a new Day Summary, it’s not populating all the fields you’re calculating in the “calculate percentages” action. Looking at Image 1, your calculate percentages action is trying to set protein_pct, fat_pct, and carb_pct, but it’s doing calculations based on the Day Summary’s totals which haven’t been set yet in Step 1.

Here’s what you need to fix - in Step 1 where you create the new Day Summary, you’re only setting User and date_key. You need to also set all the total fields there like calories_total, protein_g_total, carb_g_total, fat_g_total, etc. Right now those calculations are happening in Step 2 which only runs when the Day Summary already exists, so new entries are being created incomplete.

Move all those field calculations from Step 2 into Step 1 so when a new Day Summary is created, it gets populated with all the totals immediately. Then Step 3 can calculate the percentages based on those totals. That way whether it’s a new entry or an update, all the data gets set properly.

1 Like

This is a typical bubble race condition. All actions in a workflow will execute asynchronously, but they will not wait for any previous steps to complete. This is causing step 2/3 to update something that may not exist yet. The exception is if action refers to a previous action in the workflow. E.g. Step 2 is to update Step 1’s thing.

You should add an extra parameter to the workflow - current_days_summary. When executing the workflow, Do a search for todays day summary for the relevant user.

Step 1 - Create new day summary - only if the workflows current_days_summary is empty.

Step 2 - Thing to change is (current_days_summary merged with step 1’s new day summary):first item. If you found a Summary when executing the workflow, it will update this. If not, it will update the newly created thing in step 1.

Execute step 2 as you have now.

Step 3 - Update thing > Step 2’s Day’s Summary.

Execute step 3 as you have now.

1 Like

Thank you for taking the time to look and make a suggestion. That makes total sense. I changed, it, but am still getting incomplete data pulling over. Its getting some, but i am still missing something. I added the targets to populate as you said (if I understood right) but it still pulled over only partial. I am sure I missed a step.

Thank you, It does possibly seem like a timing issue, that makes sense. Am I understanding correctly, I have the only when statements with the search constraints, I am not sure what you meant in the first part exactly, then I put this as result of step 2 but got similar results

You’ll still be getting race conditions between step 1 and step 2. And step 3 will only ever happen if step 2 does. Don’t you want step 3 to happen regardless if you need to create a day summary or just update it?

You need to reread my first suggestion.

I can see what’s happening now. You correctly moved the total calculations into Step 1, which is why fields like protein_g_total, volume_total, and water_total are populating. But the percentage fields and some other totals are still empty because Step 3 is trying to reference Step 2’s result, which doesn’t exist when a new Day Summary is created.

Step 3 “calculate percentages” is set to modify “Result of step 2” but Step 2 only runs when updating an existing Day Summary, not when creating a new one. So when Step 1 creates a brand new entry, Step 3 has nothing to reference and the percentages don’t get calculated.

The fix is to change Step 3’s “Thing to change” from “Result of step 2” to “Search for Day Summarys:first item” with the same search constraints you have. This way Step 3 will find and update whichever Day Summary was just created or modified, regardless of whether it came from Step 1 or Step 2. Alternatively, you could add the percentage calculations directly into both Step 1 and Step 2 so they always get set at the time of creation or update.

@iamcharlesac Sorry but this is wrong. Changing step 3 to change “Search for Day Summarys:first item” is what he had initially. This creates the race condition.

If you purely do a search for:first item in every step, they will execute at the exact same time. Step 3 will not wait for step 2 to finish, and step 2 will not wait for step 1 to finish.

If you’re changing the same thing in multiple steps in a workflow, you must always try to reference them without a search. It’s also heavier in terms of WU to search every step. After all, why would you search for the thing again, when you have it in the previous step? Step 3 should be to change (Step 1’s thing:plus item Step 2’s thing):first item.

@raenafay if you follow the below, i promise it will work as you think it should.

You should add an extra parameter to the workflow - current_days_summary. Type: day_summary. When executing the workflow, Do a search for todays day summary for the relevant user. If it exists, it will not be empty, if it doesnt exist, it will be empty. This is what we want.

Step 1 - Create new day summary - only if the workflows current_days_summary is empty. Do not input any values in this step, purely create the thing if it doesn’t exist.

Step 2 - Thing to change is (workflows’s current_days_summary:converted to list:plus item step 1’s new day summary):first item. If you found a Day Summary when executing the workflow, it will update this. If not, it will update the newly created thing in step 1.

Execute step 2 as you had originally, updating values in this step only.

Step 3 - Update thing > Step 2’s Day’s Summary.

Execute step 3 as you have now, updating with the calculations. You should also try to reference the information just saved in step 2. Can you not calculate percentages using the previous steps updates? This will remove 12 searches from this step alone. Huge amount of WU thats not needed.

E.g. Step 3 protein_pct= step 2’s protein_g_total/(step 2’s protein_g_total+step 2’s fat_g_total+step 2’s carb_g_total)

Thank you so much for your patience. I was trying to listen, but clearly not getting it. I hope I did now. I am clearly a noob at this, but slowly getting better.

I added the key current day summary.. (do I need the other keys on there?)

step 1 no fields set here right? just creating current_day_summary

step 2 fields all get filled on here, is that the right conditional now?

step 3 referencing step 2’s data

I have still messed something (or a few things) up as it populates a day summary, but it is blank.

Thats all good, we all start somewhere. Only a few minor things to change.

Workflow - Parameter: current_day_summary. This should be of type: Day Summary.

  • Whenever you execute the workflow, in this parameter you’ll need to put - (Do a search for: Day Summary, with filters: User = User, Day = Day):first item.
  • This means, whenever the workflow begins, the ‘workflow’ will know whether the Day Summary exists or not. If it doesnt exist, step 1 will occur, and Step 2 will update the newly created thing. If Day Summary does exist when the workflow starts, Step 1 will not occur, and Step 2 will update the Day Summary found when executing the workflow.

Workflow - Step 2:

  • This should be (Workflow’s Parameter’s current_day_summary:converted to list:plus item:Step 1’s thing):first item.
  • The workflow parameters current_day_thing wouldn’t have shown up as you have it currently as Type: Text.
  • This should have no conditional on the worfklow
  • Make sure to input User + Date_Key, so when searched for again using these filters, it will show up.
1 Like

It worked!! Thank you SO much again for your patience.

-Raena

1 Like