Help

Using automation to update Link to "Field"

Solved
Jump to Solution
147 2
cancel
Showing results for 
Search instead for 
Did you mean: 
HarryWard
4 - Data Explorer
4 - Data Explorer

Hi All,

I'm having difficulty automating a task that I need for my database. Essentially what I'm trying to do is to have a default value in the "Link to Field" selection, which is then removed when another option is added. 

The data is currently pulled in from another table using a 'Link to Field' column, I currently have the automation working to add default option back when options are removed but having difficulty creating the script to remove the default option when a second option is added.

Below is my current code, which pulls the current options from the cell and checks whether they are the same as the field being updated, which then passes those choices to an updateOptionsAsync, from which my understanding is the wrong function to be calling.

Any help would be greatly appreciated!!

 

let inputConfig = input.config();
let currentRecord = (inputConfig.recordID);
let table = base.getTable("Deals");
let field = table.getField("Properties");
let queryResult = await table.selectRecordsAsync({fields: ["Properties"]});
let results = queryResult.records;

results.forEach(record => {
if (record.id == currentRecord) {
let selections = record.getCellValue("Properties");
propertiesCleaner(selections);
}
});

async function propertiesCleaner(choices) {
console.log(choices);
console.log(field);
console.log(field.options.choices);
await field.updateOptionsAsync(
{ choices: [...field.options.choices.filter((choice) => choice.name !== "Default Property ")] },
{ enableSelectFieldChoiceDeletion: true }
);
};
1 Solution

Accepted Solutions

Thanks for this, I ended up having to change a few things, but for anyone else who might find this useful here is my final code:

//Get the record with the "Tag" field change

const inputConfig = input.config();

const productsTable = base.getTable("Deals");

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

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

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

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


I will say the documentation around this sort of stuff is very limited and from my POV, poorly documented where there is information, took a lot of googling and chatGPT to help me get to the final solution

See Solution in Thread

2 Replies 2
TheTimeSavingCo
18 - Pluto
18 - Pluto

Try this:

 

let TABLE_NAME = "Deals"
let LINKED_FIELD_NAME = "Properties"
let DEFAULT_PROPERTY_NAME = "Default Property"

let {recordID} = input.config();
let table = base.getTable(TABLE_NAME);
let recordData = await table.selectRecordAsync(recordID, { fields: [LINKED_FIELD_NAME] });

let selections = recordData.getCellValue(LINKED_FIELD_NAME);

await propertiesCleaner(selections);

async function propertiesCleaner(choices) {
    let filteredChoices = choices.filter(choice => choice.name !== DEFAULT_PROPERTY_NAME);
    await table.updateRecordAsync(recordID,{
        [LINKED_FIELD_NAME]: filteredChoices
    })
}

 

I also suggest checking out the docs for more information on what functions to use:
https://airtable.com/developers/scripting

Thanks for this, I ended up having to change a few things, but for anyone else who might find this useful here is my final code:

//Get the record with the "Tag" field change

const inputConfig = input.config();

const productsTable = base.getTable("Deals");

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

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

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

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


I will say the documentation around this sort of stuff is very limited and from my POV, poorly documented where there is information, took a lot of googling and chatGPT to help me get to the final solution