0
votes

Based on the project tracker I have integrated a changelog into my app that relates my UserSettings model to a UserHistory model. The latter contains the fields FieldName, CreatedBy, CreatedDate, OldValue, NewValue.

The relation between both models works fine. Whenever a record is modified, I can see the changes in a changelog table. I now want add an "undo"-button to the table that allows the admin to undo a change he clicks on. I have therefore created a method that is handled by the widget that holds the changelog record:

function undoChangesToUserRecord(changelog) {
  if (!isAdmin()) {
    return;
  }

  var fieldName = changelog.datasource.item.FieldName;
  var record = changelog.datasource.item.UserSettings;

  record[fieldName] = changelog.datasource.item.OldValue;
}

In theory method goes the connection between UserHistory and UserSettings up to the field and rewrites its value. But when I click on the button, I get a "Failed due to circular reference" error. What am I doing wrong?

1
Is it client code? - Pavel Shkleinik
It's a server script. - Johan W.

1 Answers

0
votes

I was able to repro the issue with this bit of code:

google.script.run.ServerFunction(app.currentPage.descendants.SomeWidget);

It is kinda expected behavior, because all App Maker objects are pretty much complex and Apps Script RPC has some limitations.

App Maker way to implement it would look like this:

// Server side script
function undoChangesToUserRecord(key) {
  if (!isAdmin()) {
    return;
  }

  var history = app.models.UserHistory.getRecord(key);

  if (history !== null) {
    var fieldName = history.FieldName;
    var settings = history.UserSettings;

    settings[fieldName] = history.OldValue;
  }
}

// Client side script
function onUndoClick(button) {
  var history = widget.datasource.item;

  google.script.run
    .undoChangesToUserRecord(history._key);
}