Help

Loading all fields and values

Topic Labels: Scripting
Solved
Jump to Solution
2313 2
cancel
Showing results for 
Search instead for 
Did you mean: 
Matt_Lister
5 - Automation Enthusiast
5 - Automation Enthusiast

Hi all

I've just got into scripting, and have fallen at the first hurdle.

 
I thought this would load an array of all my records as objects, with each field in the object with its value as a a key value pair.
 
let table = base.getTable("Contacts");
let query = await table.selectRecordsAsync({fields: table.fields});
console.log(query.records);
 
However, it only seems to load the record id and record name. All other fields do not seem to be within the object.
I have tried specifying the exact fields in {fields: table.fields}, and then console.log(query.records[fieldname]), but this also returns undefined.
 
I'm coming from Python to Javascript, and I'm not a proffessional. Am I doing something dumb?
 
Thanks in advance.
1 Solution

Accepted Solutions
Matt_Lister
5 - Automation Enthusiast
5 - Automation Enthusiast

To answer my own question for anyone else struggling to work it out. getCellValueAsString() was the solution for me. I don't know if this is the best way of doing it. But this is how you can get whatever field values you need.

I'm printing them here. But you could instead, add each recordDetails to an array.

(disclaimer, I'm not an expert)

 

let table = base.getTable("Contacts");
let query = await table.selectRecordsAsync({fields: table.fields});

let loadedData = (query.records);

for (let record of loadedData){
const recordDetails = {
fullName: (record.getCellValueAsString("Full Name")),
lastName: (record.getCellValueAsString("Last Name")),
contactType: (record.getCellValueAsString("Contact Type"))
}
console.log(recordDetails)

}

Additionally I believe it is preferable to only load the fields you need, to save memory. So you should replace line 2 this code to something like:

 

let query = await table.selectRecordsAsync({fields: ["Full Name","Last Name","Contact Type"]});
 
So as not to load all the fields in the table.

See Solution in Thread

2 Replies 2
Matt_Lister
5 - Automation Enthusiast
5 - Automation Enthusiast

To answer my own question for anyone else struggling to work it out. getCellValueAsString() was the solution for me. I don't know if this is the best way of doing it. But this is how you can get whatever field values you need.

I'm printing them here. But you could instead, add each recordDetails to an array.

(disclaimer, I'm not an expert)

 

let table = base.getTable("Contacts");
let query = await table.selectRecordsAsync({fields: table.fields});

let loadedData = (query.records);

for (let record of loadedData){
const recordDetails = {
fullName: (record.getCellValueAsString("Full Name")),
lastName: (record.getCellValueAsString("Last Name")),
contactType: (record.getCellValueAsString("Contact Type"))
}
console.log(recordDetails)

}

Additionally I believe it is preferable to only load the fields you need, to save memory. So you should replace line 2 this code to something like:

 

let query = await table.selectRecordsAsync({fields: ["Full Name","Last Name","Contact Type"]});
 
So as not to load all the fields in the table.

Yah, that looks correct.

One tip - I prefer to use .map() .filter() and .reduce() instead of for loops.

I *think* my updated code below should work for you, but you might want to edit/remove the .filter() as it will filter out any non-matched records. I highly doubt any of your records have the name "An example filter string" 😜

let table = base.getTable("Contacts");
let query = await table.selectRecordsAsync({
	fields: ["Full Name", "Last Name", "Contact Type"]
});

let recordDetails = query.records
	.filter((record) => record.name === "An example filter string")
	.map((record) => ({
		fullName: record.getCellValueAsString("Full Name"),
		lastName: record.getCellValueAsString("Last Name"),
		contactType: record.getCellValueAsString("Contact Type")
	}));

console.log(recordDetails)