Monday, January 1, 2024

Interface in Business Central

Hello Friends,

Interfaces are a tricky concept that can be difficult to understand. In this article, you will learn how to use interfaces in Business Central step by step. By following an existing interface, you will understand how to implement it to create your own interfaces.

  • What Is An Interface
  • How To Use An Existing Interface
  • Example Setup and Test
  • How To Call An Interface
  • Summary

What Is An Interface


An Interface is a concept that defines a set of methods that codeunits can implement. It does not contain any logic or implementation itself but specifies methods that must be implemented by a codeunit that implements the interface.

Business Central interfaces were introduced in 2020. They are a way of extending Business Central functionality without the need for events. Their benefits are that they enable abstraction, decoupling, and scalability.

How To Use An Existing Interface


Interfaces can be a bit difficult to understand at first. I’ve found that the best way to learn how to create an interface is to use an existing one. This way, you will understand the concept and the steps to follow.

E-Document Core is a new application introduced in BC23. We will be using an interface in this application for this example. We will follow this link to implement the interface.

Extending e-documents functionality – Business Central | Microsoft Learn

First, we need to identify the interface to implement. The “AL Explorer” can help with it. As you can see, there are two interfaces for this module. For simplicity, we will just implement the interface called “E-Document”.

To use the interface we have to “implement” it. You can do it by creating a codeunit like this one. When you hover over “E-Document” you will see that all these methods are needed:

You can create them manually. However, you can do it automatically using the tooltip “Implement Interface”.

And all the procedures will be created empty for you.

If you drill down on “E-Document” you will be able to see the interface with all its methods.

Finally, create an enumextension extending “E-Documet Format” and using our implementation.

The key here is that the enum can be extended with more values where each of them can have a completely different implementation. Giving the flexibility mentioned above.

The enum “E-Document Format” just implements the interface “E-Document”

At this point, the foundation is established. But there is no logic in our implementation yet. Let’s create a simple example.

Inside the “Check” function we will do a Testfield in “Your Reference” field like this:


Example Setup And Test


Let’s set it up in Business Central. We have to follow 4 steps:

  • Create an E-Document Service
  • Create an E-Document Workflow
  • Create a Document Sending Profile
  • Assign the Document Sending Profile to the customer

At this point, we can go ahead and create a new “E-Document Service” and select our enum value.

Create a very simple workflow like the following. Choosing the service just created.

Create a new Document Sending Profile by choosing the workflow just created:

Finally, assign it to the customer:

Now, when you try to release a sales document for this customer without “Your Reference” you will get an error:

This is just a simple example of an interface where we only created logic for one procedure.

How To Call An Interface


This interface, in particular, is called from this event in “E-Document Subscription” codeunit in E-Document core app:

We arrive at this procedure in which we can see that the interface is called with these two lines:

EDocumentInterface := EDocumentService."Document Format";
EDocumentInterface.Check(EDocSourceRecRef, EDocumentService, EDocumentProcessingPhase);

You can find another example in “E-Doc. Import” codeunit.


Summary


In short, what you need to use an interface is:

  1. Identify the interface to implement
  2. Implement it using a codeunit with its methods
  3. Identify the enum implementing the interface
  4. Extend the enum with your custom codeunit implemented

I hope this help someone to understand Interface

Thank you for reading.

Keep Sharing....Keep Growing...