Linking records between two tables with javascript

Hello Everyone,

I hope this message finds you well. I am currently writing a JavaScript code to link all records from one table to another. For this purpose, I am using a loop, but each time the code runs, I receive the following error message:

Failed to execute operation on server: Operation invalid.

Here is the code I have written (I have debugged it using AI Copilot):

async function updateLinks() {
    const rows = await base.getRows('list time', 'Default View');
    const rowss = await base.getRows('list task', 'Default View');
    // vars
    const viewName = 'Default View';
    const columnR2 = 'Formula'; 
    // code - don't change careless!
    const currentRow = base.context.currentRow;
    const table = base.getActiveTable();
    const view = base.getViewByName(table, viewName);

    // Get the initial value of column r2
    let x = rows.map(row => row[columnR2]);

    const updatedRows = [];
    let pos = 0;

    rows.forEach((row) => {
        updatedRows.push(rows[pos]._id);
        pos++;
    });

    await base.updateLinks('1M54', 'list task', 'list time', x, updatedRows); // Changed from [updatedRows] to updatedRows
}

updateLinks();
I would greatly appreciate any guidance or assistance in resolving this issue.

Thank you in advance for your help.

Best regards

Hello Hossein,
We need to update the row links by using the row IDs from two sub-tables. Here is my use case:

async function updateLinks() {
    const table1_rows = await base.getRows('Table1', 'Default View');
    const table2_rows2 = await base.getRows('Table2', 'Default View');

    const linkId = base.getColumnLinkId('Table1', 'Link to other records')
    const linkedRowsIds = table2_rows2.map((row) => row._id)
  
    // base.updateLinks(linkId, tableName, linkedTableName, rowId, updatedlinkedRowIds)
    await base.updateLinks(linkId, 'Table1', 'Table2', table1_rows[0]._id, linkedRowsIds);
}
updateLinks();

Best regards

1 Like

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