Create a Version History for Records

This article will review setting up a version history for your records in your Knack app.

This article aims to guide you through the process of setting up a version history for your records. By implementing this feature, you will be able to track the user responsible for each edit, the date and time of the edit, as well as the specific field values that were changed.

Scenario

If you have forms that generate and modify records, it is important to keep track of the date and time, as well as the user responsible for creating or updating the record.

Requirements

Before setting up a version history for your records, you will need to have users enabled in your app. You can learn more about getting started with users here.

Tip:  

If you are interested in learning and practicing the steps outlined in this article, but you are not yet ready to implement them in your apps, you can use and explore our Inventory Manager sample app.

This will allow you to gain hands-on experience in setting up the version history.

Steps

1.  Create a Table to Track History


In the Data section of your builder, create a new table. Let's call this table "Edit Logs".

Once the new table is created, add the following fields to that new table:

  • "Edited By": A connection field connecting to the User. Where each “Edit Log” record connects to one “User” and each “User” connects to many “Edit Log” records.

  • "Edit Date" - A Date/Time field with time enabled.

  • "Product" - This connection field allows you to track the original table and easily insert a connected record from it. Each Edit Log connects with one Product and each Product connects with many Edit Logs.

  • Then, add all the fields found in the Product table. You can either manually add the fields or copy them from the table you're creating the history for. 

versionhistory1

2.  Add Record Rules to Update the Edit Log


To ensure that the Edit Log table is updated when forms are submitted in the live app, we will utilize record rules within the form view.

To begin tracking updates from the form, access the form view and proceed to add record rules.

Add record rules with the following settings:

  • Action: Insert a connected record: a new Edit Log record connected to this record

  • When: Every form submission

  • Values:

    • "Edited By": Set to the logged-in Account

    • "Edit Date": Set to the current date

    • "Name": Set to the field value of "Name"

      • This is one of the fields found in the original table. You'll want to do this for all of the fields you're tracking.

versionhistory2

 

3.  Add a Grid View for the Edit Log


Now, you can add a new grid view, showing the Edit Log records. You can add this grid view to the "View Product Details" page if you're following along in the Inventory Manager sample app.  

In the example below, we add a grid showing the Edit Log records connected to the same Purchases connected to this page's Product: 

versionhistory5

The grid will display all connected Edit Log records when viewing the details of a Product.

 

Congratulations! You have successfully implemented a version history for your records.