Record Update Log (View Based)

 

Hello Folks!

Today we’ll show you what and how to record the sequence of events of each user in a knack application. Please take into consideration that everytime someone in the platform performs an action such as insert, edit or delete, it must be logged in a table. This way you will be able to keep an internal control of all movements made in the application by all users for audit purposes.

The most important data to record in the log are:

  • User who carried out the event

  • Date and Time

  • The object

  • If it was an insert / update or delete

Below we outline the process for the implementation of the Record update log:

1. Create new Object in Builder

record update view based 1.png

2. Add 4 fields to new Object:

  • Action as Multiple Choice with 3 options (insert, update, delete)

  • Object Name as Short Text

  • User as Connection with Default User Object

  • Date/Time with default date and time options selected

record update view based 2.png

3. Create a new page with the option of requiring login, in the object select the one created in step 1 and for the views that the new page should have, select form option.

record update view based 3.png
record update view based 4.png
record update view based 5.png

4. Once the new page is created, it must be indicated that it is not visible in the application menu, this is done in the settings tab.

record update view based 6.png

5. Insert the following code in the API & Code section of the APP (replace your information):

/**
 * Create new record log.
 * @param action Log Action (insert | update | delete)
 * @param objectId Knack Object Id
 */
function createRecordLog(action, objectId) {
  // All view-based requests are accessed through a scene key and view key and
  // use a URL in the following format:
  // https://api.knack.com/v1/pages/scene_key/views/view_key/records
  var user = Knack.session.user;
  if (!user) {
    // Not authenticated
    return;
  }
  var knackObject = Knack.objects.models.find(function (item) {
  var record = item.toJSON();
    return record.key === objectId;
  });
  if (!knackObject) {
    throw new Error(
      'Object ' + objectId + ' does not exist in this Knack App.'
    );
  }
  return $.ajax({
    type: 'POST',
    headers: {
      // Replace your Knack Application ID
      'X-Knack-Application-Id': 'XXXXXXXXXXXXX',
      'Authorization': Knack.getUserToken()
    },
    // Replace scene ID and view ID from new scene created in step 3
    url: 'https://api.knack.com/v1/pages/scene_XXX/views/view_XXXX/records',
    data: {
      field_XXX: action, // Replace field_XXX for User field ID
      field_XXX: knackObject.toJSON().name, // Replace field_XXX for User field ID
      field_XXX: user.id // Replace field_XXX for User field ID
    }
  });
}
// Knack events for insert and update
$(document).on('knack-form-submit.any', function (event, view) {
  createRecordLog(view.action, view.source.object);
});
// Knack event for delete 
$(document).on('knack-record-delete.any', function (event, view) {
  createRecordLog('delete', view.source.object);
});

6. As users use the Knack forms to add, update, or delete records, the log records will be visible in the new object created in step 1:

record update view based 7.png

Did you find this helpful?

Please share it your friends and colleagues!

Let's make IT Happen!