Without the tables and knowing what you want to achieve with the functions, it is hard to help.
Without the tables and knowing what you want to achieve with the functions, it is hard to help.
How can I share my tables?
basically this script is enabling me to take data from my master table whilst reading the truck numbers and customer names.
After which the script only pulls unique values from the truck and customer fields.
Then uses each unique value to iterate through the master table and adds up all the expenses, revenues and receivables related to each unique value from the customer and truck no records and displays them in respective tables namely “truck breakdown” and “Customer breakdown”
I found one error
async function addNewToCurrent(newVal, typeOf, tableNumber)
{
console.log(“addNew function new val and size”, newVal, newVal.length);
the for loop is only running once, even though the newVal is 5.
Not sure why that loop isn’t working. The structure looks solid, so the only other reason that I can think of would be that there’s only one item in the array. That aside, there are some things to fix/optimize here.
First, there’s an easier way to create the new records by looping directly through the array instead of using the indexing method:
async function addNewToCurrent(newVal, typeOf, tableNumber) {
That said, this optimization still contains another flaw. It looks like you want to use the values of the truck and customer variables as field names, passing them through to this function using the variable typeOf. The problem is that by using the literal name typeOf, JavaScript thinks that the field name is typeOf. To use the value of the variable as the field name, wrap the variable name in square braces. This version fixes that problem:
async function addNewToCurrent(newVal, typeOf, tableNumber) {
With that done, there’s still one more way to optimize this. Instead of waiting for each record to be created individually, store the new record data as an array of objects and create the records as a batch using createRecordsAsync.
async function addNewToCurrent(newVal, typeOf, tableNumber) {
while (newRecords.length) await tableNumber.createRecordsAsync(newRecords.splice(0, 50))
}
Not sure why that loop isn’t working. The structure looks solid, so the only other reason that I can think of would be that there’s only one item in the array. That aside, there are some things to fix/optimize here.
First, there’s an easier way to create the new records by looping directly through the array instead of using the indexing method:
async function addNewToCurrent(newVal, typeOf, tableNumber) {
That said, this optimization still contains another flaw. It looks like you want to use the values of the truck and customer variables as field names, passing them through to this function using the variable typeOf. The problem is that by using the literal name typeOf, JavaScript thinks that the field name is typeOf. To use the value of the variable as the field name, wrap the variable name in square braces. This version fixes that problem:
async function addNewToCurrent(newVal, typeOf, tableNumber) {
With that done, there’s still one more way to optimize this. Instead of waiting for each record to be created individually, store the new record data as an array of objects and create the records as a batch using createRecordsAsync.
async function addNewToCurrent(newVal, typeOf, tableNumber) {
while (newRecords.length) await tableNumber.createRecordsAsync(newRecords.splice(0, 50))
}
Thank you for you input Justin.
I never had any formal training in Java and your optimization method helps a lot!
The map function seems overwhelming but the for(let item of newVal) method is what I will be using going forward.
Furthermore, I did end up fixing the error with the help of airtable support.
The loop suddenly started working, so no idea what happened there. But the output of data wasn’t displaying as I had to wrap typeOf in brackets y typeOf ] to use it as a computed property name.
Appreciate your help!
Thank you for you input Justin.
I never had any formal training in Java and your optimization method helps a lot!
The map function seems overwhelming but the for(let item of newVal) method is what I will be using going forward.
Furthermore, I did end up fixing the error with the help of airtable support.
The loop suddenly started working, so no idea what happened there. But the output of data wasn’t displaying as I had to wrap typeOf in brackets s typeOf ] to use it as a computed property name.
Appreciate your help!
To clarify, Airtable uses JavaScript, not Java. While the two languages both contain the word “Java”, that’s their only commonality. JavaScript is not derived from Java.