Save the date! Join us on October 16 for our Product Ops launch event. Register here.
Feb 01, 2022 02:26 PM
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?
Solved! Go to Solution.
Feb 02, 2022 06:19 AM
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.
Feb 02, 2022 06:58 AM
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
When this is run on the record Dacry I dont understadn why fatherNow doesn’t end up containing ‘Henri’ from Darci’s spouse feild?
Feb 02, 2022 07:20 AM
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”
Feb 02, 2022 07:31 AM
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:
Feb 02, 2022 07:46 AM
Oh man oh man, why did I not use AsAString in that line - doh. Mnay thanks both of you.
Feb 02, 2022 08:37 AM
Erm just for future - is there a nicer way to copy code into a post than the way I have been?
Feb 02, 2022 03:08 PM
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.