Help

Re: How to show linked records?

Solved
Jump to Solution
1962 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Edmunds_Priede
5 - Automation Enthusiast
5 - Automation Enthusiast

how can i show linked records for a selected table record that changes dynamicly when i select/deselect rows?

i can get selected row ids from cursor, but i am struggling to to select and show linked recods

1 Solution

Accepted Solutions

useState() isn’t in the Airtable APi docs because its a part of regular ol’ React, and not unique to Airtable.

You may have to do

const linkedRecords = useRecords(record.selectLinkedRecordsFromCell(field))

See Solution in Thread

5 Replies 5
const field = table.getFieldByNameIfExists("Name of linked field")
const linkedRecords = record.selectLinkedrecordsFromCell(field)
return (<RecordCardList records={linkedRecords} />)

Sorry, I thought you said you had that part figured out. There are examples of how to load data from the selected record in the Airtable Blocks SDK.

const [selectedRecordId, setSelectedRecordId] = useState(null);

const cursor = useCursor();

useWatchable(cursor, ['selectedRecordIds'], () => {
   if (cursor.selectedRecordIds.length > 0) {
      setSelectedRecordId(cursor.selectedRecordIds[0]);
   }
});

const selectedRecord = useRecordById(table, selectedRecordId ? selectedRecordId : null );

^ this will work for the first of selected records. If you want to return the record model for all selected records you’d use a regular table query and filter for records with an ID in the selected array,

Edmunds_Priede
5 - Automation Enthusiast
5 - Automation Enthusiast

Uncaught TypeError: (0 , _ui.useState) is not a function

now i get this. looks like useState is not in API docs :frowning:

Edmunds_Priede
5 - Automation Enthusiast
5 - Automation Enthusiast

i have now this code

   const selectedRecord = useRecordById(tableCustomers, selectedRecordId ? selectedRecordId : 'recSlhi5qVG6mNLdQ');
    
   const linkedRecords = selectedRecord.selectLinkedRecordsFromCell(fieldRP); 
  
   console.log('selected')
   console.log(selectedRecord)
   console.log('linked')
   console.log(linkedRecords[0])

selectedRecord is changing as expected, but linkedRecords is empty all the time.

no errors thrown

full code

import {initializeBlock, RecordCard,RecordCardList, useBase, useRecords,useRecordById,useCursor, useWatchable ,useLoadable , Box} from "@airtable/blocks/ui";
import React, { useState } from 'react';

function HelloWorldApp() {
    // YOUR CODE GOES HERE
    return <div>Hello world ...🚀</div>;
}


var contactsLinked;



const RecordCardExample = (linked, id) => {
 
 // if (typeof id == 'string') {
	  
//	  return <p> {id} </p>
 // }
  
     const base = useBase();
  
  
  
     const tableCustomers = base.getTableByName("Customers");
     const tableContacts = base.getTableByName("Contacts");
     const fieldRP = tableCustomers.getFieldByName('Contacts');
  
  
     const fieldPosition = tableContacts.getFieldByName('Position');
     const fieldResponsibleperson = tableContacts.getFieldByName('Responsible person');
  
   
     const [selectedRecordId, setSelectedRecordId] = useState(null);

     const cursor = useCursor();

     useWatchable(cursor, ['selectedRecordIds'], () => {
        if (cursor.selectedRecordIds.length > 0) {
            setSelectedRecordId(cursor.selectedRecordIds[0]);
        }  else  { setSelectedRecordId('recSlhi5qVG6mNLdQ')  }
     });

     const selectedRecord = useRecordById(tableCustomers, selectedRecordId ? selectedRecordId : 'recSlhi5qVG6mNLdQ');
    
     const linkedRecords = selectedRecord.selectLinkedRecordsFromCell(fieldRP); 
  
     console.log('selected')
     console.log(selectedRecord)
     console.log('linked')
     console.log(linkedRecords[0])
	  
 
     //return  <RecordCard record={linkedRecords[0]} /> 
  
 
	 return <p> </p>
 
};




initializeBlock(() => <RecordCardExample />);

useState() isn’t in the Airtable APi docs because its a part of regular ol’ React, and not unique to Airtable.

You may have to do

const linkedRecords = useRecords(record.selectLinkedRecordsFromCell(field))