Help

✅ updateRecordAsync -> must be hardcoded?

Topic Labels: Custom Extensions
Solved
Jump to Solution
2173 2
cancel
Showing results for 
Search instead for 
Did you mean: 
LooselySutble
4 - Data Explorer
4 - Data Explorer

It seems I can’t use a variable in the method

updateRecordAsync(record,{"field": value)

In the head of my script, I ask which field needs to be updated.
(ps: I use pseudo-code here to keep my question brief. Everything works, except the last line)

input.config({
        input.config.field('selectedField', {
            label: 'Which field do you want to update?',
            parentTable: 'selectedTable',
        }),
})

Let’s say I choose the field status.
Then I would like to use something like

updateRecordAsync(record,{selectedField: someValue)
or
updateRecordAsync(record,{selectedField.name: someValue)

However, the code above gives an error:

j: Field ‘selectedField’ does not exist in table

The code only works when I hardcode the field, which kinda defeats the purpose of scripting/automation…

 updateRecordAsync(record,{"status": someValue})

question: is there some way to use a variable selectedField in updateRecordAsync or do I need to keep the fieldname hardcoded?

1 Solution

Accepted Solutions
kuovonne
18 - Pluto
18 - Pluto

Since you are using script settings, you get the actual field object. You then put the field name (or ID) in square brackets.

const inputConfig = input.config({
        input.config.field('selectedField', {
            label: 'Which field do you want to update?',
            parentTable: 'selectedTable',
        }),
})

const selectedTable = inputConfig["selectedTable"]
const selectedField = inputConfig["selectedField"]

await selectedTable.updateRecordAsync(record,{[selectedField.name]: someValue})

See Solution in Thread

2 Replies 2

Hi @LooselySutble - you need to use a computed property. So, wrap your field variable name in square brackets, something like this:

let table = base.getTable('Table 5')
let query = await table.selectRecordsAsync({
    fields: ['Name']
})

let fieldToUpdate = 'Name'

//  wrap field name in square brackets
let data = {
    [fieldToUpdate]: 'Bob'
}

console.log(data)

for (let rec of query.records) {
    await table.updateRecordAsync(rec, data)
}
kuovonne
18 - Pluto
18 - Pluto

Since you are using script settings, you get the actual field object. You then put the field name (or ID) in square brackets.

const inputConfig = input.config({
        input.config.field('selectedField', {
            label: 'Which field do you want to update?',
            parentTable: 'selectedTable',
        }),
})

const selectedTable = inputConfig["selectedTable"]
const selectedField = inputConfig["selectedField"]

await selectedTable.updateRecordAsync(record,{[selectedField.name]: someValue})