Hey Backendless team!
I need to create a field in my database that will take a value of “true” each time another value is changed. I don’t think I’ll be able to do this with a generated column, since the idea is to reset the value to true any time any change is made to another field, but please let me know if I am incorrect.
Otherwise, can you point me toward any examples or give me a high level idea how I might be able to use Server code to listen for a change in a specific field and then update another field in that same record when a change occurs?
Thanks,
James
P.s. the specific context here is hypothetically that a table has a text field called “Notes” and a Boolean field called “isNewNote”. The Boolean can have any value at any time. However, whenever any change is made to the “Notes” field the Boolean needs to be updated to “true”, regardless of its current value.
P.p.s. I can’t use the “updated” field for this purpose because other fields in the record will also be changing. I need to detect the change in just a single field.
You could create something like noteUpdatedAt property, and in your frontend logic, or wherever the changes to note are coming in, just add additionally to store datetime for this particular field update.
Hello @James_Hereford,
You’re correct that a generated column won’t fit your needs for this scenario. Instead, you can achieve this by using a beforeUpdate
event handler.
Here’s how you can set it up:
- Create a
beforeUpdate
Event Handler: In the Backendless Console, go to the “Cloud Code” section and create a beforeUpdate
event handler for your table.
- Retrieve the Existing Record: Use the event handler to retrieve the existing record from the database by its
objectId
. This lets you access the previous value of the Notes
field.
- Compare the
Notes
Field: Compare the existing value of the Notes
field with the new value that’s being saved. If the values differ, it means the Notes
field has been updated.
- Update the
isNewNote
Field: If the Notes
field has changed, set the isNewNote
field to true
.
This approach will ensure that every time the Notes
field is modified, the isNewNote
field is updated to true
.
Here’s how this logic might look:
Please note that the Event Handler should be of the Blocking type:
For more information about how Event Handlers work, please refer to the documentation: Event Handlers Documentation.
Let me know if you need any further assistance!
Regards,
Stanislaw
Thanks Uldis! I totally agree. In this case I’m working with a front end component that doesn’t have a handler I can use.