Skip to content

Add documentation on 'if not Insert then Modify' best practices#256

Open
StefanSosic wants to merge 5 commits into
microsoft:mainfrom
StefanSosic:stso/IfNotInsertThenModify
Open

Add documentation on 'if not Insert then Modify' best practices#256
StefanSosic wants to merge 5 commits into
microsoft:mainfrom
StefanSosic:stso/IfNotInsertThenModify

Conversation

@StefanSosic

Copy link
Copy Markdown

image
image
image

@JeremyVyska JeremyVyska left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it. Thanks!

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants