C/AL Modifications and the Change Log

In our previous post about the Change Log in Microsoft Dynamics NAV, we talked about turning on monitoring of the Payment Terms Code on the Customer table.

Now you’ve gotten another request from that client that will involve updating the Payment Terms Code on Customers when certain things happen in the system, such as they go overdue. You’ve made the changes they’ve requested without a hitch, but two months later, they call, very disturbed, because your process doesn’t update the Change Log. What happened? You called VALIDATE on the field, that’s all that’s needed, right?

Unfortunately, no. The Change Log will automatically log all User Interface changes, but changes made in code are handled differently. As the developer, you have the responsibility to register those changes with the Change Log yourself.

Your code needs to go from:

 

Customer.VALIDATE("Payment Terms Code",'COD');
Customer.MODIFY(TRUE);

 

 

To:

 

xRecRef.GETTABLE(Customer);
Customer.VALIDATE("Payment Terms Code",'COD');
Customer.MODIFY(TRUE);
RecRef.GETTABLE(Customer);
ChangeLog.LogModification(RecRef,xRecRef);

 

 

xRecRef and RecRef are both variables of type RecordRef, and ChangeLog is a Codeunit variable pointing at the Change Log Management Codeunit.

The other logging functions work fairly similarly:

  • LogInsert
  • LogRename
  • LogDelete

If the client turns of Change Log or stops tracking the fields you’re programming for, the Change Log Management Codeunit won’t record the changes or give the user any error message, so this code won’t become deprecated when the client changes their mind about auditing.