Help

Re: Script error when updating records in another table j: Error: Failed hyperAssert: undefined

Solved
Jump to Solution
1358 1
cancel
Showing results for 
Search instead for 
Did you mean: 
stenkate
6 - Interface Innovator
6 - Interface Innovator

I use the script below to retrieve the latest date from all records in table 'personal expenses', and want to insert this value to all records in table 'personal budget' (this table is in the same base).

The script works if I update the records in the same table 'personal expenses', but not when updating the other table 'personal budget'. In that case I receive this error:

 

j: Error: Failed hyperAssert: undefined
    at main on line 33

 

Can someone help me with debugging? I'm in script noob and assume there's a missing link between selecting the records in tablePersonalExpenses and updating the records in tablePersonalBudget. Thanks in advance!

 

// Replace "Table Name" with the name of your table
let tablePersonalExpenses = base.getTable("personal expenses");
let tablePersonalBudget = base.getTable("personal budget");

console.log(tablePersonalExpenses)
console.log(tablePersonalBudget)

// Replace "Date" with the name of the date field you want to find the latest date of
let query = await tablePersonalExpenses.selectRecordsAsync({
    fields: ["Date"],
    sorts: [{field: "Date", direction: "desc"}],
    maxRecords: 1
});

// Get the latest date value
let latestDate = query.records[0].getCellValue("Date");

console.log(latestDate)

// Update the value of field "" in all records with the highest value
let updateRecords = query.records.map(function(record) {
    return {
        id: record.id,
        fields: {

            "Latest Import Date": latestDate
        }
    };
});

// Update records - Only up to 50 updates are allowed at one time, so do it in batches
    while (updateRecords.length > 0) {
        await tablePersonalBudget.updateRecordsAsync(updateRecords.slice(0, 50));
        updateRecords = updateRecords.slice(50);
    }

 

1 Solution

Accepted Solutions
stenkate
6 - Interface Innovator
6 - Interface Innovator

Update: after some trial and error I indeed found out I forgot something: to query the records in the personal budget to update. This is the final code which works:

 

// Select the tables
let tablePersonalExpenses = base.getTable("personal expenses");
let tablePersonalBudget = base.getTable("personal budget");

// Query the records in personal expenses table for the dates and sort them descending
let query = await tablePersonalExpenses.selectRecordsAsync({
    fields: ["Date"],
    sorts: [{field: "Date", direction: "desc"}],
    maxRecords: 1
});

// Get the latest date value (= 1 record in descending sorted order)
let latestDate = query.records[0].getCellValue("Date");

// Print the latest date
console.log("Latest import date: ", latestDate)

// Query the records in personal budget table for the "Latest Import Date" field
let tablePersonalBudgetQuery = await tablePersonalBudget.selectRecordsAsync({
    fields: ["Latest Import Date"]
});

// Update the value of field "Latest Import Data" in all queried records with the highest value
let updateRecords = tablePersonalBudgetQuery.records.map(function(record) {
    return {
        id: record.id,
        fields: {

            "Latest Import Date": latestDate
        }
    };
});

// Update records - Only up to 50 updates are allowed at one time, so do it in batches
    while (updateRecords.length > 0) {
        await tablePersonalBudget.updateRecordsAsync(updateRecords.slice(0, 50));
        updateRecords = updateRecords.slice(50);
      }

 

See Solution in Thread

3 Replies 3
stenkate
6 - Interface Innovator
6 - Interface Innovator

Update: after some trial and error I indeed found out I forgot something: to query the records in the personal budget to update. This is the final code which works:

 

// Select the tables
let tablePersonalExpenses = base.getTable("personal expenses");
let tablePersonalBudget = base.getTable("personal budget");

// Query the records in personal expenses table for the dates and sort them descending
let query = await tablePersonalExpenses.selectRecordsAsync({
    fields: ["Date"],
    sorts: [{field: "Date", direction: "desc"}],
    maxRecords: 1
});

// Get the latest date value (= 1 record in descending sorted order)
let latestDate = query.records[0].getCellValue("Date");

// Print the latest date
console.log("Latest import date: ", latestDate)

// Query the records in personal budget table for the "Latest Import Date" field
let tablePersonalBudgetQuery = await tablePersonalBudget.selectRecordsAsync({
    fields: ["Latest Import Date"]
});

// Update the value of field "Latest Import Data" in all queried records with the highest value
let updateRecords = tablePersonalBudgetQuery.records.map(function(record) {
    return {
        id: record.id,
        fields: {

            "Latest Import Date": latestDate
        }
    };
});

// Update records - Only up to 50 updates are allowed at one time, so do it in batches
    while (updateRecords.length > 0) {
        await tablePersonalBudget.updateRecordsAsync(updateRecords.slice(0, 50));
        updateRecords = updateRecords.slice(50);
      }

 

Thank you for sharing your script writing experience and the solution. 

Out of curiosity, why are you using the maxRecords parameter in your script?

stenkate
6 - Interface Innovator
6 - Interface Innovator

You're welcome and thanks! Regarding your question:


@kuovonne wrote:

Thank you for sharing your script writing experience and the solution. 

Out of curiosity, why are you using the maxRecords parameter in your script?


To be honest this script is a result of various online explorations and copy/pasting, this maxRecords was part of such a paste. Looking at it now, it indeed has no real function for the actual result. Maybe there's a bit value in terms of performance? I've removed it from my script.