Is there a Python script that would allow to create a new record in a linked table ?
I would like to use this with a button, and it will basically do the same as the Add record that link the current row we are working from to the newly created record in the other table.
This is also interesting for me because then the button will open the newly created record and show calculation information from formulas (in the other table where the record is created) that the Add record cannot show.
I’m sorry, it is rather hard to explain. Basically, a python script to replicate the Add record linking 2 records in 2 different tables.
from seatable_api import Base, context
# Authenticate using context
base = Base(context.api_token, context.server_url)
base.auth()
# Additional configuration
TABLE1_NAME = 'Table1'
TABLE2_NAME = 'Table2'
LINK_COLUMN_NAME = 'Linked_Records' # Replace with your actual link column name
CURRENT_ROW_ID = context.current_row["_id"]
def add_linked_record(current_row_id):
"""
Add a new record to Table2 and link it to the current record in Table1
:param current_row_id: Row ID of the current record in Table1
:return: Row ID of the newly created record in Table2
"""
try:
# Prepare the new record for Table2
new_record_data = {
# Add your custom fields here
# Example: 'title': 'New Linked Record',
# Add any other required fields for Table2
}
# Add the new record to Table2
new_row = base.append_row(TABLE2_NAME, new_record_data)
new_row_id = new_row["_id"]
# Get the link column ID
link_id = base.get_column_link_id(TABLE1_NAME, LINK_COLUMN_NAME)
# Add the link between the two records
base.add_link(link_id, TABLE1_NAME, TABLE2_NAME, current_row_id, new_row_id)
print(f"New record added to {TABLE2_NAME} with ID: {new_row_id}")
print(f"Linked record {new_row_id} to {current_row_id} in {TABLE1_NAME}")
return new_row_id
except Exception as e:
print(f"An error occurred: {e}")
return None
new_record_id = add_linked_record(CURRENT_ROW_ID)