Javascript code for process the records of table and link

Hello,

I require a JavaScript code to process the records of two tables. If they match the condition, link them. If they do not match the condition, process the other records.

I was testing a for loop, but each time I attempt to execute it, I receive the following error:
image

Here is my code:

javascript

const rows = base.getRows('list time', 'Default View');
const rowss = base.getRows('list task', 'Default View');

// vars
const viewName = 'Default View';
const columnR2 = 'Formula'; 

// code - don't change carelessly!
const currentRow = base.context.currentRow;
const table = base.getActiveTable();
const view = base.getViewByName(table, viewName);

updatedRows = [];
let pos = 0;
rows.forEach((row) => {
  x = row[columnR2];
  base.updateLinks('1M54', 'list task', 'list time', x, [rows[0]._id, rows[1]._id, rows[2]._id, rows[3]._id]);
  pos++;
});

Hi @Hossein,
I struggle understanding your code:

  • you say If they match the condition but I can’t see any test (is it done directly in SeaTable and the result is present in columnR2 ? Even if I’m right, I think you’ll need to check that there’s actually something in row[columnR2]
  • if I’m wrong and you want to compare each row of the first table with each row of the second table in your script, you’ll probably need two nested for loops, looking something like:
for each row of rows { 
    for each row of rowss {
        if (test) {
            base.updateLinks or even better appending data to execute a base.batchUpdateLinks after both for loops
            break eventually, depending on the number of rows from `rowss` being allowed to be link to each row of `rows` (1-1, 1-n, n-1 or n-n relationship)
        }
    }
}
  • your current code is always linking the row id present in the Formula column of your list time table (which should be an id from the list task table in order to work) to the four first rows of your list time table/ Is it what you’re trying to do ?

Bests,
Benjamin


I love spending time gathering information and explaining solutions to help you solve your problems. I spend… quite a bit of time on it :sweat_smile: .If you feel grateful for the help I’ve given you, please don’t hesitate to support me. I am also available for paid services (scripts, database architecture, custom development, etc.).

Hi @bennhatton, Thank you for the reminder. The JavaScript code I am trying to write is highly complex and constantly evolving. For this reason, I decided to prepare it in sections. The part I have shared here is related to the for loop, on which I have made every effort but have been unable to resolve.

Please take a look at the images below.


I want the code to perform the following task: It should review the records in Table T1 and identify the number present in each. If the number is 1, it should link to the first empty record in Table T2. If the number is 2, it should locate the first two empty records in Table T2 and link to them. And so on.

In essence, the code should read the number in Table T1 and, based on that number, link it to the corresponding number of unlinked records starting from the first available record in Table T2.

This task has consumed a significant amount of my time, but I have not been able to solve it. I would greatly appreciate your assistance in making this code work.

If my code is overly complex, please accept my apologies. Even I find it confusing at times. The code I wrote works as you described. As I mentioned in my previous message, this code was intended to test the feasibility of the for loop. However, it was unsuccessful. The operation performed by the code was not important to me; I only wanted to ensure that the for loop functioned correctly. Unfortunately, it didn’t, and the same error, which I shared in the image in my initial message, occurred.

Hi Hossein, don’t apologize, it’s always pretty hard to keep a code crystal clear and easily understandable! Don’t save on comments, they can be very useful to return to an old job at a later date… I don’t really get the full connection between your first message and the second one, but what you’re trying to achieve is pretty straightforward for me in your second message, so here is a proposal. Unfortunately, I didn’t manage to use batchUpdateLinks (I got an error error: base.batchUpdateLinks is not a function which is pretty weird, maybe @cdb can help you on that specific point) so here is a version using updateLinks, but this can make a pretty huge amount of server calls depending on the size of your tables, that’s why it would be really better to use batchUpdateLinks. I defined as much as name variables at the beginning to make it easier to adapt to your use case.

const table1Name = 'Table1';
const table2Name = 'Table2';

const table1ViewName = 'Default View';
const table2ViewName = 'Default View';

const table1LinkColmunName = 'T2';
const table1NumberColumnName = 'Number';
const table2LinkColumnName = 'T1';

const rows = base.getRows(table1Name, table1ViewName);
const rowss = base.getRows(table2Name, table2ViewName);

recordsWithoutLinks = [];

// Looking for unlinked records in table2
rowss.forEach((row) => {
  if (!(table2LinkColumnName in row) || row[table2LinkColumnName] == null) {
    recordsWithoutLinks.push(row._id);
  }
  else {
    output.text(row._id + ' already linked');
  }
});
// Uncomment if you manage to get base.batchUpdateLinks() to work //
//let row_id_list = [];
//let other_rows_ids_map = {};

// Iterating over all records of table1
rows.forEach((row) => {
  if (!(table1LinkColmunName in row) || row[table1LinkColmunName] == null) { // not sure if you want to keep this test : it keeps only the unlinked records
    // get the number of records to link in the Number column
    let numberOfLinks = Number(row[table1NumberColumnName]);
   // Ensure that there are enough unlinked records in table2
    if(recordsWithoutLinks.length>=numberOfLinks) {
      // Uncomment if you manage to get base.batchUpdateLinks() to work //
      //row_id_list.push(row._id);
      //other_rows_ids_map[row._id] = recordsWithoutLinks.splice(0,numberOfLinks); // get n first items of recordsWithoutLinks
      // Comment if you manage to get base.batchUpdateLinks() to work //
      base.updateLinks(base.getColumnLinkId(table1Name, table1LinkColmunName), table1Name, table2Name, row._id, recordsWithoutLinks.splice(0,numberOfLinks));
    }
  }
  else {
    output.text(row._id + ' already linked');
  }
});
// Uncomment if you manage to get base.batchUpdateLinks() to work //
//base.batchUpdateLinks(base.getColumnLinkId(table1Name, table1LinkColmunName), table1Name, table2Name, row_id_list, other_rows_ids_map);
output.text('script completed');

Hope you’ll be able to adapt this to your use case…

Bests,
Benjamin


I love spending time gathering information and explaining solutions to help you solve your problems. I spend… quite a bit of time on it :sweat_smile: .If you feel grateful for the help I’ve given you, please don’t hesitate to support me. I am also available for paid services (scripts, database architecture, custom development, etc.).

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.