Links¶
link id and column key
linkId should not be mistaken with the column key! The key value is unique (like an id) whereas the link id will be shared between the two linked columns. Please note that linkId is used as argument to add/update/remove links, whereas you'll have to provide linkColumnKey (the link column key) to get linked records. Both information are available in the column object:
{
"key": "Cp51", /* (1)! */
"type": "link",
"name": "Link column",
"editable": true,
"width": 200,
"resizable": true,
"draggable": true,
"data": {
"display_column_key": "0000",
"is_internal": true,
"link_id": "UAmR", /* (2)! */
"table_id": "FJkA", /* (3)! */
"other_table_id": "nw8k", /* (4)! */
"is_multiple": true,
"is_row_form_view": false,
"view_id": "",
"array_type": "text",
"array_data": null,
"result_type": "array"
},
"permission_type": "",
"permitted_users": [],
"permitted_group": [],
"edit_metadata_permission_type": "",
"edit_metadata_permitted_users": [],
"edit_metadata_permitted_group": [],
"description": null,
"colorbys": {}
}
-
The column
key(referred aslinkColumnKeyinbase.getLinkedRecordsarguments) -
The link id of the column (referred as
linkIdin the add/update/remove link(s) methods arguments) -
The table whose id is
table_idis referred later in this section as the source table (the table containing this column) -
The table whose id is
other_table_idis referred later in this section as the target table
Get linkId¶
getColumnLinkId
Get the link id of the column columnName from the table tableName.
Output String (throws an error if table tableName or column columnName doesn't exist)
Example
Get linked records¶
Rows and records, source and target
Rows and records are basically the same things. However, to make the following description easier to understand, we will differentiate them:
-
Rows are from the source table (the table whose id is
tableId) -
Records are the rows from the target table (the table linked to the source table in the column whose
keyislinkColumnKeyor whose link id islinkId)
getLinkedRecords
List the records linked (in the column whose key is linkColumnKey) to one or more rows of the source table. The row(s) you want to get the linked records from are defined in the linkedRows object (see below).
await/* (1)! */ base.getLinkedRecords(tableId: String, linkColumnKey: String, linkedRows: Object) /* (2)! */;
-
awaitis used for asynchronous functions. This is required to ensure that the following operations (or the variable where you store the results) wait for the query's response to arrive before continuing to execute the script -
tableId: the id of source tablelinkColumnKey: the column key of the link-type column of source table (not the link id frombase.getColumnLinkId)linkedRows: an array of objects, each of them containing:-
row_id: the id of the row we want to get the linked records from -
limit: the maximum number of linked records to get (default is 10) -
offset: the number of first linked records not to retrieve (default is 0)
-
Output A key:value data structure where each key is the id of a row of the source table and the corresponding value is an array of link objects (see Output structure example below)
Example
{
'FzNqJxVUT8KrRjewBkPp8Q' /* (1)! */: [
{'row_id': 'LocPgVvsRm6bmnzjFDP9bA', 'display_value': '1'} /* (2)! */,
{'row_id': 'OA6x7CYoRuyc2pT52Znfmw', 'display_value': '3'},
...
],
'Jmnrkn6TQdyRg1KmOM4zZg': [
{'row_id': 'LocPgVvsRm6bmnzjFDP9bA', 'display_value': '1'},
{'row_id': 'OA6x7CYoRuyc2pT52Znfmw', 'display_value': '3'},
...
]
}
-
id of a row of the source table
-
link object:
row_idis the id of the linked record (row from the target table)display_valueis the displayed in the column whosekeyislinkColumnKey(from a column of the target table)
Output Object containing the linked records for each row (see Output structure example above)
Example: Get linked records from current row
const table = base.getTableByName('Table1');
const linkColumn = base.getColumnByName(table,'Table2 link');
const currentRowLinks = await base.getLinkedRecords(table._id, linkColumn.key, [{'row_id': base.context.currentRow._id, 'limit':100 /* (1)! */}]);
currentRowLinks[base.context.currentRow._id].forEach((link) => {output.text(link)});
limit:100 => the response will return maximum 100 rows
Add link¶
addLink
Add link in a link-type column. You'll need the source target's name tableName, the target table's name linkedTableName, the linkId from the link-type column and both the ids of the rows you want to link: rowId for the row from the source table and linkedRowId for the record from the target table.
base.addLink(linkId: String, tableName: String, linkedTableName: String, rowId: String,
linkedRowId: String);
Output Nothing
Example
base.addLink('5WeC', 'Team Members', 'Contacts', 'CGtoJB1oQM60RiKT-c5J-g', 'PALm2wPKTCy-jdJNv_UWaQ');
Example: Add link to current row
// Do not hesitate to store the tables' and columns' names at the beginning of your script,
// it will make it really easier to update if names change
const table1Name = "Table1";
const table1LinkColumnName = "Table2 link";
const table2Name = "Table2";
const linId = base.getColumnLinkId(table1Name,table1LinkColumnName); /* (1)! */
const currentRowId = base.context.currentRow._id;
base.addLink(linId, table1Name, table2Name, currentRowId, 'J5St2clyTMu_OFf9WD8PbA');
- Remember you can use
base.getColumnLinkIdto get the link id of a specific link-type column.
Update link(s)¶
updateLinks
Update the content of the link-type column whose link id is linkId for the row with id rowId in the table tableName. It will remove all existing row links and add new links to records of table linkedTableName with ids listed in the updatedlinkedRowIds array.
Output Nothing
Example
const records = base.getRows('Contacts', 'Default_view');
// Update links for row from "Team Members" with _id CGtoJB1oQM60RiKT-c5J-g to [records[0]._id, records[1]._id, records[2]._id, records[3]._id]
// Real-life tip: ensure that the array "records" actually contains at least 4 elements!
base.updateLinks('5WeC', 'Team Members', 'Contacts', 'CGtoJB1oQM60RiKT-c5J-g', [records[0]._id, records[1]._id, records[2]._id, records[3]._id]);
Remove link¶
removeLink
Delete the link to the record from table linkedTableName whose id is linkedRowId in the row from table tableName whose id is rowId. Every arguments are String.
Output Nothing
Example