Dec 06, 2020 10:22 AM
Hey I’m trying to update a few fields on a record. Same process each time. So I have a for loop that goes through an array with a combo of the field I want to update and the values to put in.
A basic version looks like this:
Array = [{“Field”:“field 1”, “value 1”: “something”}, {“Field”:“field 2”, “value 1”: “something”}]
For item in array do:
Some processing
Table.updateRecordAsync(RecordID, {"${variable string with field name}"
: another variable value})
However I can’t get the field
"${variable string with field name}"
to take a variable no matter how I set it up. I have tried every combo of ’ " { [ around the variable. And tried constructing the variable separate as a full string to put in but to no avail.
Is it possible to use a variable in the field?
How could I do this?
Many thanks.
Dec 06, 2020 10:25 AM
I predict @kuovonne will beat me to answering this question so I think I’ll just have another bite of my cinnamini. :winking_face:
(Honestly, I spent more time thinking about how to spell “cinnamini” than I did on the question.)
Dec 06, 2020 10:25 AM
You need to use a backtic character ` instead of a single or double quotes.
Dec 06, 2020 10:44 AM
Really? So you decide to not answer at all? Even though you did answer first?
I just happened to decide to check the forums because I needed a break from sewing. I’m on a tiny 4” phone screen on a phone that was obsolete when I got it used five years ago. Typing the whole line of code is too hard and I don’t even have a real keyboard in front of me so I can’t say where the key is located. And the screen is too small to see a preview of what I type so I wasn’t sure if I should put backtics around my backtic to make it show up in the code font.
Enjoy your cinnamini, which I had to type multiple times to get autocorrect to stop autocorrecting. :crazy_face:
P.S. thanks for the laugh and the excuse to let me rant about the difficulties in posting from my phone. I haven’t decided if I should invest in a newer phone or stop checking the forums so often.
Dec 06, 2020 11:03 AM
Shoot now that I re-read the question, it looks like you are trying to use a variable as an object key, not insert a variable in a string. In that case, try something like this:
Table.updateRecordAsync(RecordID, { [variableForFieldName] : variableForValue})
Since the variable holds a string, you need to put the variable in square brackets to use the value of the variable as the field name.
The ${variable}
text distracted me from your real issue, leading me down an different mental path. When you use ${}
syntax, you have to use backtics.
Apologies for the briefness of the reply. I’m on a small phone screen and just taking a break from sewing. It is all to easy to grasp at one small part of the issue and not see the big picture.
Dec 06, 2020 12:50 PM
I recommend both. [additional random characters to make a post]
Dec 06, 2020 06:54 PM
thank you @kuovonne [ ] are the answer. best of luck with the sewing.
Feb 11, 2021 06:13 AM
Thanks for this! I had the same issue in my base and this answer is very helpful.
One thing I don’t understand, and it comes from my lack of knowledge about JavaScript -
Why do you have to say
Table.updateRecordAsync(RecordID, { [variableForFieldName] : variableForValue})
instead of
Table.updateRecordAsync(RecordID, { [variableForFieldName] : [variableForValue]})
I personally am not understanding the reason/necessity of the brackets around the one variable but not the other.
Feb 11, 2021 09:46 AM
This is because one situations is where you are using the variable as an object key, and the other situation you are using just a regular variable value.
This thread has a longer discussion of the issue.
Feb 11, 2021 12:06 PM
Thanks. That separate thread did help a lot! Especially this part:
I suppose it would be easier if the square brackets weren’t necessary, but then it would be impossible to tell if the key were an actual name, or a variable because
let myFields = {fieldName: "Value..."};
at runtime becomes
let myFields = {"fieldName": "Value..."};