google-app-maker

Display result of a query that calculates the total of a field as the value of a label in a list


I have two Datasource tables Projects and tt_records with a hours number field. There is a one to many relation between the Project and tt_records. I would like to display the total number of hours per project in a table. I am able to compute the total hours in server side function, how do I bind the total with a label on the UI. I am attempting to use the following in the binding on the field. I see the function is called through info statements in the console logs, however the value does not display on the UI clientgetProjHours(@datasource.item._key); following is the Client Script

function clientgetProjHours(key){  
  return (google.script.run.withSuccessHandler(function (key) {
      console.info("received result");
    }).getProjHours(key));


}

Following is the server side script

function getProjHours(key){
  console.log ("In the function getProjHours (" + key +")");

  var pRecords = app.models.Projects.getRecord(key);
  console.log("Contents of " + pRecords); 
  var tRecords =pRecords.tt_record;
  console.log("Contents of t Records" + tRecords); 
  var total = 0;
  tRecords.forEach (function (item){
      total += item.actuals;                    
      });
  console.log ("The result is: " + total);
  return total;   

 } 

Could you please suggest the best way to achieve this fuction. Thank you very much for your help


Solution

  • key parameter in function (key) { is the result of the Server Script.

    So you just need to replace:

    function (key) {
    

    With:

    function (result)
    

    Also replace:

    console.info("received result");
    

    With:

    app.pages.[PageName].descendants.[LabelName].text = result;
    

    But as it mentioned already Calculated Model should fit such use case better.