Wednesday, January 1, 2025

Understanding Modify(true) and Modify() in AL: When to Use Them and What to Watch Out For

Hello Everyone, 

During a recent Business Central project, I encountered a serious issue when modifying bulk records using Modify(True). The system froze, causing performance bottlenecks and making the process unusable. After troubleshooting, I realized that improper use of Modify(True) was the root cause.

What Are Modify(True) and Modify?

  • Modify: Saves changes to the database without triggering the OnModify trigger.
  • Modify(True): Saves changes and triggers the OnModify trigger, executing any additional logic defined in the table.

What Went Wrong?

In my project, I was updating thousands of records using Modify(true), thinking it would ensure business rules were applied. However, this caused:

  1. Performance Bottlenecks – The OnModify trigger executed for each record, significantly slowing down the process.
  2. Recursive Calls & Locking – If the OnModify trigger contained additional Modify(True) calls, it created a loop, causing database locks.
  3. System Hanging – The excessive load on the server caused Business Central to become unresponsive.

How to Fix It?

  • Use Modify Instead of Modify(true) for Bulk Updates
    If you don’t need the extra logic in OnModify, use Modify to improve performance.

  • Move Business Logic Outside of OnModify
    Instead of relying on OnModify, consider executing necessary logic in an explicit function that you call after bulk modification.

  • Use DisableTrigger(True) for Temporary Updates
    If OnModify must be skipped temporarily, use Rec.DisableTrigger(True); before modifying records, then re-enable it afterward.

  • Batch Processing
    Instead of modifying all records at once, process them in smaller batches to prevent timeouts and locking issues.

Lesson Learned

Using Modify(True) incorrectly can cause severe performance issues, especially when working with large datasets. Always evaluate whether triggering OnModify is necessary and optimize accordingly.

Have you encountered similar challenges? Let’s discuss in the comments!



Reach out to me if you have any questions or suggestions. 

Check out other blogs, if you haven't already. 

Kindly suggest what else you want me to blog about. Keep following and supporting!

You can also email me at jyotsnaoberainav@gmail.com

Also, visit my website JONAV if you are looking for NAV/D365BC Training and/or Services!

Thank you! 

No comments:

Post a Comment