Help

Re: Clear/Blank out Linked Field via Automated Script

6 0
cancel
Showing results for 
Search instead for 
Did you mean: 

Hello all,

I’ve been updating a Record in a Linked Table to point to a different Main Record using the below code;

let _ins = input.config();

let main_table = base.getTable("Main Table");
let linked_table = base.getTable("Linked Table");

let main_rec    = _ins._get_record;
let linked_rec  = _ins._get_linked_record;
let linked_val  = _ins._get_linked_value;

console.log(`main_rec:   ${main_rec}`);
console.log(`linked_rec: ${linked_rec}`);
console.info(`linked_val: ${linked_val}`);

if (linked_val == "Not Listed") {
    console.warn("Not Listed - Clearing and rewriting entry");
        
    await linked_table.updateRecordAsync(`${linked_rec}`, {
        "Linked Text": `Example updated ${linked_table} record text`,
      //"Main"   : [{id:`${main_rec}`}] //Field is updated with expected record via recordID
        "Main"   : [{id:`${""}`}] //Attempt to clear field is not working...
        });
    console.info(`Updated Table "${linked_table.name}" record ${linked_rec} with "${main_rec}`);
};

This particular line of code works, updating an entry

//Field is updated with expected record via recordID
 "Main" : [{id:`${main_rec}`}] 

But what if I want to update the entry with a blank value, clear out the field for that record?

// Attempt to clear field is not working
"Main" : [{id:`${""}`}] 

I’ve tried null, undefined, and a few other ideas - none of which are executing. Keen for some help if anyone can spare a moment.

I’m also keen to rewrite my code with more direct calls. For example, although this works it makes me feel uneasy, and I’d like to know the preferred syntax;

    await linked_table.updateRecordAsync(`${linked_rec}`, {}

I’m sure I’m not to be using string literals for record ID lookups :joy: … yeah it works, no I’m not proud of it. Appreciate some pointers!

14 Replies 14

How are you setting the input variable _get_linked_record? Is it the result of a “Find Records” action or is it from a triggering record? What does typeof linked_rec produce? If linked_rec is a string, why not use the variable by itself?

It’s a mix of Automation input, but also record queries. This morning my previous JS learning finally “clicked” in my head of how to approach problems within Airtable scripts, and now I’m leveraging .filter and .map by chaining them together, and drawing data out of getCellValue etc.

//console.log(query.records.filter( record => (record.getCellValue("Numbers") == "0")).map( record => record.getCellValue("Important Note")));

It’s all coming back to me, just like riding a bike! Feels good. :smiling_face_with_sunglasses:

This looks suspicious to me. It looks like you are comparing the value of a number field with a text string, and the two would never be equal. If the “Numbers” field actually returns a text string that looks like a number, then it is badly named.

Yes, good spot. But it’s purposefully a string field filled with leading zero number entries that have a set/locked digit length. Aren’t inherited projects just the dandiest things?

Craig_Toohey
6 - Interface Innovator
6 - Interface Innovator

Hi community.  Perhaps there's something that's over my head that I'm not understanding about what the author was trying to achieve, but for the noobs and amateurs out there, if you're looking for a way to set a linked record field to blank, this is the easiest way I've found:

1. Add a Trigger for your automation

2. Add a Run a Script action.  Enter this as the script:

output.set("blankField", null);

That's it.  One line.

3. Add an Update Record action.  Select the Table for the record you wish to update, then enter the Record ID you want to update (the simplest is to select the Airtable record ID from the automation Trigger, but you could also use Find records and a repeating group if you want to do many) then choose the field you want to update, set the gear option to configure to Dynamic (to use variables from a previous step in the automation), hit the little blue plus sign to find your script action, click on the Run a Script from the "Use data from..." pop-up, and then click on your blankField you created with the script.

4. Test it.

Tada!