I have a backend function that finds the max value in a field.
$BODY$
DECLARE
BEGIN
RETURN (SELECT MAX((po_expenses.po_log.po_number)) AS po
FROM {po_expenses}.[po_log]);
END;
$BODY$
I then have a frontend function that assigns that value to a global variable as well as a second variable incremented by one. It took me a bit to figure out how to pass that value to the global variables due to being a JS novice and not understanding Promises, but I worked it out. I have this function set to run before form load.
`function ReturnPromise() {
return app.call_backend({po_expenses.po_get_last_number});
}
async function ResolvePromise() {
const valueFromPromise = await ReturnPromise();
return valueFromPromise;
}
async function main() {
let current_po = await ResolvePromise();
app.set_variable({po_expenses.var_po_previous_number}, current_po);
app.set_variable({po_expenses.var_po_next_number}, current_po + 1)
}
main();`
I have a second function that is set to run after the form loads that assigns the value from the var_po_next_number variable to the po_number field on the form.
//Assign PO Number
app.set_field_value({F12::0 po_log.po_number}, app.get_variable({po_expenses.var_po_next_number}), true);
The issue is, most of the time I open the form it shows the last value that was stored in the variable. If I click new it updates to the new correct value. So I think that perhaps the backend call is not resolving before the form loads. The documentations seems to suggest that the "Before load" function should complete, then the form loads, then the "After load" function should run. Somehow the cart is getting before the horse. Is there a way to ensure that one function completes before the next is called?
Additional context: The po_number value is not unique, as multiple records can share the same PO allowing them to serve as the ref number needed to signify a split transaction for later import into QuickBooks. Otherwise, I would have just used the system generated id number.
Thanks.