Search code examples
google-app-maker

Google App Maker how to save and update records?


We have methods to create and update the item in App Maker, but it will save the whole record. If I want to update only a particular field on the button click, there is no option.

Please post if there are any options to update a particular field?


Solution

  • Automatic Save Mode

    In auto save mode App Maker instantly saves every field, so you have per-field saving granularity. In other words, whenever field's value is changed App Maker sends request to server to save the modification.

    // this code will trigger async call to server to save the modification
    // only for this particular field.
    app.datasources.MyDatasource.item.MyField = 'My new value';
    

    Manual Save Mode

    With manual save mode there is no easy way to save modifications subsets separately, since whenever you call 'saveChanges' method App Maker will try to persist all modifications made to the datasource. Here are some pretty bad workarounds that I would not recommend unless you have no other options:

    // Implementation with callback chaining if field save
    // order matters. It will work extremely slow.
    var ds = app.datasources.MyDatasource; 
    ds.item.MyField1 = 'My new value 1';
    
    ds.saveChanges(function() {
      ds.item.MyField2 = 'My new value 2';
    
      ds.saveChanges(function() {
        ds.item.MyField3 = 'My new value 3';
        ...  
      });
    });
    
    
    // Implementation without chaining. In theory should work
    // faster(if it would work at all...)
    var ds = app.datasources.MyDatasource;
    
    ds.item.MyField1 = 'My new value 1';
    ds.saveChanges();
    
    ds.item.MyField2 = 'My new value 2';
    ds.saveChanges();
    
    ds.item.MyField3 = 'My new value 3';
    ds.saveChanges();
    ...  
    

    Server Script

    Pretty much same answer as for Manual Save mode, it is doable, but I would not recommend do it since performance will significantly degrade.

    record.MyField1 = 'My new value 1';
    app.saveRecords([record]);
    
    record.MyField2 = 'My new value 2';
    app.saveRecords([record]);
    
    record.MyField3 = 'My new value 3';
    app.saveRecords([record]);
    ...