Help

getCellValueAsAString

Topic Labels: Extensions
Solved
Jump to Solution
6181 16
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!”);