0

How to write code to batch update data in two or more child tables?

Table2 works if there is only one line of data. However, I need to modify multiple lines of data. How do I modify my code?

Uploaded files,Thank you for your help!

7 replies

null
    • gold_cat
    • 5 mths ago
    • Reported - view
    • Fred
    • 5 mths ago
    • Reported - view

    Looking at your DB, first let us take a look at 程式. You can remove the select statement because you are only looking at related records. This makes the code much faster as your dataset grows up. You should always use relationship where ever you can. Try NOT use select statements as much as possible.

    let aa := table2;
    table2.Table3.[number('选项')]
    

    Now to your button. You can simplify the code to:

    if avg('程式') = 1 then
        table2.Table3.('选项' := 2)
    else
        table2.Table3.('选项' := 1)
    end
    

    This will take every related record in Table3 and modify 选项 in each record to whatever you want.

    Interesting use of avg() to get a sense of the selections of a choice field.

      • gold_cat
      • 5 mths ago
      • Reported - view

       

      Fred, you are my idol. I am a beginner, and I may have made some beginner mistakes. Thank you for your help!

    • Fred
    • 5 mths ago
    • Reported - view

    Let us troubleshoot your original code as it can help in the future.

    You wrote in 程式 :

    let aa := table2;
    (select table3 where '数据表2' = aa).[number('选项')]
    

    This doesn't work since table3 is in a N:1 to table2, Ninox will always return an array of numbers for 数据表2. So this select is basically saying find records in table3 where only it is linked to only 1 record in table2. Once you have more than 1 record linked the equal doesn't work anymore.

    You can see this if you create a new formula field in table1 with:

    (select table3).'数据表2'
    

    One way to fix this is to use the contains() command. It would look like:

    let aa := table2;
    (select table3 where contains(text('数据表2'), text(aa))).number('选项')
    

    Why do we use the text() command? Because contains() can't handle NID or RID so we have to convert the info into simple text.

    Now to your button:

    if avg('程式') = 1 then
        let aa := table2;
        let xx := (select table3 where '数据表2' = aa);
        for xx in xx do
            xx.('选项' := 2)
        end
    else
        let aa := table2;
        let xx := (select table3 where '数据表2' = aa);
        for xx in xx do
            xx.('选项' := 1)
        end
    end
    

    In your for loop command I would recommend against reusing variable names like you do in line 4 and 10.

    If you copy over the contains() and fix the for loop variable it would look like:

    if avg('程式') = 1 then
        let aa := table2;
        let xx := (select table3 where contains(text('数据表2'), text(aa)));
        for loop1 in xx do
            xx.('选项' := 2)
        end
    else
        let aa := table2;
        let xx := (select table3 where contains(text('数据表2'), text(aa)));
        for loop1 in xx do
            xx.('选项' := 1)
        end
    end
    
      • gold_cat
      • 5 mths ago
      • Reported - view

      I learned a lot again, and I saw the "loop1" command, which I need some time to understand. have a nice day~ 

    • Fred
    • 5 mths ago
    • Reported - view
     said:
    I saw the "loop1" command,

     Just to be clear it is a for loop command. "loop1" is just a variable name used in the for loop. "loop1" can be anything. I was just saying don't reuse variable names like you did with "xx".

      • gold_cat
      • 5 mths ago
      • Reported - view

       thanks!

Content aside

  • Status Answered
  • 5 mths agoLast active
  • 7Replies
  • 62Views
  • 2 Following