Help

Re: Automation to remove a single linked record, not all

Solved
Jump to Solution
3334 2
cancel
Showing results for 
Search instead for 
Did you mean: 
Justin_Eppley
6 - Interface Innovator
6 - Interface Innovator

I have a table of products that includes a linked record field to a “tags” table. Each product can have multiple tags. I want to create an automation that auto applies one of the tags, called “New Product”, to the product and auto removes the same tag at a later date but does not remove all the other tags in the field.

I was able to get the first part to work wonderfully – I have a field that is the last modified date and I setup an automation to apply the “New Product” tag to the the product if the item enters a view and the last modified date was within the last 30 days. But, I can’t figure out how to remove the tag without also removing every other tag in the field.

Any thoughts or threads of wisdom?

Thanks,
Justin

1 Solution

Accepted Solutions

I remapped my base, then copied in your code (and changed double quotes to single) - and it worked as expected.

You might need to add some debugging to the code for various variables, such as

console.log(removeProductTagRecord )

And also check the Run History of the Automation to see if it’s triggering and working (without doing anything) or triggering and failing (with error).

I would hazard a guess, that the only reason why it’s not working, is that you’re using a different trigger method of waiting 30 days - so I’m assuming you’re using a Grid View with a filter, along with the trigger “When a record enters a view”? And if so, just double check its setup - and be warned, Views can be easily modified, and I’ve become more accustomed to not using View config within scripting.

See Solution in Thread

7 Replies 7
Zack_S
8 - Airtable Astronomer
8 - Airtable Astronomer

Can’t think of a way to do this directly without using the scripting extension. If you have a paid plan you can run a script from an automation.

@Zack_S Thanks. I’m not the best scripter in the world but it’s good to hear some validation that it doesn’t look possible outside a script. I’ll let you know how it goes.

Yo, I think I have this one solved for you.

We have a Product Table
image

And a Tags Table;
image

I wrote two Automations to manage the Linked “Tags” field.

The first Automation adds the “New Product” tag to any newly created record in the Product Table - fairly straight forward to do with no-code Automation.

image

And then finally, we have another Automation with the trigger “When a record is updated” that monitors the “Tag” field in the Products table - where a script is executed upon a change being detected.

image

Below is my script, which looks to be working as hoped in my brief testing;


//Get the record with the "Tag" field change
const inputConfig = input.config();
const productsTable = base.getTable("Products");
const myRecord = await productsTable.selectRecordAsync(inputConfig.recordId);

//Get the recordID for the "New Product" Tag
const tagsTable = base.getTable("Tags");
const tagRecords = await tagsTable.selectRecordsAsync({fields: ["Name"]});
let [newProductTagRecord] = tagRecords.records.filter( record => (record.name === "New Product"));

//Logic for if the Tags Field is empty, in this case "null"
if (myRecord?.getCellValue("Tags") == null) {
    await productsTable.updateRecordAsync(inputConfig.recordId, {
        "Tags" : [{id: newProductTagRecord?.id}]
    })
} ;

//Logic for if the Tags Field is 2 or more tags, then remove the "New Product" tag.
if (myRecord.getCellValue("Tags").length >= 2) {
    
    let removeProductTagRecord = myRecord.getCellValue("Tags").filter( record => (record.name != "New Product"));
        
    await productsTable.updateRecordAsync(inputConfig.recordId, {
        "Tags" : removeProductTagRecord
    })
};

@Karlstens This looks great! I think this does achieve what we’re trying to do. I’m just not getting it to work in my environment – I’ve not really used scripting much to this point.

In my case:
-the products table name is “Product Catalog Reference”
-the tags table name is “Official Tags Table”
-the field that houses the tags on the product table record is called “Product Tags”
-instead of using the watching field, I am using a trigger of when a particular fields date is greater than 30 days
-in the input config area I added the record Id and labeled as you have in your script and I added the “Product Tags” field as well.

Finally, here is how I amended your code to try to fit my environment:

//Get the record with the “Tag” field change
const inputConfig = input.config();
const productsTable = base.getTable(“Product Catalog Reference”);
const myRecord = await productsTable.selectRecordAsync(inputConfig.recordId);

//Get the recordID for the “New Product” Tag
const tagsTable = base.getTable(“Official Tags Table”);
const tagRecords = await tagsTable.selectRecordsAsync({fields: [“Name”]});
let [newProductTagRecord] = tagRecords.records.filter( record => (record.name === “New Product”));

//Logic for if the Tags Field is empty, in this case “null”
if (myRecord?.getCellValue(“Product Tags”) == null) {
await productsTable.updateRecordAsync(inputConfig.recordId, {
“Product Tags” : [{id: newProductTagRecord?.id}]
})
} ;

//Logic for if the Tags Field is 2 or more tags, then remove the “New Product” tag.
if (myRecord.getCellValue(“Product Tags”).length >= 2) {

let removeProductTagRecord = myRecord.getCellValue("Product Tags").filter( record => (record.name != "New Product"));
    
await productsTable.updateRecordAsync(inputConfig.recordId, {
    "Product Tags" : removeProductTagRecord
})

};

It runs “successfully” but when I test it, it does not remove the tag. Where have I gone wrong?

Thanks SO much for your help.

I remapped my base, then copied in your code (and changed double quotes to single) - and it worked as expected.

You might need to add some debugging to the code for various variables, such as

console.log(removeProductTagRecord )

And also check the Run History of the Automation to see if it’s triggering and working (without doing anything) or triggering and failing (with error).

I would hazard a guess, that the only reason why it’s not working, is that you’re using a different trigger method of waiting 30 days - so I’m assuming you’re using a Grid View with a filter, along with the trigger “When a record enters a view”? And if so, just double check its setup - and be warned, Views can be easily modified, and I’ve become more accustomed to not using View config within scripting.

@Karlstens Very sorry for the super slow response.  Finally got around to re-implementing and this did work just as advertised.  Thanks and super helpful in helping me learn exactly what happening in the script too.  

No problem. One further technique I tend to do for all my scripting is "destructuring" of the input.config() object, for direct creation of the variables that carry the input values.

So where you see this.

const inputConfig = input.config();
const myRecord = await productsTable.selectRecordAsync(inputConfig.recordId);

It's better to do this instead.

const {recordId} = input.config();
const myRecord = await productsTable.selectRecordAsync(recordId);

If you had multiple inputs, you can destructure them as such;

const {recordId, anotherInput, andAnotherInput} = input.config();

console.log(recordId);
console.log(anotherInput);
console.log(andAnotherInput);