Collection would be one way of setting defaults on a new form but the timing would be tricky; even if you update the collection right after save, loading collection happens asynchronously, so there is no way to guarantee that the collection contains the new value before the form reloads.
The clean way, would be to store/retrieve the values with frontend functions, using the value store. You would need 2 functions:
- One function is executed before
form record saved
, storing the field values in the value store (value_store_set()
).
- One function is executed after
form loaded
, getting the previous field values from the value store (value_store_get()
) and applying it to the target fields. Because this function is always executed when the form opens, make sure that the values from the store are actually set (eg. not undefined).
I cannot think of an example at the top of my head - but the value store is pretty easy to use. Just make sure to read the context help. To test it out, you can always send output to the browser console with console.log()
.