Search code examples
google-app-maker

Count records in already applied query


I know the method for getting the full count of records:

function countRecords(){
var records = app.models.BigQuery.newQuery().run(); 
  return records.length;

}

or for getting the count of records from a new query

function queryModel(){

  var qq = app.models.BigQuery.newQuery();
  qq.clearFilters();
  qq.parameters.v1 = "GNa";
  qq.parameters.v2 = "TH";
  qq.where = "(document_name contains :v1) OR (document_name contains :v2)";
  var results = qq.run();
  return results.length;

}

But what I need is the server or client side way to get a count from the applied query (which does seem to be persistent across different pages using the datasource). In other words, I need to write this without using "newQuery.run()". (I'd prefer client side, so I can attach it to the widget with the query that is created from my form).

query.items just gives you the length up to the first pagination mark.

Edit, trying this, but it gives a circular reference error:

changing my server side code to:

function countRecords(query){
var records = query.run(); 
  return records.length;

}

and my button side code to:

google.script.run
.withSuccessHandler(function(o){console.log("Success"+o);})
.withFailureHandler(function (e){console.log("Error"+e);})
.countRecords(widget.datasource.query);

error is

Failed due to circular reference. at Popup1.Content.Panel2.Table1Panel.Table1.Button3.onClick:4:2


Solution

  • What worked for now is:

    widget.datasource.items.length
    

    I had to change my pagination to 400 and

    I know that will not work for sets larger than 400 but this is what handles my current use case.