onAfterSave firing too soon

$w.onReady(()=>{
        $w('#confirmBtn').onClick((event)=>{
        $w('#vdrDataset').getCurrentItem()
//      $w('#vdrDataset').setFieldValue("Active", false);
        $w('#vdrDataset').save();
        $w('#vdrDataset').onAfterSave(()=>{
            $w('#vdrDataset').refresh();
        });
        });
})

This code effectively hides a record from a collection by toggling it to false in the ‘Active’ tag. Anything not marked as true will be filtered out from the displayed results. The problem is the displayed results aren’t updated when this happens, so I’m trying to force them to refresh but now I’ve hit an issue where it’s trying to refresh during the save, instead of after it.

TLDR: I’m getting “Operation (onAfterSave) not allowed during save” whenever I run this code. I guess that means that onAfterSave doesn’t iteslf know not to run during the save.

Any easy workarounds for this?

Hi David :raised_hand_with_fingers_splayed:,

First of all, you should wrap your code inside the dataset’s onReady( ) function, I assume your dataset is a dynamic one.

Then, wait a little bit before refreshing the dataset - the changes might not be live even though the save() promise is resolved.

$w.onReady(() => {
    $w('#vdrDataset').onReady(() => {
        $w('#vdrDataset').onAfterSave(() => {
            setTimeout(() => $w('#vdrDataset').refresh(), 250);
        })
        
        // Set the field to inactive
        $w('#vdrDataset').setFieldValue("Active", false);
        $w('#vdrDataset').save();
    })
})

Note: This method is only meant for the dynamic dataset, the regular dataset requires additional steps.


Hope this helps~!
Ahmad

Hi Ahmad,

This was exactly it! I was trying to figure out whether I needed to use an await function, but setTimeout was perfect.

Thank you.

@davidbowring the async/await is a completely different terminology, in your case, we needed to delay an action, not waiting for a delayed action.

You’re most welcome :wink: Happy to help.

@davidbowring Another note, the events should be declared before attempting to save, for example, the onBeforeSave() function should be declared before calling the save function in order to properly run.