Search code examples
google-apps-scriptgoogle-cloud-platformgoogle-cloud-sqlgoogle-app-maker

How to re-execute the query script of an App Maker Datasource


Goal

Use App Maker to collect User Birthdays and display only the Birthdays this Month.

Issue

I have a data model, persons. In that model are two Datasources, the default persons and a second birthdaysThisMonth. The datasource query script in birthdaysThisMonth properly runs and returns only the birthdays this month from the persons model.

However, when I change a birthdate in the persons datasource, the birthdaysThisMonth datasource remains unchanged, e.g., the Query script in birthdaysThisMonth is not re-executed.

To change the birthdate, I select a new date from the date picker, and the new value is shown in a table. I am not using a submit button, I see the change when the date picker looses focus.

What I've Tried

This script is executed as a Query script in the birthdaysThisMonth datasource which is not set to Manual save mode. It returns the records I want.

function setBirthdays() {
  var calcRecords = [];
  var personsRecords = app.models.persons.newQuery().run();
  for (i=0; i<personsRecords.length; i++) {
    var id = app.models.persons.newQuery().filters.Id._equals = i;
    if (personsRecords[i].birthdate.getMonth() == thisMonth())  {
      var calcRecord = app.models.persons.newRecord();
      calcRecord.emailSecondary = personsRecords[i].emailSecondary;
      calcRecord.birthdate = personsRecords[I].birthdate;
      calcRecord.Id = personsRecords[i].Id;
      calcRecords.push(calcRecord);
    }
  }
  return calcRecords;
}

Question

How do I re-execute the query script in birthdaysThisMonth when the data in persons has been updated. How do I trigger the query so it reevaluates the data in persons and filters accordingly.

Using Events and onAfterSave seems promising, but I haven't found an example of this.

BTW

I'd like this work to happen on the server side if possible.


Solution

  • See Markus Malessa's comment for the answer.

    Unfortunately you can't use server events like onAfterSave to trigger reloading a datasource on the client, so your proposed solution won't work. It would seem that your only possible solution would be to make your persons datasource a manual save datasource, in the form where you change the birth date you will need a 'Save' button that calls widget.datasource.saveChanges() and in that function incorporate a callback that will reload your birthdaysThisMonth datasource

    The way I'm attempting to push/pull data isn't great. Looks like Manual Mode provides the types of features I need.