Search code examples
google-app-maker

Generating an email list from AppMaker Database


I am trying to figure out how I can get database information involving the email column, make an array with all of the emails and then use the "button" feature to populate the "To:" part of an email page.

Any help is appreciated. Very new at this and pointing me on where to get the info would be great. Thanks


Solution

  • I recommend you to run a server script that would query the datasource that has the emails. The script will look something like this:

    function getEmails(){
    
      var query = app.models.<yourmodel>.newQuery();
      var results = query.run();
    
      var allEmails = [];
    
      if(results.length > 0){  
        for(var i = 0; i < results.length; i++){    
          var uniqueEmail = results[i].<emailfieldname>;
          allEmails.push(uniqueEmail);      
        }
      }
    
      return allEmails.join();      
    
    }
    

    Then add a script to the button widget "onclick" event that will run the server script and manipulate the returned data. Something similar to this:

    function poulateToField(response){
    
      <widget path>.text/value = response;
    
    }
    
    google.script.run.withSuccessHandler(poulateToField).getEmails();
    

    The above widget path would be the path to the "To:" widget, which can be a text box, text area, etc. In my case, I used a text area and the path was this "widget.parent.descendants.TextArea1.value"

    I hope this helps. If you have more questions, just let me know! :)

    P.D. Please don't forget to review the official documentation for a better and more detailed explanation.