Help

Re: link data records with each other within a table

Solved
Jump to Solution
927 0
cancel
Showing results for 
Search instead for 
Did you mean: 
mirkolando
7 - App Architect
7 - App Architect

Hello everyone,

I have a table in which we record events. Some of these events relate to a media campaign that is also recorded in the same table.

I have therefore created a column in which you can link a campaign when creating an event. To do this, I use a view in which only the campaigns are displayed.

This works without any problems. However, when I assign an event to a campaign, I would like the assigned events to be displayed in the campaign data record.

Does anyone have any idea how I can implement this?
Currently, only the selected campaign is displayed in the event record, but not the other way around.

I would like to be able to click on the campaign record and see all linked events.

Does anyone have any ideas on how I can implement this?

Thank you very much and best regards
Mirko

1 Solution

Accepted Solutions
ScottWorld
18 - Pluto
18 - Pluto

@mirkolando 

Unfortunately, Airtable doesn’t offer automatic backlinking within the same table.

Be sure to email support@airtable.com and suggest this as a feature request.

Your only 2 options for this would be to:

(1) Write a custom JavaScript to handle this for you. Two scripts that do this are available here and here.

or

(2) For a no-code way of doing this that doesn’t require writing any programming code at all, you can use Make’s advanced Airtable automations, which is a no-code automation tool for Airtable.

There is a small learning curve with Make, which is why I’ve assembled a bunch of Make training resources in this thread.

For example, to instantly trigger a Make automation from Airtable, check out this thread on instantly triggering a Make automation from Airtable using a webhook.

You will also need to know how to work with arrays, which I demonstrate on this Airtable podcast episode.

p.s. If you have a budget for your project and you’d like to hire an expert Airtable consultant to help you with any of this, please feel free to contact me through my website: Airtable consulting — ScottWorld 

See Solution in Thread

5 Replies 5

Ah, you're going to want to google "same table backlinks", and the general way to handle it is to use scripting unfortunately. @kuovonne  has a script ready built for this that you can find here: https://kuovonne.gumroad.com/l/joRkE

ScottWorld
18 - Pluto
18 - Pluto

@mirkolando 

Unfortunately, Airtable doesn’t offer automatic backlinking within the same table.

Be sure to email support@airtable.com and suggest this as a feature request.

Your only 2 options for this would be to:

(1) Write a custom JavaScript to handle this for you. Two scripts that do this are available here and here.

or

(2) For a no-code way of doing this that doesn’t require writing any programming code at all, you can use Make’s advanced Airtable automations, which is a no-code automation tool for Airtable.

There is a small learning curve with Make, which is why I’ve assembled a bunch of Make training resources in this thread.

For example, to instantly trigger a Make automation from Airtable, check out this thread on instantly triggering a Make automation from Airtable using a webhook.

You will also need to know how to work with arrays, which I demonstrate on this Airtable podcast episode.

p.s. If you have a budget for your project and you’d like to hire an expert Airtable consultant to help you with any of this, please feel free to contact me through my website: Airtable consulting — ScottWorld 

@ScottWorld thank you very much! Your mentioned option one with the JavaScript worked gread within an automation process! 

You're welcome! Glad I could help! 😃

Pjero_Kusijanov
7 - App Architect
7 - App Architect

I have used quite a lot of time to sort this out, and I have finally managed to come to bullet profs solution that really works as it is supposed to.  

It includes Airtable automations and Script.

 

My table name: Contacts
My linked field name in Contacts table: Linked Records
*Change this values in script to match yours or use same names as I did.

In table where you want to use bi-directional linking (Automatically Linking Records to Each Other);

1. Create field "Link to another record" linking to itself.
     - In my case, {Linked Records}. Make sure to check "Allow linking to multiple records"


2. Create Automation

2.2 Action 1: "When a record is updated". Point it to "Contacts" table, and for fields use {Linked Records}

2.3. Action 2: "Run Script".
       - For input variable; Set name to: recordId, and value Airtable record ID (See photo attached)
       - Paste the Script 

 

 

// Define the table
let table = base.getTable("Contacts"); // Replace "Contacts" with your actual table name

// Get the record that triggered the automation
let inputConfig = input.config();
let recordId = inputConfig.recordId;
let record = await table.selectRecordAsync(recordId);

// Define the linked records field
let linkedField = "Linked Records";

// Get the current linked records for the trigger record
let currentLinkedRecords = record.getCellValue(linkedField) || [];

// Array to store records that need to be updated
let recordsToUpdate = [];

// Loop through all linked records and ensure bidirectional link
for (let linkedRecord of currentLinkedRecords) {
let linkedRecordId = linkedRecord.id;
let linkedRecordDetails = await table.selectRecordAsync(linkedRecordId);

let linkedRecordLinkedTo = linkedRecordDetails.getCellValue(linkedField) || [];

// Check if the current record is already in the linked record's linked field
if (!linkedRecordLinkedTo.some(rec => rec.id === record.id)) {
// Add this linked record to the update array (only if the link doesn't already exist)
recordsToUpdate.push({
recordId: linkedRecordId,
linkedRecords: [...linkedRecordLinkedTo, { id: record.id }]
});
}
}

// Update all necessary records in a batch to avoid multiple triggers
if (recordsToUpdate.length > 0) {
let updates = recordsToUpdate.map(record => ({
id: record.recordId,
fields: {
[linkedField]: record.linkedRecords
}
}));

// Batch update all records
await table.updateRecordsAsync(updates);
}

// Now handle removing bidirectional links
let allRecords = await table.selectRecordsAsync();
let recordsToRemove = [];

// Loop through all records and check for unlinked records
for (let otherRecord of allRecords.records) {
if (otherRecord.id !== recordId) {
let otherLinkedRecords = otherRecord.getCellValue(linkedField) || [];

// Check if the current record was previously linked but is now unlinked
if (otherLinkedRecords.some(rec => rec.id === recordId) && !currentLinkedRecords.some(rec => rec.id === otherRecord.id)) {
// Add this record to the removal array (only if the link exists and needs removal)
recordsToRemove.push({
recordId: otherRecord.id,
linkedRecords: otherLinkedRecords.filter(rec => rec.id !== recordId)
});
}
}
}

// Batch remove records that no longer need to be linked
if (recordsToRemove.length > 0) {
let removals = recordsToRemove.map(record => ({
id: record.recordId,
fields: {
[linkedField]: record.linkedRecords
}
}));

// Batch update all records to remove unneeded links
await table.updateRecordsAsync(removals);
}


Hope you manage to get it to work. Enjoy 🙂