By | 24 January 2012

Creating an Effective Audit Subrecord

Here is a simple but invaluable audit subrecord that I like to put on all my records. It logs when the row was originally created and by who and when the row was last updated and by whom. This can be invaluable for debugging issues and doing research on data. You just need to put this sub-record on all your records and the code will fire automatically to set the audit fields.

Sub-Record Name: C_AUDIT

Field Name Usage Field Type
LASTUPDDTTM The last date-time the row was updated. DateTime
LASTUPDOPRID The last user who updated the row. Char(30)
C_CREATED_TIME The date-time the row was created. DateTime
C_CREATED_USER The user who created the row. Char(30)

The subrecord has one piece of Peoplecode on LASTUPDDTTM.SavePreChange

If GetRow().IsNew And
      GetRow().IsChanged Then
   GetRecord().C_CREATED_TIME.Value = %Datetime;
   GetRecord().C_CREATED_USER.Value = %OperatorId;
   GetRecord().LASTUPDDTTM.Value = %Datetime;
   GetRecord().LASTUPDOPRID.Value = %OperatorId;
Else
   
   If GetRow().IsChanged Then
      GetRecord().LASTUPDDTTM.Value = %Datetime;
      GetRecord().LASTUPDOPRID.Value = %OperatorId;
   End-If;
   
End-If;

Do you want to become a CI Expert?

If you want to learn more about developing using Component Interface then check out our CI Training Video.