Feb 18, 2022 11:23 PM
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?
Solved! Go to Solution.
Feb 19, 2022 06:33 AM
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})
Feb 19, 2022 04:30 AM
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)
}
Feb 19, 2022 06:33 AM
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})