Hello! I'm trying to create a button in a table that when I press it, moves the whole record to another table within the same base and, AFTER THAT, deletes the record from the original base. I'm using an API Token. Also, both tables have exactly the same columns, in the same order.
However, I'm stuck with how to retrieve the record ID of the row where the button pressed is.
Could someone help out with the script? This is what I have so far:
// Airtable's API token and base information
const API_TOKEN = 'aaaaaaaaaaaaaaaaaaaaaaaa';
const BASE_ID = 'appaaaaaaaaaaaaaaa';
const SOURCE_TABLE_NAME = 'Table1';
const DESTINATION_TABLE_NAME = 'Table2';
// Get the current record ID using input.recordAsync
input.recordAsync('Select a record to move and delete').then((record) => {
const recordId = record.id;
moveAndDeleteRecord(recordId);
});
// Function to move and delete a record
async function moveAndDeleteRecord(recordId) {
try {
// Step 1: Get the record data
const getRecordResponse = await fetch(`https://api.airtable.com/v0/${BASE_ID}/${SOURCE_TABLE_NAME}/${recordId}`, {
method: 'GET',
headers: {
Authorization: `Bearer ${API_TOKEN}`,
},
});
const recordData = await getRecordResponse.json();
// Step 2: Create a new record in the destination table
await fetch(`https://api.airtable.com/v0/${BASE_ID}/${DESTINATION_TABLE_NAME}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${API_TOKEN}`,
},
body: JSON.stringify({ fields: recordData.fields }),
});
// Step 3: Delete the record from the source table
await fetch(`https://api.airtable.com/v0/${BASE_ID}/${SOURCE_TABLE_NAME}/${recordId}`, {
method: 'DELETE',
headers: {
Authorization: `Bearer ${API_TOKEN}`,
},
});
console.log('Record moved and deleted successfully');
} catch (error) {
console.error('Error moving and deleting record:', error);
}