Auto update Backendless database

Hi. I was wondering if it is possible to auto update data tables, from the backend itself. For example, in case of a hotel booking application, the number of available rooms gets auto updated after the checkout date of a customer. Is something of this sort possible?

Hi Saurav

You may implement it by using business logic. Depending on the event you may set up an event handler which will execute the required logic when triggered. If you need some logic to be executed after an update request in your table - add afterUpdate event handler for this table and add necessary logic to it.

If you want some logic to be executed on particular date/time - add necessary logic to a timer.
Docs and examples for event handlers.
Docs and examples for timers.
If you have additional questions - do not hesitate to ask.

Regards Anton

I have a question about the timers. I checked out the timers I can create in the Business Logic tab. It asks for a start time and an end time. Is there a way that timers can check the values in the columns of data tables, for the start and end time?

I’ve written some demo code for timers, just to see if I’ve understood. Can you tell me if its okay?
Here’s the code:


execute (req) {
Backendless.Data.of("Bookings").find()
      .then(function (result){
        var res = result;
      })
      .catch(function (error){
      });
      for (var i=0; i<res.length(); i++) {
        var today = new.Date()
        if (res.checkout.getDate() == today.getDate() && res.checkout.getMonth() == today.getMonth() && res.checkout.getFullYear() == today.getFullYear() && res.in_use == true) {
          res.in_use = false;
          Backendless.Data.of("Hotels").save({objectId: res.hotelID, free_rooms: free_rooms + res.rooms})
            .then(function(res) {
            })
        }
      }
  }

If I use this code, will it automatically run on a daily basis?

No, the timer can’t check anything before its start.
The code you provided looks OK.

I’m getting an error saying that the code is incorrect.
Entire code in this link.

Can you please provide a full error message and code if any?

This is the error message: “Unable to deploy the code. Some of the files contain errors. Please fix them in Coding section.”

The entire code is in this link.

The Coding console highlights the line where the error is detected.

Nothing of that sort is happening.

Don’t you see this when deploying?

Here is a fixed code: https://pastebin.com/C1Wv4fDB
Somehow you missed to enclose the doc comment before the execute method.

I fixed the error. But how to I change the mode from “Draft” to “Debug” and “Prod”?

You cannot debug while in coding. As to Prod, when you click ‘Deploy’ - your timer moves to Prod mode (while draft also still exists, of course).

I’ve run the timer code outside of the console. It works as expected. But the “invoke timer” option doesn’t seem to produce the same results. The data remains unchanged even after running it. What exactly is the “Invoke timer” option for?

The “Invoke timer” button runs the code in your timer immediately, so there shouldn’t be any differences from running by schedule. How do you verify that there’s not effect? Maybe it’s just the condition inside your timer is not satisfied at the moment of running it manually and that’s why you don’t see your data changed.

I’ve made dummy data entries to ensure that the conditions will be satisfied. There are certain data columns whose values are being changed in the timer’s code, which is not changing.

I’ve run the exact same code from outside the platform in a regular JS file, and it works as expected. I could attach the code, and give you my application key for you to check if you’d like.

Can you simply run something like

Backendless.Data.of("Hotels").save({"test":123})

just to check that the timer runs? If it doesn’t work, please just provide your app ID so that I could try to run it and check.

Your example seems to be working. But mine still doesn’t. Can I give you the app ID for you to check, because I’m sure my code is correct.

My example is just a simplified form of what you’re trying to do, still it shows that apparently there’s no problem with Backendless APIs or timers. Finding problems in the customer’s code is not included into free support on the forum, so you may want to contact sales@backendless.com for consulting services.