Backend workflow issue - Update User

Hello, I would like to extract 10 cards chosen randomly from an CARDS database and for each of them create an entry in the NFT-MetaData database, then assign the Current User the corresponding uniqueID by modifying the NFT_owned field (List of CARDS) in the database. USERS data.

I followed the following method:

Step 1: Create a Workflow API to Create NFT-Metadata

Create a Workflow API:
    In the "Backend Workflows" tab of Bubble.io, create a new Workflow API called "create_nft_metadata".
    Configure this Workflow API to accept the necessary parameters:
        card_id (of type Card)
        user_id (of type User)

Create an Entry in the NFT-MetaData Table:
    Add a “Create a new thing” action in the Workflow API to create an entry in the NFT-MetaData table.
    Configure the fields as follows:
        CardID: card_id (Workflow API parameter)
        Owner_ID: user_id (Workflow API parameter)

Update User:
    Add a "Make changes to a thing" action to update the user's NFT_owned field.
    Configure the action to add the new NFT-MetaData entry to the user's NFT_owned list.

Step 2: Call the Workflow API for Each Selected Image

Program a Workflow on a List:
    Return to the registration workflow.
    Add a “Schedule API Workflow on a list” action.
    Configure it as follows:
        Type of things: Cards
        List to run on: The 10 random images selected
        API Workflow: create_nft_metadata
        card_id: This Card's unique ID
        user_id: Current User's unique ID

The 10 NFTs are correctly created, but for the Update user step the NFT_owned list only contains 5, or even 6 elements. Does anyone have a solution?

Thanks in advance

This is a race condition issue: you are trying to update the same list from different workflows at the same time. Bubble does not handle this very well and the changes essentially overwrite eachother causing some entries to not be registered on the list.

The short answer - to fix your issue quickly -
On the Schedule API workflow on a list set the ‘delay’ to 1-2 seconds between every iteration. This will make sure that the scheduled workflows don’t run into race conditions and overwrite each other. This will mean that it might take a while for all NFTs to get tied to the User, but it will be much more reliable.

The long answer - in case you are interested in development best practices -
Using a list field on the User to save the list of owned NFTs is not the best way to go about things - for quite a few reasons. List fields can be great when you are sure the list wont need to contain too many elements, and with owned NFTs thats not the case - but there are many other things to consider.
Instead, it would be better to rely solely on the User owner_id field on the NFT table.
There is lots of discussion on the forum regarding this - if you are interested you there is lots to read.

Great, problem solved. Thank you very much for your help. As the NFT database is likely to have around 1 million entries, I thought it would be faster for future processing to store purchased NFTs in the USERS Table (since buyers will only have around a hundred NFT). I will read the forum discussions on this topic. Thanks again and have a nice day :grinning:.