Refreshing Form After Save

I have a new table and form, both called Settings. This is to replace setting some variables in code in my InitialSetup function. Table fields are SettingName, SettingType and SettingValue.

SettingType is a combo on the form with possible values Text, Number, Boolean and JSON.

SettingValue stores the actual value as text, regardless of type. it is a Memo field.

I also have user fields for each type of setting. These are initially hidden, and each one is shown depending on SettingType. So if SettingType is JSON, then the SettingJson text control (also a Memo) is displayed.

When a record is shown, the InitSettingValue function runs. part of what it does is load the SettingValue into the appropriate visible control. So for JSON, it basically does a pretty-print of the JSON value into the SettingJson unbound field.

This works fine on the initial load, but I’m having trouble getting it to work after the field (or record) is saved.

I put InitSettingValue in the new On Finish event for the form. I was told this event would fire AFTER the record was saved. As you may remember, the On Complete event actually fires before the record is saved, and prevents the actual saving of the record.

What I expected to happen is that when I change the text of SettingValue, then save the form record, it should redisplay the SettingJson field, including any changes made to SettingValue. This is not happening.

I must actually move to another record, then move back to see the formatted changes in SettingJson.

Is there a way to refresh the form data via code?
Is the On Finish event not firing as expected?

My original intent was to call code in the On Validate event immediately after changing the field value to refresh the visible field. But that won’t work either.

Can you suggest a way to make this work?

Thanks…

Thanks, but this is mostly solved. My validate code had some issues.

Even without anything in the On Finish event, it now works mostly. The On Validate function attempts to parse the json, and if it passes, it automatically re-displays the SettingsJson field with prettified data, and also replaces the SettingsValue with stringified non-pretty value. this is the one that’s stored in the database.

Since this happens in the On Validate function, I don’t need to re-display the info when saving the record.

What’s not working: If I’ve changed the field value, and can’t get it right, I hit the cancel X button, which should also fire InitSettingValue, thus refreshing the SettingsJson prettified value from the original SettingsValue field. This is not happening. It just cancels the update, but doesn’t re-display the data properly.

Thanks…

Hi Ron,

Thank you for letting me know that this functionality is partially working.

Just to clarify it, the event On Finish was not designed to change the value from the same form, as it does not trigger the backend with all necessary states. One example of how you can use it is, after saving a form, you could programmatically navigate to another menu using this event.

Regarding your question: If you meant the cancel X button on the form field, the only event that triggers a function is the ‘On Click‘, because you are still clicking on the field.

Regards,
Elton S

Thanks for the answer Elton. I thought the On Finish would be able to modify an unbound field value, as it’s not being written back to the database.

I was referring to the main cancel button on the form. I want to cancel an update or change I was trying to make to a field or fields on the form.

There is an On Cancel event available, which I interpret to mean that this event fires when I click the X at the top right of the form, next to the save checkmark. I put InitSettingValue in the On Cancel event during form design. This function should take what is in the SettingValue field and prettify it in the unbound SettingJson field.

The intent on this form is to have the SettingValue field hidden. This is a bound field to the same-named field in the table. The edit should be done in the visible SettiingJson field. For now, I’m keeping the SettingValue field visible during development.

Can you tell me why the function is not restoring the proper value into the SettingJson field? Thanks…

Hi Ron,

The On Cancel button on the form will be triggered when you click the X button on the main form; this will close/cancel the current form edition, and return the field values to the latest data saved in the table when you accessed the form.

If you are using this function to change another form’s field, it won’t work because it will discard all the changes and remove the form from edit mode.

In case it could help you, as I don’t know your case scenario:
You could perhaps add a field not bound to the table, with a display-type button, and add a function to reset a field value.

Regards,
Elton S

Well, thanks so much for the quick reply.

The Settings table / form have 3 fields:
SettingName (text 50),
SettingType: (drop-down: Boolean, Text, Number, JSON),
SettingValue: (memo)

The form also has 4 unbound fields, one for each type. The SettingBool is a checkbox, the SettingText is a memo, SettingNumber is a text field and SettingJson is memo. These each become visible based on SettingType.

The idea is to make the entries in the unbound fields, then the validate event will make sure the text is correct, and store the value in the SettingsValue bound field. In the case of JSON, the validate event should also pretty-print it back to that field, and stringify it to the value field.

I was hoping the cancel button would call the InitSettingValue function which would simply re-display the prettified JSON in that field. Also, I had hoped the On Finish event would also be able to redisplay the unbound field prettified.

It’s been about 2 weeks with no answer on this topic.

When I open my Settings form and pick a setting from the list, the On Show event runs the InitSettingValue function, which, given the setting type, makes the appropriate unbound field visible, then loads it from the bound SettingValue field, which will be hidden after development. In the case of SettingType of “JSON”, this means it pretty-prints the json object into the field. This works fine.

All changes will be made to the visible unbound field, and the On Validate event will cause the JSON (or whatever) value to be verified, then stringified into the SettingValue field.

This is working properly. However, if I made a change to the JSON which will not validate, then I click the Cancel X button, this is also set to run the InitSettingValue function. But in this case, it does not really work. the incorrect JSON value is not “fixed” and redisplayed (pretty) in the unbound field. My changes are indeed cancelled, and if I move to another setting and back, it will be properly displayed in the unbound field.

The question is, why doesn’t the InitSettingValue function cause the field to be properly refreshed after a Cancel??

Here is pertinent code:

function InitSettingValue(five, context, result) {
five.log('InitSettingValue');
    const raw = five.field.SettingValue || '';
    const fieldType = (five.field.SettingType || '').toLowerCase();
    let fieldName;

    // reset all user-facing fields first
    five.field.SettingJson = '';
    five.field.SettingText = '';
    five.field.SettingNumber = '';
    five.field.SettingBool = false; // checkbox → default false

    // now populate based on type
    if (fieldType === 'json') {
        fieldName = 'SettingJson';
        try {
            five.field[fieldName] = raw ? JSON.stringify(JSON.parse(raw), null, 2) : '';
            // five.field.SettingJson = raw ? JSON.stringify(JSON.parse(raw), null, 2) : '';
        } catch (e) {
            five.field[fieldName] = raw;
            // five.field.SettingJson = raw;
            // const msg = 'For JSON, Item names and text values must use double quotes.\n  ex: {"Item": value (or "value"),...} \n\nArrays must use double quotes if text\n. ex: [Item, Item...] (or ["Item", "Item",...])';
            // five.log(raw);
            // MessageBox(five, {Title: "Invalid Value", Message: msg});
        }
        ShowSettingField(five, fieldName, fieldType);
    }

Here is the pertinent snippet of the library code:

function ShowSettingField(five, fieldName, fieldType)  {
    
    // display properly formatted field data
    
    const raw = five.field.SettingValue || '';
    const fType = (fieldType || '').toLowerCase();
    five.log(`ShowSettingField: fieldName=${fieldName}, fType=${fType}`);
    five.field[fieldName] = '';
    switch (fType) {
        case 'json':
            try {
                five.field[fieldName] = raw ? JSON.stringify(JSON.parse(raw), null, 2) : '';
                five.log('ShowSettingField: set field value pretty')
            } catch (e) {
                five.field[fieldName] = raw;
                five.log(`ShowSettingValue: error ${e.message}`);
            }
            break;

Thanks…

Hi Ron,

The On Show event is not triggered when clicking on the Cancel X button.

When you click on the Cancel X button, the fields should return to their initial state value. Can you confirm that is the case, what is teh default value when you first navigate to teh form?

What you may be able to do is, when you click on the cancel button, you programmatically click on the edit button and change the value. I think you have done it before.

I will check if I have any functionality that may help you, but I cannot guarantee it, as it is likely to be custom development.

Regards,
Elton S

Thanks for the quick answer, but I don’t think you understood my question. Sorry if I wasn’t clear.

I mentioned On Show only because that event is triggering a function that takes the actual bound field and puts its contents into an unbound field, in this case, it pretty-prints the JSON into the unbound field. This function is InitSettingValue.

I also call that function from the On Cancel event. My expectation was that calling the function from On Cancel would cause the not-yet-changed bound field to be redisplayed “prettified” in the unbound field (the same as it is when first showing the record). Remember I’m making the actual edit in the unbound field.