Javascript to create new record with linked column in one click

If there are 2 tables:

| Book Name (text) | Borrow (button) |

| ID (autonumber) | Book Name (link) |

Once Borrow button clicked, I want a new record be created in “Requests” table with correct link to the book record where I clicked its button.

How to write a script in Javascript to do that?

I tried below lines which get the current row and the new row, but how to add the link.

const currentRow = base.context.currentRow;
const table = base.getTableByName('Requests');
var newRow = base.addRow(table,{});

Got it, here it’s:

// Parent table
const linkedTableName = 'Books';

// Child table
const tableName = 'Requests';

// Get linkId of the column in child table
const linkId = base.getColumnLinkId('Requests', 'Book Name');

// Create new record in child table and get its id
const newRow = base.addRow(tableName, {});
const rowId = newRow['_id'];

// Get current record id
const currentRow = base.context.currentRow;
const linkedRowId = currentRow['_id'];

// Add the link of the current record to the new record created
base.addLink(linkId, tableName, linkedTableName, rowId, linkedRowId);

Or in short 3 lines:

const currentRow = base.context.currentRow;
const newRow = base.addRow(base.getTableByName('Requests'), {});
base.addLink(base.getColumnLinkId('Requests', 'Book'), 'Requests', 'Books', newRow['_id'], currentRow['_id']);
1 Like

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