Help

Re: Warning when running a script from a different table

1982 0
cancel
Showing results for 
Search instead for 
Did you mean: 
Jim_Webster
4 - Data Explorer
4 - Data Explorer

I have a button that runs a script to select a record in a different table and change a field in that record. Whenever i run it I get a warning: Can’t use record from button field
This script is set up to use records in the “Farm Details” table, but was opened from a different table.
Is it possible to prevent this warning or edit it in some way as i know the button is running from a different table.
Thanks very much!

6 Replies 6

The Scripting block applies the record associated from a button click to whatever the first rendered record input is. That means if you’re triggering a script from the “wrong” table, and you don’t want the evaluated table to change dynamically (i.e. always look at the “Farm Details” table) you need to insert a superfluous record input at the start to conditionally appear whenever you launch the script from the wrong table.

let desiredTable = base.getTable("Farm Table")
let activeTable = base.getTable(cursor.activeTableId)

if (activeTable.id !== desiredTable.id) {
    let triggerRecord = await input.recordAsync("Record from wrong table that triggered this script", activeTable)
}

let record = await input.recordAsync("Select a record", desiredTable)

or

let desiredTable = base.getTable("Farm Table")
let activeTable = base.getTable(cursor.activeTableId)

let record = await input.recordAsync("Select a record", activeTable)

if (activeTable.id !== desiredTable.id) {
    record = await input.recordAsync(`Reselect record, pick from the ${desiredTable.name} table`, desiredTable)
}

Thanks Kamille, that doesn’t seem to work as it always asks twice for a record - I’m always triggering the script from the wrong table - I made a separate table which has the buttons on it as they are not needed in each record This was the original script

// Update a record in the Farm Details table
let table = base.getTable(“Farm Details”);
let query = await table.selectRecordsAsync();

let recordId = await input.recordAsync(‘Select a farm to change the farm name’, table);

let name = await input.textAsync(‘What is the new farm name?’);

await table.updateRecordAsync(recordId, {
“Farm Name”: name ,
})

console.log(“Farm Name has been updated!”);

If I haven’t misread this: just remove the individual awaits, then declare a single Promise.race method bundling them all together (and await that one, of course) at a point where you actually need some input to proceed.

The syntax would be something like const raceWinner = await Promise.race([promise1ref,promise2ref,etc])

EDIT: Oh, and while not immediately relevant here, sometimes selective source table filtering is the optimal behavior:

const currentTable = () => base.getTable(cursor.activeTableId).name;

if(['Table 1','Table 2'].includes(currentTable()));

const cr = await input.recordAsync('',base.getTable(currentTable()));
output.clear()
output.markdown(`## Script called by record ${
    cr.name} (${
        cr.id}) from table "${
        currentTable()}"`
            );

So, this would allow using calling records from buttion presses in Table 1 and Table 2, but not from any other. Even if those hypothetical others each had a button set up to activate the exact script hosting this code.

That’s important information. I was under the impression that the scripts are sometimes called from the correct table like normal. You could follow Dominik’s example or simply adjust either of my examples by adding a output.clear() to the top (inside) of the if() statement. One way or another

I am fairly certain that ^that behavior can’t be changed, so if you’re triggering the script from the wrong table you have to ask for the record twice. output.clear() makes this an “out of sight, out of mind” deal.

Jim_Webster
4 - Data Explorer
4 - Data Explorer

Thank you very much! those helpful suggestions got me through to a solution
Regards
Jim