1
votes

I'm building a document management application with the following Data Models:

  • Doc_Metadata - Approval_Requests - WorkflowStage - Approver - Comment

I am trying to use the Document Approval Workflow template as a starting point, and associating the Doc_Metadata parent to the "Requests" model, such that each approval request is associated to (owned by) a parent Metadata record.

I have gotten it to work from start to finish without throwing any errors, HOWEVER, no matter what I do I cannot get the Metadata - Request relation to save.

I've posted my client scripts for the Add Request page below, and also attached the zip of my application in case someone wants to look in more detail.

Any and all suggestions are incredibly appreciated, I love the idea of appmaker but have been struggling to understand relations versus how they are traditionally handled in SQL.

    /**
 * @fileoverview Client script functions for AddRequest page.
 */


/**
 * Navigates user to the add request page and sets page URL parameters.
 * @param {string=} metadataKey - optional metadata with this key will be used
 *     as default for the new approval request.
 */
function gotoAddRequestPage(metadataKey) {
  var params = {
    metadataKey: metadataKey
  };
// DEBUG
    console.log(params.metadataKey);
    console.log(metadataKey);

  gotoPage(app.pages.AddRequest, params);
}


/**
 * Creates a new request and redirects user to the edit screen afterwards.
 * @param {Widget} submitButton - button that triggers the action.
 */
function createRequest(submitButton) {
  var addRequestPage = submitButton.root;

  if (addRequestPage.validate()) { 
    submitButton.enabled = false;


    submitButton.datasource.saveChanges({
      success: function() {
        submitButton.enabled = true;

     //DEBUG
        console.log("requestId:" + submitButton.datasource.item._key);

        goToRequestDetailsPage(submitButton.datasource.item._key);
      },
      failure: function(error) {
        submitButton.enabled = true;
      }
    });
  }
}



/**
 * Creates a new request and redirects user to the edit screen afterwards.
 * @param {Widget} cancelButton - button that triggers the action.
 */
function cancelCreateRequest(cancelButton) {
  cancelButton.datasource.clearChanges();
  app.showPage(app.pages.Main);
}

function onRequestCreate () {
  google.script.url.getLocation(function(location) {
    var metadataKey = location.parameter.metadataKey;
    var props = {
    metadataKey: metadataKey
    };
    var allMetadataDs = app.datasources.AllMetadata;
    var metadataDs = allMetadataDs.item;
    var requestDs = app.datasources.RequestsByKey;

    //DERBUG//
    console.log("metadataKey: " + metadataKey);

    var newRequest = requestDs.createItem();
    newRequest.Metadata = metadataDs;

    var requests = metadataDs.Request;

    requests.push(newRequest);
  });
}
1

1 Answers

1
votes

struggling to understand relations versus how they are traditionally handled in SQL

You can configure your app to use Cloud SQL database: https://developers.google.com/appmaker/models/cloudsql

I cannot get the Metadata - Request relation to save

Here is a snippet that should work(assuming that you are using datasource in autosave mode).

var allMetadataDs = app.datasources.AllMetadata;

// metadata record that you need should be selected at this point of time
var metadata = allMetadataDs.item;
var requestDs = app.datasources.RequestsByKey.modes.create;
var requestDraft = requestDs.item;

// This line should create relation between draft request record and
// existing Metadata record. 
requestDraft.Metadata = metadata;

// send your draft to server to save
requestDs.createItem(function(newRecord) {
  // log persisted request record asynchronously 
  console.log(newRecord);
});

By the way, your life will become way easier, if you add a drop down with metadata items to the request creation form.