Please wait
Talk to Expert
Microsoft Dynamics Business central

Interface in AL – Business Central

Interface in AL

Introduction:

Interface are a powerful tool for building flexible and adaptable code in Microsoft Dynamics 365 Business Central (BC). Introduced in 2020, Interface in AL offer significant benefits over traditional event-based extensions, allowing you to achieve cleaner code organization and easier code reuse. Which was already available in almost all the other languages.

This blog dives deep into interface in AL, guiding you through their concepts, creation, and practical application in real-world scenarios.

What Is Interface in AL?

Interface is a blueprint of the Procedures (methods) that various objects can implement. It declares the methods are available but does not define their specific code execution. Think of it as a contract that specifies what needs to be done without dictating how it’s done. This separation allows for flexibility and code reusability.

Benefits of Using Interface:

  • Reduced Dependency on Implementation Details: Interface decouple code from the actual execution of methods. This makes the code adaptable to changes and easier to maintain.
  • Enhanced Code Reusability: You can create codeunits (reusable blocks of code) that implement multiple interface, making them versatile and applicable in various scenarios, promoting code efficiency and reducing redundancy.
  • Polymorphism: Interface enable polymorphic programming, where variables can hold objects of different types as long as they implement the same interface. This allows you to write generic code that works with various implementations seamlessly.

How to use Interface in AL?

First, we have to create an interface object in AL. Then we will use the ‘Implements’ keyword in the objects which contains the methods we’re going to use via interface. Here in AL we have the tinterface snippet which will give the object structure of interface.

Step 1: Creation of Interface

Interface Definition:

  • Use the interface keyword followed by the desired interface name.
  • Specify the methods (functions) the interface requires, including their names and parameters.

Here we have one interface named as CustomerPaymentMethod that we’ll use in sales process based on the properties of customer. This interface is having one procedure AssignPaymentMethod to assign the payment method on the sales order. Interface can only have the syntax of methods to declare them, and it cannot have the definition of particular method that we’re calling from interface.

Create Interfaces:

  • CustomerPaymentMethod: Contains a method named AssignPaymentMethod.
  • CustomerPaymentTerms: Contains a method named AssignPayTerms.

Interface in AL object with one procedure

We have created another interface object named as CustomerPaymentTerms to define the payment terms on sale order. This interface is also having a procedure declared That we will  define in the codeunits later. Thing here to note is we can use multiple procedures in single interface as well. Just to describe two different scenarios we are using two interfaces.

AL object Interface with one procedure

Step 2 : Create Enum

Create a custom enum field on the customer table to distinguish between customer types if the customer is a company (different legal entity) or Individual (as person).

Extends the table object of customer and added a field of Type Enum

 

Extends the page object of customer card and added new field

Step 3: Implement Interface with Enum/Codeunit

  • Use the implements keyword in codeunits to declare their interface compliance.
  • Implement all methods defined in the implemented interfaces.

Implement Interface with Enum:

  • Create a custom field on the Customer table (e.g., CustomerType) using an enum with values like “Company” and “Individual.”
  • Implement the CustomerPaymentMethod interface with this enum.

Implement Interface in Codeunits:

  • Create two codeunits: CompanyCustomer and IndividualCustomer implementing both interface.
  • Define the logic for assigning payment methods and terms within each codeunit’s methods.

Here to take a note we can implement the enum values to single interface only. So, we are going to implement CustomerPaymentMethod interface with this enum. We need to define the codeunit implemented by this interface against the enum values as shown below in the implementation property.

Create an Enum that implements the interface with two values associated with two codeunits

For codeunits we can implement multiple interfaces which must have all the procedures from the interfaces they’re implemented to. but all the procedures from all the implemented interfaces should be defined in the codeunit. here I have differ the code in both codeunits to substitute based on conditions.

A codeunit that implements two interface and includes all procedures of them

 

A codeunit that implements two interface and includes all procedures of them

Step 4: Using Interface in Code:

  1. Assigning Enum to an Interface: Assign the enum value based on customer type (company or individual)  to determine which codeunit implementing the interface.
  2. Without using Enum- Assigning codeunit: Assign a codeunit directly based on any conditions (e.g., consider individual customers with high balances as companies). This scenario wouldn’t use an enum.

Here are the actual use of interface in AL that we will use in the below examples by using both scenarios mentioned above

Examples : Interface in AL

Assigning Enum to an Interface:

I have created an action that will modify the payment method and payment terms field based on the implemented codeunits. First we’re getting the record of customer which is selected on sell-to customer no. field than by using the customer type enum field we’ve defined on customer master  we’re assigning it into the interface variable of CustomerPaymentMethod.

if the enum has been implemented to an interface than only We can only assign an enum to interface variable . By assigning that enum it will consider the codeunit implemented to that particular enum value and that’s how it’ll return the text from procedure AssignPaymentMethod in the message box. If the selected customer will be having company in Customer Type field it will use the codeunit Company Customer to call the procedures via interface as we have implemented this codeunit against the enum value and same the other way.

Without using Enum- Assigning codeunit:

We want to consider an individual customer as company if that customer is having Balance (LCY) more than 10000. For this logic we will not going to use enum as it’s with multiple conditions.

Here we will assign direct the particular codeunit based on conditions to an interface CustomerPaymentTerms. so, the interface will use the procedures from assigned codeunit only as shown below.

an action on sales order page to show the example with interface in AL

Output:

If the Customer Type is Individual and balance (LCY) is more than 10000 :

input values for example 1 - Interface in AL

 

Output example 1


Output example 1

input values for example 2

Output example 2

Output example 2

Conclusion:

Interface empower you to write modular, flexible, and maintainable code in Dynamics 365 Business Central. By understanding their principles and implementation, you can leverage interface to streamline your development process and create adaptable solutions.

Additional Resources

Please follow the link below to know more about Interface in AL.

Microsoft Documentation: https://learn.microsoft.com/en-us/training/modules/business-central-interfaces/1-introduction

Do you have questions about applying interface in AL? Feel free to reach out for further guidance or support in customizing your Business Central solution. Our experts at Madhda Business Solutions offers tailored advice and support to ensure a seamless solution.


Comments