Easy way to Audit your DML transactions in Apex

Often (if not ALWAYS) you'll want to know whom inserted or updated a record in your table, from your Apex application. There may be many ways to do this, but the easiest I've found is to use a simple PL/SQL anonymous block process, to capture the logged in username from the Apex session cookie, and pass this to a form field. Of course, you'll want to add the necessary columns to your table to store these values and include them with your DML transaction.

On your form, add a PL/SQL anonymous block process that runs at any point prior to the DML transaction.

Here is the code:

    xcookie owa_cookie.cookie;
   xcookie := owa_cookie.get('LOGIN_USERNAME_COOKIE');
   :AUDIT_USER := xcookie.vals(1);

You can even choose to display the field on the page, as a subtle warning to the user that you are auditing this transaction. To do this, make sure the Process runs prior to printing your Regions.