Search code examples
google-app-maker

Google App Maker: How to access properties of an item bound to widget?


In the App maker, I have created a datasource with several items which I'm displaying on a table. When the user clicks on an item, a pop up comes up with the properties of the item the user clicked bound to the widget. Once the user clicks the save button on that pop up, I want to get the id of the current item in a server script. How can I achieve this?


Solution

  • You will need to use an asynchronous operation. The official documentation explains how you can achieve this. Basically, you need to do something similar to this:

    Let's say that on a server script you have the following:

    function doSomething(id){
        //do something
        if(!id){
            throw Error("Id is missing");
        }        
        return "ID = " + id;
    }
    

    Then on the SAVE button you need to have something like this on the onClick event handler:

    var id = app.datasources.**yourDataSource**.item.id;
    google.script.run.withFailureHandler(function(error){
        console.log(error);
    ).withSuccessHandler(function(response){
        console.log(response);
    }).doSomething(id);
    

    I hope this helps!