Record Update Log (Object 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

![](upload://tPgUbdVc7tI3gJTrsTNRqhWFFqA.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

![](upload://xRic5WiKj7WLVD9DGq8ULXVUoNz.png)

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

 

// Replace with your Knack APP Info

var KNACK_HEADERS = {

  'X-Knack-Application-ID': 'XXXXXXXXXXXXXXXXXXXXXX',

  'X-Knack-REST-API-Key': 'YYYYYYYYYYYYYYYYYYYYYYYY'

};

// Replace info with your Knack "Record Change Log" Object

var OBJECT_RECORD_LOG = 'object_XXXXXX';

/**

 * Create new record log.

 * @param action Log Action (insert | update | delete)

 * @param objectId Knack Object Id

 */

function createRecordLog(action, objectId) {

  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: KNACK_HEADERS,

    url: Knack.api_url + '/v1/objects/' + OBJECT_RECORD_LOG + '/records',

    data: {

      field_XXX: action, // Replace field_XXX for User field ID

      field_YYY: knackObject.toJSON().name, // Replace field_YYY for User field ID

      field_ZZZ: user.id // Replace field_ZZZ 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);

});

4. 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:

![](upload://6po2oQy3N5iyww0C1Ml7APLRb4c.png)

We hope you can incorporate this in all your Knack applications!

If you liked this subject in particular, please let us know by leaving a comment below.

Hey David - thanks for the warning.  Any chance you could provide some guidance on implementing this safely?

Once again, DO NOT use this as is. It will expose all the data in your app to anyone on the internet. Use view-based API calls instead, and never put your API key in your code.