I have a multi-page app. Let's call page 1 Table and page 2 Form. At the top of the Table page I have a Create button that launches me over to the Form. I am trying to set a variable (call it varCurrentRecord) to my ID from my Retool Database table.
I have tried this 100 ways and just couldn't get varCurrentRecord to populate. I landed on triggering the following JS Query on page load under certain conditions:
console.log("hit script");
// Run `qCreateNewPlan` and wait for results
try {
const plan = await new Promise((resolve, reject) => {
qCreateNewPlan.trigger({
onSuccess: (data) => {
resolve(data);
},
onFailure: (error) => {
console.error("Error triggering qCreateNewPlan:", error);
reject(error);
},
});
});
console.log("Plan result:", plan);
// Check and process the result
if (plan.result && plan.result.length > 0) {
const planId = plan.result[0].id;
console.log("Plan ID:", planId);
// Set `varCurrentRecord` to the plan ID
await varCurrentRecord.setValue(planId);
console.log("varCurrentRecord updated to:", varCurrentRecord.value);
} else {
console.error("No results found in plan.result.");
}
} catch (error) {
console.error("An unknown error occurred:", error);
}
console.log("Script complete.");
Where qCreateNewPlan submits an insert to my database. Please tell me there's an easier way?