Skip to content

Add documentation for SetAutoCalcFields method#254

Open
StefanSosic wants to merge 4 commits into
microsoft:mainfrom
StefanSosic:stso/SetAutoCalcFields
Open

Add documentation for SetAutoCalcFields method#254
StefanSosic wants to merge 4 commits into
microsoft:mainfrom
StefanSosic:stso/SetAutoCalcFields

Conversation

@StefanSosic

@StefanSosic StefanSosic commented May 12, 2025

Copy link
Copy Markdown

image
image

@TheDoubleH TheDoubleH 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.

Nice and on point

@AndreasMoth

AndreasMoth commented May 13, 2025

Copy link
Copy Markdown

Looks great, do we want to mention you can SetAutoCalcFields on OnOpenPage as far as I recall (do double check), to have the field pre-calculated. Especially useful for lists if you really do need to calculate a field. This is only needed for fields not on the page however, as a FlowField shown on a page will always be calculated.
Maybe we also want to mention that CalcFields should only be used if you already have the record, if you're fetching it, always use SetAutoCalcFields.

@StefanSosic

Copy link
Copy Markdown
Author

Looks great, do we want to mention you can SetAutoCalcFields on OnOpenPage as far as I recall (do double check), to have the field pre-calculated. Especially useful for lists if you really do need to calculate a field. This is only needed for fields not on the page however, as a FlowField shown on a page will always be calculated.
Maybe we also want to mention that CalcFields should only be used if you already have the record, if you're fetching it, always use SetAutoCalcFields.

Double-checked and added both :)

@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.

Agreed, a must-have.

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

This PR adds a new Best Practice documentation page describing how and when to use the SetAutoCalcFields method in Business Central AL to improve FlowField retrieval performance.

Changes:

  • Introduces a new Best Practices article for SetAutoCalcFields, including description and general usage guidance.
  • Adds guidance for list pages and a comparison between SetAutoCalcFields and CalcFields.
  • Provides “Bad” vs “Good” AL code examples illustrating the performance impact.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

See the documentation on learn.microsoft.com for more information about [SetAutoCalcFields](https://learn.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/record/record-setautocalcfields-method).

{{% alert title="Note" color="info" %}}
Starting in Business Central 2025 release wave 1, the SetAutoCalcFields is also available on the RecordRef data type.
Comment on lines +31 to +32
## SetAutoCalcFields or CalcFields
It's best practice to use SetAutoCalcFields when fetching a record since it ensures automatic FlowField calculation. On the other hand, CalcFields should only be used if you already have the record and need to explicitly trigger the calculation. This distinction prevents unnecessary performance overhead, ensuring that database queries remain optimized.

```AL
Customer.SetFilter(Balance, '>%1', LargeCredit);
Customer.SetAutoCalcFields(Balance)
### Bad Code

```AL
Customer.SetFilter(Balance, '>%1' , LargeCredit);
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.

7 participants