Bearbeiten

Freigeben über


AL code actions

CHANGED IN: Business Central 2024 release wave 2

Note

We're working on improving the onboarding experience for AL developers. If you've input for this article, we'd love to hear from you. Please use the Feedback section at the bottom of this article to help us make improvements that you’d like to see.

We also welcome contributions to our docs. New to contributing to docs? Don't worry, read more about contributing here Contribute to the help.

The AL Language extension for Microsoft Dynamics 365 Business Central can help users fix issues in the code. Code Actions is a Visual Studio Code feature that provides the user with possible corrective actions right next to an error or warning. If actions are available, a light bulb appears next to the error or warning. When the user chooses the light bulb (or presses Ctrl+.), a list of available code actions is presented. A code action can be applied to a single instance or a broader scope depending on the type of action.

In AL Language extension for Microsoft Dynamics 365 Business Central, the following code actions are available in the current version:

  • Multiple IF to CASE converting code action
  • Spell check code action
  • Interface implementer
  • Make method local
  • Use parenthesis for method call fix for instance, document, project, or workspace.
  • Fix explicit with statements on the instance, document, project, or workspace.
  • Fix implicit with statements on the instance, document, project, or workspace.
  • Fix old report layout and replace with rendering layout section
  • Fix for AW0013
  • Convert pages or page extensions to use the actionRef syntax for promoted actions on the action bar. Fix for instance, document, project, or workspace. Learn more in Code action for actions.
  • Set the default value for ApplicationArea on a page or a report level and remove all duplicates on field level. This code action can be applied to an object, a document, a project, or a workspace.
  • Convert existing event parameter in event subscribers from string literal to new identifier format. Fix event subscriber on the specific EventSubscriber instance, the active file, the active project, or the whole workspace. This makes it easy and controllable to opt in to the new syntax.
  • Insert using statement for a missing namespace. Fix for instance, document, project, or workspace.
  • Move the tooltip from page controls to table fields or clean them up from the page in case of duplicates. Learn more in Tooltip property.
  • Fix code to use the this keyword for self-reference and code readability. Learn more in Use the this keyword for codeunit self-reference.

Examples

The spell check code action is triggered on certain syntax errors:

spell check code action

The make method local action is triggered to fix the CodeCop Warning AA0207:

make method local code action

Code actions for promoted actions

Use the code action to convert legacy syntax for promoted actions to the actionref syntax, which is introduced with Business Central 2022 release wave 2. In-client customizations, user personalization, and profile configurations are automatically converted into the new syntax, so this is primarily applicable to DEV extensions. The code action can apply to a single instance, the document, the project, or the workspace.

Options for applying code action on actionref syntax

Note

For Designer extensions, use F6 to open Designer, which opens the page where the legacy syntax is used. Choose the Lock symbol and use Unlock page to automatically convert the legacy syntax for the running code. Selecting Alt+F6 will bring you back into Visual Studio Code, and show the converted actionref code.

To enable AL Code Actions

  1. Open the Command Palette by selecting Ctrl+Shift+P and then open the settings.json file.
  2. Enter the setting al.enableCodeActions and set it to true like this "al.enableCodeActions": true
  3. Save the settings file. You have now enabled code actions on your project.

Alternatively:

  1. Open the settings page, Ctrl+, and choose either User Settings or Workspace Settings depending on which scope you want the code actions to apply to.
  2. Navigate to Extensions > AL Language extension configuration.
  3. Choose the Enable Code Actions checkbox. You've now enabled code actions on your project.

AL development environment
AL outline view
AL Formatter
Directives in AL