Help

Re: getCellValueAsAString

Solved
Jump to Solution
2380 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Neil_Thorman
6 - Interface Innovator
6 - Interface Innovator

Hello community - I’m a week on form ‘Hello World’ as far as scripting (and coding in general) is concerned so apologies if this is basic - one of my issues is knowing correct terms and without them it’s hard to search for answers.
I’m creating a script app that adds a record to a table called ‘Characters’.
The new record will incorporate some info from other records on the same table so to feel my way I’ve done:

dynastyNow = record.getCellValueAsString(“Dynasty”)

motherNow = record.getCellValueAsString(“Name”)

fatherNow = record.getCellValue(“Spouse”)

countryNow = record.getCellValueAsString(“Country”)

output.text(dynastyNow)

output.text(motherNow)

output.text(fatherNow)

output.text(countryNow)

It all works as expected except fatherNow - which returns: nothing if I use getCellValueAsString and ‘null’ if I use getCellValue.
It must be because the spouse field is a linked record field (the others are either single selects or single line texts- how do I interogate the linked record to produce the info I need?

1 Solution

Accepted Solutions
Justin_Barrett
18 - Pluto
18 - Pluto

Your latest screenshot helps a ton. Your earlier description made it sound like you weren’t getting anything, but you’re actually getting the correct data for that field when using getCellValue, which is an array of objects. If you want the literal text of that linked record, use “getCellValueAsString” and you’ll see “Henri”

See Solution in Thread

16 Replies 16

How exactly are you accomplishing that part? Your code fragment and explanation only tells part of the story. How did you go about getting the data for the record tied to the “record” variable? Are you 100% certain that you know which record it’s retrieving? Also, is this occurring in a script in the Scripting app, or as part of an automation? In general, more context would be very helpful.

Congratulations on beginning your coding journey!

It sounds like your record does not have a value for that field.

How are you obtaining the record? If you are using selectRecordAsync are you including a list of fields?

Neil_Thorman
6 - Interface Innovator
6 - Interface Innovator

Thanks guys - so this is a bit more code and context.
I’ve got two bits of code so far:

// Create a record in the Characters table
let table = base.getTable(“Characters”);

let rollConst = Math.floor(Math.random() *5) - 2
let rollCharisma = Math.floor(Math.random() *5) -2
let rollStren = Math.floor(Math.random() *5) -2

let sexRoll = Math.floor(Math.random() * 2) + 1
output.text(sexRoll)

if (sexRoll == 1) {
output.text(“It’s a girl”)
}
else {
output.text(“It’s a boy”)
}
let babyName = await input.textAsync(’ Name them!’)
let birthYear = await input.textAsync(‘What year is it?’)
//Integer numBirthYear = Integer.valueOf(birthYear)

let recordId = await table.createRecordAsync({
“Name”: babyName,
“Constitution”: rollConst,
“Charisma”: rollCharisma,
“Strength/Guile”: rollStren,
“Year of Birth”: Number(birthYear)
});

if (sexRoll == 1) {
table.updateRecordAsync(recordId, {“Sex” : {name: “Female”}})
}
else {
table.updateRecordAsync(recordId, {“Sex” : {name: “Male”}})
}
table.updateRecordAsync(recordId, {“Marital Status” : {name: “Single”}})
table.updateRecordAsync(recordId, {“Deceased” : {name: “Alive”}})

Above for me was proof of concept ie ‘so I can create a record and begin to populate it.’
I’ve also (seperately currentyl) the below:

let table = base.getTable(“Characters”)

let record = await input.recordAsync(“Choose a Lady”, table);

let roll = (Math.floor(Math.random() *5)+1) + (Math.floor(Math.random() *5)+1)

if (roll > 6) {

output.text(“Congratulations”)

}

else {

output.text(“Not this time”)

}

dynastyNow = record.getCellValueAsString(“Dynasty”)

motherNow = record.getCellValueAsString(“Name”)

fatherNow = record.getCellValue(“Spouse”)

countryNow = record.getCellValueAsString(“Country”)

output.text(dynastyNow)

output.text(motherNow)

output.text(fatherNow)

output.text(countryNow)

This latter is triggered by a button feild in an existing record and checks to see if (in this use case) a new character record should be created. I’ve shown myself I can read information out of the clicked record and assign it to a varible - excpet I can’t in the “Spouse” field. I assume because it is a ‘link to another record’ fiedl type. Ultimately, I’ll combine these two bits of code so that clicking the button checks to see if a new record shoudl be created; if it shold be then it’ll read some data from the clicked record and write it into the created record along with some randomy generated character stats (yea its for a game :grinning: ) Thanks again

Hi Justin, your specific Q’s. It’s a scripting app. And re the record - I think in my first code chunk the reocrd I’m working with is the newly created one? In the second fragment - it’s an existing record
ie the one contianing the button that was clicked on to intiate the app? But this might be the problem - to read a sting into fatherNow from the field “spouse” I need to access the record linked to in the spouse feild I think - but how?

Neil_Thorman
6 - Interface Innovator
6 - Interface Innovator

Thinking it through my Q becomes: "how do I read the record Id of the record in a linked record feild’

Neil_Thorman
6 - Interface Innovator
6 - Interface Innovator

Leading me here:

and leaving me not understanding the comment

“//have to use Id’s when using linked records”

But the trail is cold again at this point.

You refer to two different code fragments, but I only see one code fragment in your initial post.

Screen captures of the relevant fields would also help.

Neil_Thorman
6 - Interface Innovator
6 - Interface Innovator

Apologies this is the first snippet. I think I’m on the right lines regarding being able to read from my table - except the "Spouse’ field. I’ll post the second snippet seperately. In the screenshot below - when I click on the button in Anna’s record I’d like output.text(fatherNow) to display the word “Lothar”

Screenshot 2022-02-02 at 13.56.25

let table = base.getTable(“Characters”)

let record = await input.recordAsync(“Choose a Lady”, table);

let roll = (Math.floor(Math.random() *5)+1) + (Math.floor(Math.random() *5)+1)

if (roll > 6) {

output.text(“Congratulations”)

}

else {

output.text(“Not this time”)

}

dynastyNow = record.getCellValueAsString(“Dynasty”)

motherNow = record.getCellValueAsString(“Name”)

fatherNow = record.getCellValue(“Spouse”)

countryNow = record.getCellValueAsString(“Country”)

output.text(dynastyNow)

output.text(motherNow)

output.text(fatherNow)

output.text(countryNow)

This is the second bit. I’ve writtent them seperately till now so I can spot errors but the idea is to bring them together such that a click in a feild will determine if a new reocrd should be created and then create it with information read form the ‘clicked on’ record.

// Create a record in the Characters table
let table = base.getTable(“Characters”);

let rollConst = Math.floor(Math.random() *5) - 2
let rollCharisma = Math.floor(Math.random() *5) -2
let rollStren = Math.floor(Math.random() *5) -2

let sexRoll = Math.floor(Math.random() * 2) + 1
output.text(sexRoll)

if (sexRoll == 1) {
output.text(“It’s a girl”)
}
else {
output.text(“It’s a boy”)
}
let babyName = await input.textAsync(’ Name them!’)
let birthYear = await input.textAsync(‘What year is it?’)
//Integer numBirthYear = Integer.valueOf(birthYear)

let recordId = await table.createRecordAsync({
“Name”: babyName,
“Constitution”: rollConst,
“Charisma”: rollCharisma,
“Strength/Guile”: rollStren,
“Year of Birth”: Number(birthYear)
});

if (sexRoll == 1) {
table.updateRecordAsync(recordId, {“Sex” : {name: “Female”}})
}
else {
table.updateRecordAsync(recordId, {“Sex” : {name: “Male”}})
}
table.updateRecordAsync(recordId, {“Marital Status” : {name: “Single”}})
table.updateRecordAsync(recordId, {“Deceased” : {name: “Alive”}})

console.log(“Created a record!”);

The screen capture shows that there is no record where both {Mother} and {Spouse} have a value. Thus, it make sense that scripting is showing a value for {Mother}, but no value for {Spouse}.

Do you want to create a new character where the selected record will become the {Mother} of the new character, and the {Spouse} of the selected record will become the {Father}? If so, that requires very different logic from what your code currently does.

While you might need to learn the difference between getCellValue versus getCellValueAsString, you have a bigger issue in the logic of how your code works versus what you want to do.

Neil_Thorman
6 - Interface Innovator
6 - Interface Innovator

Am I confusing the issue between father and spouse? I want to read the contnets of the spouse feild into the variable fatherNow

I’ve slightyl altered the output code for clarity

dynastyNow = record.getCellValueAsString(“Dynasty”)
motherNow = record.getCellValueAsString(“Name”)
fatherNow = record.getCellValue(“Spouse”)
countryNow = record.getCellValueAsString(“Country”)

output.text(Dynasty, ${dynastyNow}.);
output.text(Mother, ${motherNow}.);
output.text(Father, ${fatherNow}.);
output.text(Country, ${countryNow}.);

And a clearer screenshot

Screenshot 2022-02-02 at 14.56.51

When this is run on the record Dacry I dont understadn why fatherNow doesn’t end up containing ‘Henri’ from Darci’s spouse feild?

Justin_Barrett
18 - Pluto
18 - Pluto

Your latest screenshot helps a ton. Your earlier description made it sound like you weren’t getting anything, but you’re actually getting the correct data for that field when using getCellValue, which is an array of objects. If you want the literal text of that linked record, use “getCellValueAsString” and you’ll see “Henri”

To clarify a bit more, when using getCellValue, you’re asking for the literal value stored for a field. For a single line or long text field, that’s already going to be text. For a linked record field, though, it’s going to be an array of objects, with each object containing the ID of a linked record; e.g. {id: "recXXXXXXXXXXXXXX"}. If you use output.text to display an object, though, all you’ll get is object Object for each one (the surrounding square braces are the array structure).

If the stored data isn’t already a string (as in this case) and you want to use/display it as a string, then you should use getCellValueAsString.

For more on the type of data returned for each field type, open the “API” section of the docs at the bottom of the script editor and read through the “Cell values and field options” section, or go here to open it separately:

Oh man oh man, why did I not use AsAString in that line - doh. Mnay thanks both of you.

Erm just for future - is there a nicer way to copy code into a post than the way I have been?

After pasting your code into the post editor, select it, then hit the button on the editor toolbar that looks like this: </> . That formats it as preformatted text, which displays more cleanly and is also safer when copying/pasting.