Help Needed: Editing/Updating Multiple Records with Xano Post API

Options
Hi everyone,

Objective: I want to be able to edit/update multiple records through the Xano Post API for my tables

Summary of issue: I understand that the issue is in the mapping a list [] to a singular field. My "Edit Record" function is not allowing these arguments/inputs to be passed and throwing an error. But I don't know how to fix this

Steps/Process I am following

Example table "Fruits"

Schema: id | created_at | name | price

Step 0: Created LIST Inputs as
•  item_id_list_input; integer []
•  item_name_list_input; text []
•  item_price_list_input; integer []

Step 1: Created a For Loop where the input item_id_list_input is passed;
• function is For Loop
• input of this step is item_id_list_input 
• output of this step is a variable called item_id_list

[Screen Shot 2022-06-06 at 15.29.35 PM.png]

Step 1a: Under the For Loop, created a step to "Get Records" from fruits WHERE field_name fruit_id is equal to item_id_list;
• function is Get Record
• input of this step is item_id_list
• output of this step is item_original


[Screen Shot 2022-06-06 at 15.29.46 PM.png]

Step 1b: Under the For Loop, created a step to "Edit Records" from fruits WHERE field_name fruit_id is equal to item_original.id
• function is Edit Record
• input of this step is item_original
• output of this step is item_updated
• mapped fruit_name to item_name_list_input; text []
• mapped fruit_price to item_price_list_input; integer []


[Screen Shot 2022-06-06 at 15.30.07 PM.png]
Error that I am getting:
• name - Text filter requires a scalar value
• price - Integer filter requires a scalar value


[Screen Shot 2022-06-06 at 15.33.09 PM.png]

Comments

  • Chris Coleman
    Chris Coleman Administrator

    ADMIN

    Options
    Hi, Umair. If you use the debugger, you can see exactly where your function stack is stopping and what data is being passed. This will usually help troubleshoot this issue. Typically when I've seen this, it's because the data is being passed as an array.
  • Umair Kamil
    Umair Kamil Member
    Options
    Hi Chris,

    Thank you for your reply. Yes I understand the issue, but could you please help/guide me on how to resolve it? I'm not sure of how I should split the array. Once again my objective is to build a post API that allows the end user to make bulk/batch updates. Such as selecting multiple rows on the front end and updating the columns.
  • Chris Coleman
    Chris Coleman Administrator

    ADMIN

    Options
     I took a look at your function stack, and it looks like you might have to essentially have stacked loops to update each record type. This is because your Edit Record function is still being passed an array to use to edit a single record. So, you have your original For loop that grabs the records one by one, and then under that you would have another, which could be For each item_name_list_input where the ID matches, update that record, and then build another one below for item_price_list_input. Does that make sense?
  • Umair Kamil
    Umair Kamil Member
    Options
     Yes that makes sense! But is there any better way, such as by splitting the array? I saw some array filters; the "first", "last", "pop" array filters but they didn't work for this use case.

    I'm wondering if there's any better way to bulk update multiple records? Like I'm worried that nesting For loops would not be the most efficient thing in terms of computational power
  • Umair Kamil
    Umair Kamil Member
    Options
     I also saw the tutorials for the Database Request series and couldn't find any video on "Bulk Edits" or "Multiple Record Edits"
  • Chris Coleman
    Chris Coleman Administrator

    ADMIN

    Options
     Nesting loops here is the way to go. Even if there was some manipulation you could do to 'split the array', you'd essentially be going through the records one by one either way, just like a loop is.
  • Umair Kamil
    Umair Kamil Member
    Options
      - I nested the loops, but all records in the database are being updated with the last item of the list. Please could you help me understand where I am making a mistake?
    [Screen Shot 2022-06-09 at 13.09.59 PM.png][Screen Shot 2022-06-09 at 13.11.24 PM.png][Screen Shot 2022-06-09 at 13.13.34 PM.png]
  • Umair Kamil
    Umair Kamil Member
    Options
    Objective: I want to be able to edit/update multiple records through the Xano Post API for my tables

    Step 0: Created LIST Inputs as
    •  item_id_list_input; integer []
    •  item_name_list_input; text []
    •  item_price_list_input; integer []

    Step 1: Created a For Each Loop where the input item_id_list_input is passed;
    • function is For Each Loop
    • input of this step is item_id_list_input 
    • output of this step is item_id_list


    Step 1.1: Under the main For Each Loop, created a nested For Each Loop where the input item_name_list_input is passed;
    • function is For Each Loop
    • input of this step is item_name_list_input 
    • output of this step is item_name_list


    Step 1.1.1: Under the Nested For Each Loop, created an Edit Record from Fruits
    • function is Edit Record
    • search by field_name = id and field_value = item_id_list (from Step 1)
    • mapped fruit_name to item_name_list
    • output of this step is item_names_updated


    Step 1.2: Under the main For Each Loop, created a nested For Each Loop where the input item_price_list_input is passed;
    • function is For Each Loop
    • input of this step is item_price_list_input 
    • output of this step is item_price_list


    Step 1.2.1: Under the Nested For Each Loop, created an Edit Record from Fruits
    • function is Edit Record
    • search by field_name = id and field_value = item_id_list (from Step 1)
    • mapped fruit_price to item_price_list
    • output of this step is item_prices_updated
  • Umair Kamil
    Umair Kamil Member
    Options
    Hi   - Still need a bit of help with this. Still Struggling! 
  • Chris Coleman
    Chris Coleman Administrator

    ADMIN

    Options
     Hi, Umair! I went to take a look at your function and it seems to have changed dramatically. Do you still need assistance with this? Happy to dive back in if so.
  • Umair Kamil
    Umair Kamil Member
    Options
     Hi Chris, yes I would be grateful for the help! Please do assist. I'm also wondering whether I need to merge the inputs into an object or something?
  • Chris Coleman
    Chris Coleman Administrator

    ADMIN

    Options
     Thanks. In the function as it stands right now, do you experience the same issue?
  • Umair Kamil
    Umair Kamil Member
    Options
     So the function does execute, as you can see in the image. However, the issue is that the last name and last price get executed in the loop. You can see, as suggested, I'm nesting the loops. But the result is that the last name (e.g Dragon Fruit) and last price (e.g 3320) have been pushed to the database.

    I'm wondering, do I need to merge the inputs and create an object? I saw the tutorial for how to call an external API and map the data to your database. In that  created a variable from the results object in the API response. And thereafter, he used a single For Loop to map all the items in the object to the database, if I'm not mistaken.
  • Chris Coleman
    Chris Coleman Administrator

    ADMIN

    Options
     Thanks for clarifying. I reached out to Michael on this one and I think we're going to recommend you join us for an upcoming office hours so we can troubleshoot this one live, just to make sure we get it right for you.
  • Umair Kamil
    Umair Kamil Member
    Options
     Sure. I've booked one for Tuesday, would that suffice? Or should I book one for today/Thursday? Asking as it says on calendly that Tuesday is for basic troubleshooting and Thursday is for advanced trouble shooting. 
  • Chris Coleman
    Chris Coleman Administrator

    ADMIN

    Options
     Either one is fine this time around, no worries!