sample code in javascript BL beforeupdate to prevent certain column change

I want to prevent certain column to be changed, I want that column to retain it’s original value. Isn’t it possible to get old values without additional call (findbyid)? Code below is working but I’m not sure that it’s optimal:

Backendless.enablePromises();
Backendless.ServerCode.Persistence.beforeUpdate('TableA', function(req) {
    var store = Backendless.Data.of('TableA');
    return store.findById(req.item.objectId)
    .then(obj => {
        if (req.item.constCol && req.item.constCol != obj.constCol) {
            req.item.constCol = obj.constCol;
            return Promise.resolve();
        }
    })
    .catch(err => {
        return Promise.reject('Unable to. Got an error : ' + err.message)
    });
});

also, it’s not working if I set event to async. What should I change for async event?

Hi Yuriy,

Your approach is fine, I don’t see any obvious way to do the same with less code.

The async events are executed asynchronously, which means that they’re only launched before the update, but the code may be executed during or after the update - that’s why it may not do what you expect. It means that you can’t do this using an async event because of the way asynchronous events work.

Thanks. I hoped that there are exist some parameters with old values. Maybe you will consider adding it on your roadmap?

Yes, I think we may discuss some kind of immutable columns. Thanks for the suggestion!