Closed TechInterMezzo closed 2 months ago
Currently you could only check all relevant inputs via form states, which is not very convenient.
We could add another form state condition, something like "form has any changes". This would return true if any field value has been changed.
Would that address your case?
We could also consider making app.record_save() return a promise, though that would probably take some work. Would that be helpful to you?
Both additions would be great. But "form has any changes" would already help me with my current case. I would remind the user to save the changes and enable the button for the custom function with the new form state. The promise from record_save() could help me to automatically save changes before executing the next step. And I wouldn't have to let the user do it beforehand.
I just checked and unfortunately, adding new form state conditions requires a DB schema change, for which we need a minor release - this would need to wait for R3.9.
The promise would just be frontend and should be doable. I´ll add it to the wish list for R3.8.3.
Besides getting and setting a value of a form field from frontend functions, it could also be useful to check if a value was changed. Or adding a function to check a form state by name.
Checking form states by name would be quite brittle - names can change and then you would have to know that you might have been referencing that name somewhere. States are generally quite hard to mix with event driven code like functions as states are constantly re-evaluated while functions are executed at specific events, which can be very unintuitive to troubleshoot.
To check for a changed field value should be easy enough - I think there is already a request for something like that. I´ll look at it for R3.8.3.
I checked whether we could sensibly make app.record_save() return a promise for the next patch release. Unfortunately, we´d need to redesign some components to make that consistent with other calls, so it cannot happen in the next patch release.
But I do have a solution for your case: Instead of calling a backend function directly after app.record_save(), you would execute the call after the next form reload. A form reload occurs after a save is executed and the record is reloaded - so you can execute your backend call with the next form load event (before/after are both fine in this case). To store your changes between the save and reload, you can use app.value_store_set() / app.value_store_get().
Another update: R3.8.3 will include the discussed app.get_field_value_changed() function to check whether the field value is currently marked as 'changed'.
Regarding the usage of the form load event after saving. What about the (after) form save event? Does it get triggered when saving started or when it finished?
For every function event, you can click on the question mark to get the related info.
In this case for the form save after event:
This event is executed after a user successfully executes a save of the form record. This works for both new & existing records.
Because a record is reloaded after a successful save, the event 'form loaded' is also executed after this event is finished.
In short: It occurs after saving completes. So yes, it would also be a good event to execute your backend call.
Closing issue as there is no open task or request to answer.
Is it possible to check if there are any unsaved changes in a form? I am using a custom frontend function that shouldn't be invokable if there are any unsaved changes, because the backend function it calls uses the data from the database. app.record_save() seems to be async without a callback, so I can't use it to force saving the changes before calling the backend function. There could be a timing problem.
At the moment I am using form state conditions but to add every field one by one is very time consuming and easy to forget if new fields get added.