Use AL code actions

Completed

The AL Language extension can help users to 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, these 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

  • Fix implicit with statements

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

Enable AL code actions

  1. Open the Command Palette by pressing 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.

Some code actions make sense to run in a larger context than for just the instance. If the code action supports it, there will be the option to run it either on the specific manifestation it's triggered from, within the document it's triggered from, within the project it's triggered from, or within the complete workspace. This allows developers to refactor code faster and more efficiently across a larger source scope without having to go to each specific manifestation to trigger the code action there.

In 2022 release wave 2, the following code actions support running in larger scopes:

  • Convert promoted actions

  • AA0008 - Add parenthesis

  • AA0241 - To lowercase

Examples of AL code actions

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

Screenshot of the spell check code action.

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

Screenshot of the make method local action.

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.

Screenshot of the code action.

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. Pressing Alt+F6 will bring you back into Visual Studio Code, and show the converted actionref code.