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



Henrik Helgesen (TheDoubleH)
approved these changes
Jun 4, 2025
Henrik Helgesen (TheDoubleH)
requested review from
Jeremy Vyska (JeremyVyska),
Arend-Jan Kauffmann (ajkauffmann) and
waldo (waldo1001)
June 4, 2025 06:12
Jeremy Vyska (JeremyVyska)
approved these changes
Jun 9, 2025
Jeremy Vyska (JeremyVyska)
left a comment
Collaborator
There was a problem hiding this comment.
I like it. Thanks!
Jeremy Vyska (JeremyVyska)
requested a review
from Jesper Schulz-Wedde (JesperSchulz)
June 9, 2025 08:53
Henrik Helgesen (TheDoubleH)
requested review from
Jesper Schulz-Wedde (JesperSchulz),
Arend-Jan Kauffmann (ajkauffmann) and
waldo (waldo1001)
and removed request for
Jesper Schulz-Wedde (JesperSchulz),
Arend-Jan Kauffmann (ajkauffmann) and
waldo (waldo1001)
December 3, 2025 20:45
Copilot started reviewing on behalf of
Henrik Helgesen (TheDoubleH)
June 17, 2026 00:13
View session
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.