Search code examples
google-apps-scriptgoogle-app-maker

How do you use query.filters.FulllName._contains on the Directory Model


GOAL

If you want to query and filter FullNames from the Directory Model, how do you do it? Can filters.FullName._contains = be used?

What I've Tried

I've tried this in the Directory Model's Server Script. This returns all the GSuite records, cool.

var gQ = app.models.GSuite.newQuery();
gQ.filters.FullName._contains = '';  // filter empty string
var recs = gQ.run();
return recs; // returns all records

When I change the filter string to a character, I get no records.

var gQ = app.models.GSuite.newQuery();
gQ.filters.FullName._contains = 'a';  // filter the letter a
var recs = gQ.run();
return recs; // returns - recs undefined 

Question

What is wrong with the string I assign to filters.FullName._contains =?

What am I doing wrong?


Solution

  • Case Sensitive Solution

    This script filters the Directory Model using a Custom Parameter, but it does not use filters.FullName._contains =.

    Instead, using Javascript's search(), each FullName get's a RegEx evaluation. if (rec.FullName.search(eFilter) > -1))

    Server Script for Directory Model

    var gQ = app.models.GSuite.newQuery();
    var gD = gQ.run();
    
    if (query.parameters.FilteredGSuiteEmail === null) {
      return gD; // return all records
    }
    
    var eFilter = query.parameters.FilteredGSuiteEmail;
    console.info("eFilter: " + eFilter);
    
    var recs = [];
    gD.forEach(function(rec) {
      console.log("searchValue: " + rec.FullName.search(eFilter));
      //console.log("search: " + rec.FullName);
      if(rec.FullName.search(eFilter) > -1) {
        console.log("search: " + rec.FullName);
        var newRec = app.models.GSuite.newRecord();
        newRec.FullName = rec.FullName;
        newRec.PrimaryEmail = rec.PrimaryEmail;
        recs.push(newRec);    
      }
    });
    return recs;
    

    I need to convert my filter string to a RegEx argument that is case-insensitive.

    Is there a better way to do this?