Configuring Custom Audit Records

March 15, 2014 , by

Inserting Default Values when a Record is Added

For each applicable field in the data model, one can specify a default value to be populated for the field.
In addition, the field can be set to read-only and/or hidden so that the user doesn't have control over the field. The default value applies only to add interface.

You can provide a literal string or a numeric value to the default field. In addition you can specify certain placeholders which are described below:

  • now = DateTime.UTCNOW
  • user_name = User name of the logged in user
  • user_id = Internal Pistachio User Id of the logged in user
  • user_ad_guid = The ActiveDirectory GUID of the logged in user.

In the case of audit records we could have two field - CreatedOn and CreatedBy.
We could configure the CreatedOn to be a hidden field, with a default value of now.
Similarly, we could configure the CreatedBy field as a hidden field, with a default value of user_name

Using Rules to Update Audit Records

We might want to update certain fields in the data model on edit interface. For example, we have two fields - ModifiedOn and ModifiedBy. We will create a rule to update the ModifiedOn and ModifiedBy field on edit interfaces. The rule will have one condition (check to see if it is edit page) and two results (setting new value for ModifiedOn and setting new value for ModifiedBy).

Rule Condition:

The condition to determine that it is an edit interface is to check to see that the record id of the active record is not equal to -1. In Pistachio, when a record has not yet been added, its assigned a value of -1. If we had to check for add page our condition would be record_id = -1.

Condition Type = Value Not Equals
Test Formula = ##record:record_id## 
Condition Value = -1

Basically, we are saying, if the record id is not -1.

Rule Results

The rule result will simply set the values of our desired fields. We will have 2 rule results. One to set the ModifiedOn field, the other to set the ModifiedBy field.

Modified Date:

Result Type = New Value
Result Structure Element = ModifiedBy (Or any desired field)
Result Value = now

Modified By (Available in Version 1.2.5 onwards)

Result Type = New Value
Result Structure Element = ModifiedBy (Or any desired field)
Result Value = `user_name`

The placeholder user_name along with user_id, user_ad_guid is available as of version 1.2.5