Best Practices for AL Coding in Microsoft Dynamics Business Central (MSDYN365BC)

Hi ALL.

In this blog we will go through Best practices for AL Code which i normally use while doing AL code.

Microsoft has recommended best practices while developing extensions in AL to ensure the Steadiness of the objects as well as better readability of AL code. This best practice will help in maintaining a uniform structure of the extension which we would be building in AL.

How to maintain Extension Structure?

As per best practice in AL code, we should maintain a single Folder per extension which will include app.json, Launch.json, AL Objects, AL packages, Test toolsets, images.

I have added Folders such as Codeunits, Tables, Pages, Reports to easily access the Objects.

Best Practice in AL for MSDYN365BC

How to do File Naming ?

As shown in the above screenshot. The object name should be

<ObjectNameExcludingPrefix>.<FullTypeName>.al

i.e ActionHandler.codeunit.al or CreditBalanceNotification.pageext.al

you can you below recomended abbreviation for different objects.

ObjectAbbreviation
PagePage
Page ExtensionPageExt
Page CustomizationPageCust
CodeunitCodeunit
TableTable
Table ExtensionTableExt
XML PortXmlport
ReportReport
Request PageRequestPage
QueryQuery
EnumEnum
Enum ExtensionEnumExt
Control Add-insControlAddin
DotnetDotnet
ProfileProfile

How to do Formatting AL Code?

Microsoft has recommened

  1. Use Pascal Case (This means that the first letter of every word in the name is capitalized ). e.g CreditBalanceNotification or OpenCustomer.
  2. Four space indentation
  3. New line for Curly bracket.

You can use AL format by ShortCut Shift+Alt+F as shown in below screen shot. For more information on how to use it, see AL Formatter.

AL Code Formatting

How to Format File Structure?

File structure should be uniform through out the extension. Below is the recommended structure.

  1. Properties
  2. Object-specific constructs such as:
    • Table fields
    • Page layout
    • Actions
  3. Global variables
    • Labels (old Text Constants)
    • Global variables
  4. Methods

How to do Referencing?

Referencing is done by the object name.

 e.g.  TempCustomer: Record Customer Tempoprary;

         CreditBalanceNotification: Notification;

How to do Variable Naming ?

Variable Naming should be PascalCase if temporary variables are used then Temp should be the prefix.

How can we call method?

When calling a method, include one space after each command if you are passing multiple parameters. Parentheses must be specified when you are making a method call or system call such as: Init(), Modify(), Insert() etc.

e.g OpenCustomer(‘C0001’);

OpenCustomer(‘C0001’, Customer);

This are some of the Microsoft Standards which should be followed by the developers to make the code Unified and readable.

You can download Dynamics 365 Business Central 2019 Release Wave 2 and Install Microsoft Dynamics Business Central on premises 2019 Wave 2 Release

To get new updates and learn about Microsoft Dynamics 365 Business Central Microsoft Learn

How to Enable Data Encryption in Business Central Wave 2 Release (MSDYN365BC)

Hi ALL,

Happy Diwali.

In my previous blogs, I have mentioned many new properties and there use.In this blog we will focus on How to Enable Data Encryption in Business Central Wave 2 Release (MSDYN365BC) and how to check in AL VS Code that the functionality is enabled.

Encryption is a process that encodes a message or file so that it can be only be read by certain people who can decrypt the message which has its key.

Encryption keys are created by using algorithms designed to make sure that each key is unique and unpredictable. The keys that are used by Dynamics 365 Business Central are generated by the .NET Framework Data Protection API.

You can encrypt data on the Business Central server by generating new or importing existing encryption keys that you enable on the Business Central server instance that connects to the database

How to Enable Data Encryption in MSDYN365BC

Enable data encryption

The process of enabling the Data Encryption is shown in above screen shot.

  1. In the Search box, enter Data Encryption Management.
  2. Choose Enable Encryption.choose Yes.
  3. In the Set Password window, enter a password that protects the encryption key, and press OK.
  4. Export File will be downloaded, choose Save,
  5. Keep the file at safe location where the encryption key is stored, and then choose Save.

Use Data Encryption in AL.

Below mentioned code checks that Encryption is enabled or not.

You can use the logic according to your requirement by using Codeunit 1266 “Cryptography Management”.

Use Data Encryption in AL MSDYN365BC

You can download Dynamics 365 Business Central 2019 Release Wave 2 and Install Microsoft Dynamics Business Central on premises 2019 Wave 2 Release

To get new updates and learn about Microsoft Dynamics 365 Business Central Microsoft Learn

How to Manage Notifications in Microsoft Dynamics Business Central Wave 2 Release (MSDYN365BC)

I have been writing blogs on the amazing new feature of 2019 Wave 2 release , so this is additional to previous blogs.

Notifications provide a programmatic way to send non-intrusive information to the User Interface (UI) in the Web client.

You can use Notification and NotificationScope data types and methods in AL, Code can be added notify users. The following table provides an overview of the available methods.

MethodDescription
MESSAGESpecifies the content of the notification that appears in the UI.
SCOPESpecifies the scope in which the notification appears.
SENDSends the notification to be displayed by the client.
ADDACTIONAdds an action on the notification.
SETDATASets a data property value for the notification
GETDATAGets a data property value from the notification.
RECALLRecalls a sent notification.

So this is basics about Notification in Microsoft Dynamics Business Central Wave 2 Release.

Now lets have a look at the Code aspect in AL.

How to Create Notification in AL

In this example we will Create Message,Scope, Send, AddAction,SetData.

So lets start Creating the Extension.

Open VS Code and Press “Ctrl + Shift + P” to open Command Pallet.

Give Project Name as CreditBalanceNotification and Setup App.json file.Refer to My Blog on How to Connect Dynamics 365 Business Central 2019 Release Wave 2 to Visual Studio Code

Create a New file CreditBalanceNotification.al and Create Page Extension as shown below.

Notification in MSDYN365Bc

As shown in the above Screen shot CreditBalanceNotification is of having Data Type as Notification.

So from this variable we can access all the method which are stated in above table.

As specified in the ADDACTION we have used ActionHandler Codeunit, and below is the code for the same.

Notification in MSDYN365BC

This is simple code where if we get he Customer then it will open the Customer Page.

You can download the code from GitHub.

Now we will test this, if you go to sales order page and check a Sales Order for the Customer whose Balace is greater then Credit Limit then you will get this notification.

You can change the credit Limit by clicking on the link.

To get new updates and learn about Microsoft Dynamics 365 Business Central Microsoft Learn

Thanks for Reading.

How to enable Code Actions for AL Language in Microsoft Dynamics Business Central Wave 2 Release (MSDYN365BC)

Hi All,

I came across Amazing Feature “Code Action” in VScode, so i thought of sharing some insights with you all.

You can download Dynamics 365 Business Central 2019 Release Wave 2 and Install Microsoft Dynamics Business Central on premises 2019 Wave 2 Release

What is Code Action?

Code Action in AL Language help fix the code issue by providing possible Corrective actions.A light bulb will appear next to the error or warning.

Code Action provides possible corrective actions which will help the developer to code in better way.

How to Enable Code Action in VSCode?

Open the Settings Page in VSCode, and choose either User Settings or Workspace Settings depending on which scope you want the code actions to apply to.

Navigate to Files > Preferences > Search for Code Actions.

Click on the Enable Code Actions checkbox. You have now enabled code actions on your project.

Enable Code Analysis in MSDYN365BC

How does Code Action works in VSCode?

After enabling the Code Action, While developing any AL extension you will be getting the Light Buib Symbol on Right hand side while you are typing. Which is shown in the below mentioned Screen shot.

Code Action in MSDYN365BC

This will be benifitial for future development in AL.You can also refer ”How to set Code Analysis Tools”

Stay tuned for my Next blog on How to Manage Notifications in Microsoft Dynamics Business Central Wave 2 Release (MSDYN365BC).

To get new updates and learn about Microsoft Dynamics 365 Business Central Microsoft Learn

Thanks for reading

How to set Code Analysis Tools with the Ruleset in Microsoft Dynamics Business Central Wave 2 Release (MSDYN365BC)

Hi All,

In this topic we will discuss about, how you can use a custom ruleset to customize the severity of diagnostics produced by the code analysis tools that are part of the AL Language extension for Visual Studio Code.

You can download Dynamics 365 Business Central 2019 Release Wave 2

You can check my blog on Install Microsoft Dynamics Business Central on premises 2019 Wave 2 Release

Lets begin with the set of Code Analyzer in MSDYN365BC.

How to enable Code Analysis?

  1. Open Visual Studio Code
  2. Go to File > Preferences >Settings
  3. As mentioned in screenshot, search for CodeAnalyzer and Edit in Setting.json.
Enable Code Analyzer in MSDYN365BC

Now add Code in Setting.json file

“al.enableCodeAnalysis”: true,  

“al.codeAnalyzers”: [“${AppSourceCop}”,”${CodeCop}”,”${PerTenantExtensionCop}”,”${UICop}”]

For more information about the available analyzers, see AppSourceCopCodeCopPerTenantExtensionCop, and UICop.

Setting.json in Visual Studio Code MSDYN365BC

Now you have enabled the CodeAnalysis and CodeAnalyzers in Visual Studio.

In next step we will go through build Custom Ruleset in Busness Central wave 2.

Creating and customizing a ruleset

For this we will create a New file Custom.ruleset.json where i have specified “action” : “None” and saved it.

Custome.ruleset.json in MSDYN365BC

After Creating the NewRuleSet, we need to set this path in Setting.json.

Enable Rulesetpath in Visula studio for MSDYN365BC

To Test the RuleSet i have writen some code on Page extension and as soon as i write the code, i am getting the warning messages as show below.

Testing RuleSet in MSDYN365BC

So the new set RuleSet has been applied as you can see AL(AA0001) warning has been displayed which we have enabled in the Custom.rulset.json file.

For more information about custom rules, see ruleset for the Code Analysis tool.

You can download the code from GitHub.

If you need help to setup GitHub refer to my previous blog How to Connect VS Code to GITHUB in Microsoft Dynamics 365 Business Central (MSDYN365BC).

Stay tuned for my Next blog.

Thanks for reading

How to Set ObsoleteState and ObsoleteReason in Microsoft Dynamics Business Central Wave 2 Release (MSDYN365BC)

In my previous blogs, I have mentioned many new properties and there use. 

ObsoleteState: You can set this property to specify the object is removed or will be removed in the future.

ValueDescription
NoNot obsolete. This is the normal/default setting.
PendingWill be obsolete in a future release. This setting has no effect on the current use of the table, field, or key in code.
RemovedIs obsolete. Applies to tables and table fields; the element is not deleted from the database, however, references to the element in code are only allowed in upgrade codeunits. References from other objects will result in a runtime error.
ObsoleteState in MSDYN365BC

ObsoleteReason: You can specify the reason for discontinuing.

ObsoleteReason in MSDYN365BC

The above mentioned property can be applied to

  • Table objects
  • Table fields
  • Table keys
  • Codeunit
  • Enum
  • EnumValue
  • Page
  • PageAction
  • PageActionArea
  • PageActionGroup
  • PageActionSeparator
  • PageArea
  • PageChartPart
  • PageField
  • PageGroup
  • PageLabel
  • PagePart
  • PageSystemPart
  • Query
  • QueryColumn
  • QueryDataItem
  • QueryFilter
  • Report
  • ReportColumn
  • ReportDataItem
  • RequestPage
  • XmlPort

AL compiler will only detect ObsoleteState property, you will not get erros or warning while compiling from C/Side.

If you have set ObsoleteState as pending then you will face warning and if it have been marked as Removed you will get error.

This will be benifitial for future development in AL.

To get new updates and learn about Microsoft Dynamics 365 Business Central Microsoft Learn

Stay tuned and keep learning.

How to Set up VS Code to Docker in Microsoft Dynamics 365 Business Central wave 2 Release (MSDYN365BC)

Hi All,

In my previous blog, I have mention How to Connect VS Code to GITHUB in Microsoft Dynamics 365 Business Central (MSDYN365BC).

This blog is regarding configuring Visual Studio Code for Docker. I will recommend you to first refer to the blog posted by Saurav Dhyani on Setup Docker Container and Installing Dockers.

After installation is completed following shortcut will be created on your desktop

  • <Container name> Windows Client – opens the Windows client for your application in the container.
  • <Container name> Web Client – opens the Web client for the your application in the container.
  • <Container name> CSIDE – opens the Dynamics NAV Development Environment for developing your application using CSIDE.
  • <Container name> PowerShell Prompt – opens a Windows PowerShell prompt in the container. This gives you access to the Dynamics NAV (https://docs.microsoft.com/en-us/powershell/business-central/overview), which you can run against the container sandbox environment.
  • <Container name>Command Prompt – opens a Windows command prompt in the container.

You need the values for Dev. Server, Dev. ServerInstance, and Files parameters that you retrieved from the Windows PowerShell ISE console when you ran the powershell script.I will recommened you to save details in Text file for future reference.

Docker for MSDYN365BC

Open Visual Studio Code, go to Extensions, and then edit the “Server” as Dev.Server and “SeverInstance” as Dev. ServerInstance as shown in Below screen shot . You now have the AL Language extension enabled.

Press Ctrl+Shift+P and then choose AL Go!.

VS Code Docker MSDYN365BC

You have now set up Visual Studio Code with the AL Language extension.

You can enter a new support request to Microsoft Dynamics Technical Support​ from PartnerSource under Support if you have any querry.Advertisements

Stay tuned for my next blog.