Add documentation on 'if not Insert then Modify' best practices#256
Open
StefanSosic wants to merge 5 commits into
Open
Add documentation on 'if not Insert then Modify' best practices#256StefanSosic wants to merge 5 commits into
StefanSosic wants to merge 5 commits into
Conversation
StefanSosic
commented
Jun 3, 2025



TheDoubleH
approved these changes
Jun 4, 2025
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a new Best Practice documentation page to the AL guidelines site explaining why the “if not Insert then Modify” pattern is inefficient in Business Central and proposing an alternative approach.
Changes:
- Introduces a new Best Practices doc page describing the performance drawbacks of “if not Insert then Modify”.
- Adds a “Note” callout clarifying a singleton-table exception and links to the Singleton Table pattern.
- Provides alternative sample code using
IsEmpty()to avoid insert-failure handling.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| When handling data insertion in Business Central, a common but inefficient pattern is attempting to insert a record and modifying it if the insert fails due to duplicate keys. This approach introduces unnecessary performance overhead and should be avoided. | ||
|
|
||
| {{% alert title="Note" color="info" %}} | ||
| This pattern is only good when used on singleton table. Read more about [Singleton Table](https://alguidelines.dev/docs/navpatterns/patterns/singleton/singleton-table/). |
|
|
||
| ## Best Practice: Use IsEmpty | ||
|
|
||
| Instead of relying on insert failures, ensure the record’s uniqueness upfront using a number series or an incremental primary key. If the data comes from an external source where duplicates might exist, use IsEmpty to check for existence. |
Comment on lines
+42
to
+44
| if SomeRecord.IsEmpty() then | ||
| SomeRecord.Insert() | ||
| else |
|
|
||
| Instead of relying on insert failures, ensure the record’s uniqueness upfront using a number series or an incremental primary key. If the data comes from an external source where duplicates might exist, use IsEmpty to check for existence. | ||
|
|
||
| ```AL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.