google-app-maker

How to filter records in Google App Maker?


I am creating an application that will help our employees manage tasks. Employees are divided into regions in the country. I would like the employees from a given region to see tasks related only to this region.

Workflow:

  1. A task is submitted through the form. One of the fields in the form is the "name of the region".

  2. The task falls into view for region.

  3. Employees can take over the task from the view for their region.

I have MAIN data model named Service where embedded is form for submitting a task.
I also have data dictionary modal, where I have imported data about employee and his region.

Questions:

EDIT: I have tried to do datasource on Service modal and write some query doing filters, but I don't exactly understand how the query works.

EDIT 2: This is how modals look like:

Service:
1

DOT:
2


Solution

  • The way to go here probably would be to have a datasource under your Service model that filters your Service records on Province via a subquery of the DOT model. This can be accomplished via a query server script in this datasource.

    Example code would be (recall that 'query' is a default variable for the current model server script so the subquery needs it's own variable):

    var DOTmodelquery = app.models.DOT.newQuery();
    DOTmodelquery.filters.email._equals = Session.getActiveUser().getEmail();
    var result = DOTmodelquery.run();
    
    if (result === undefined || result.length === 0) {
      throw new app.ManagedError('Could not retrieve user to perform this query!');
    } else if (result[0].Province === null) {
      throw new app.ManagedError('User does not have a Province value to perform this query!');
    } else {
      query.filters.Province._equals = result[0].Province;
    }
    
    return query.run();
    

    Not sure that a relationship between Service and DOT would be the way to go here but you could possibly consider establishing a Province model and then having a relationship between Province-Service and Province-DOT and then try running a query in Service like so query.filters.Province.DOT.email._equals = Session.getActiveUser().getEmail();. Hopefully this will get you what you were hoping for.