Skip to main content
Solved

✅ updateRecordAsync -> must be hardcoded?

  • February 19, 2022
  • 2 replies
  • 31 views

Forum|alt.badge.img

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?

Best answer by kuovonne

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})

2 replies

JonathanBowen
Forum|alt.badge.img+18
  • Inspiring
  • 1110 replies
  • February 19, 2022

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
Forum|alt.badge.img+29
  • Brainy
  • 6009 replies
  • Answer
  • February 19, 2022

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})