Tuesday, June 2, 2020

AL Interfaces in Microsoft Dynamics 365 Business Central

Hi Readers,
Microsoft Dynamics 365 Business Central is enhancing and in April 2020, came with new features and advancements. 
To know what all accessible features do we have in Microsoft Dynamics 365 Business Central 2020 Wave 1 release, refer Business Central 2020 Updates

There is one interesting feature about which I wanted to write from forever, and that is AL Interfaces. 

Now, what are Interfaces, how to use them and what are its benefits? I'll try to explain it as simply as possible. 

Say, you have a Codeunit A and in that, you create a method Test wherein it will get the Name of the Customer. 
Now, in another Codeunit B, you need the same method Test. Here, you have to get the Name but of a Vendor.

Business Central gives you the facility to declare the method just once and uses it wherever you want. Yes, I am talking about AL Interfaces. This gives you a level of abstraction and security. You have to declare the method once and implement the interface. It also helps in better code structure, reusability and reduces dependency. All you have to do is access the Interface and you will have access to its methods. This answers your what and why questions. 

Now, how do we implement such a thing? Let's take a simple example and go step by step. 

Scenario: Return 'This is Codeunit A'  and 'This is Codeuntt B' from methods of Codeunit A and Codeunit B respectively. 

Here, Codeunit A and Codeunit B want the same functionality but want us to return different Texts. Instead of writing separate methods, we'll use AL Interfaces.
1. Create an AL Interface as shown below:

2. For you to use this method, we'll use it in Codeunit A and Codeunit B. The method of doing that is called implementing the Interface. Implement Interface IText in Codenit A and Codeunit B as shown below:


I hope this blog was helpful and provided an easy explanation.

Write your suggestions in the comment section.
You can also email me at jyotsnaoberainav@gmail.com

Also, visit my website JONAV if you need help with any NAV/BC Services!

Thank you!
 

6 comments:

  1. Hi, I am Krishna a NAV/BC technical consultant.
    Just a doubt-

    Here if we will not use interface in cideunit A and B still it will give same output whatever a particular method is returning. Isn't?

    ReplyDelete
    Replies
    1. Hello Krishna, thank you for taking in interest.
      Yes, you can. What interfaces does
      is declares methods and the Codeunit has to implement all the methods if it implement Interface. It give you a level of abstraction and other things mentioned. Do check other blogs too! Thank you :)

      Delete
  2. Yes exactly... It provides a abstraction level that force to implement all the methods mentioned in the interface and multiple ways to call methods.
    Thanks.

    ReplyDelete
  3. Hii, I am hitesh. I have doubt that if I'll implement interface in codeunit and that interface contain many methods then it's necessary to use this methods in that codeunit even there is no use of them ??

    ReplyDelete
    Replies
    1. Hello Hitesh, thank you for the comment. The simple answer is yes. If you have an interface and a codeunit implements it, it has to consider all the methods. The main use of interface is it provided a sense of abstraction and proper way of dealing with the code. What I mean is, if you have an interface it will only contain the valid method say CRUD operations for Customer, then when a codeunit will use it then it'll know all the methods he will need to take. Each interface will be having legit methods.
      Hope this helped.
      Keep following! :)

      Delete