{"id":319,"date":"2020-06-14T07:00:00","date_gmt":"2020-06-14T07:00:00","guid":{"rendered":"http:\/\/fredborg.org\/?p=319"},"modified":"2020-12-23T20:45:48","modified_gmt":"2020-12-23T20:45:48","slug":"interfaces-in-business-central","status":"publish","type":"post","link":"https:\/\/fredborg.org\/?p=319","title":{"rendered":"Interfaces in Business Central"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Interfaces are one of those things that most developers that have worked with other systems than NAV has always been hoping for, and now we finally got them, although in a limited edition since to get the most out of interfaces your programming language has to be OOP, which AL is not, one can only hope for the&nbsp;future&nbsp;&#x1f609;&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">All source code can be found here:&nbsp;<a href=\"https:\/\/dfredborg.visualstudio.com\/Business%20Central\/_git\/Business%20Central?version=GBinterfaces\" rel=\"noreferrer noopener\" target=\"_blank\">https:\/\/dfredborg.visualstudio.com\/Business%20Central\/_git\/Business%20Central?version=GBinterfaces<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">So, what is an interface? &#x1f914; Well the name kind of gives it away, it is a concept that will&nbsp;allow&nbsp;you to protect your code, by giving other functions the ability to&nbsp;implement&nbsp;your interface instead of calling the underlying code directly.&nbsp;It is also a way to implement a strong contract, forcing other people to implement everything the way that you have intended.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In&nbsp;Business&nbsp;Central the real power in interfaces is that it allows you extend your code more easily, one of the most common problems that we have faced in AL is that you have not been able to extend the option type, to solve this we got the&nbsp;Enum&nbsp;data type, which was a step in the correct direction, but we still had a problem if we wanted to be able to change what happened when a certain option was&nbsp;chosen. With interfaces, you now&nbsp;could&nbsp;extend your&nbsp;enums&nbsp;and their&nbsp;functionality, so let us&nbsp;look&nbsp;at how you can do this.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First thing you&nbsp;must&nbsp;do is create a new object of type Interface and add at least one function, now it is important to note that interfaces can only contain functions with no code, but only as a signature. And you should also note, that whenever one wishes to implement your interface, they must implement all the functions, in your interface object. In the code below I have created an interface called&nbsp;IGetMyCode&nbsp;with a simple function&nbsp;GetColor&nbsp;that returns a text:&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\ninterface &quot;IGetMyColor&quot;\n{\n  procedure GetColor(): Text;\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Next, we need to create some&nbsp;codeunits&nbsp;that implements our interface, this is done by adding the&nbsp;implements&nbsp;IGetMyColor, and it must contain a function with the same name as the interface which was&nbsp;GetColor, I have created three simple&nbsp;codeunits&nbsp;Red, Blue, and Green that just exits with a text value.&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\ncodeunit 50100 Red implements IGetMyColor\n{\n    procedure GetColor(): Text\n    begin\n        exit(&#039;Red&#039;);\n    end;  \n \n}\n\n\ncodeunit 50101 Blue implements IGetMyColor\n{\n    procedure GetColor(): Text\n    begin\n        exit(&#039;Blue&#039;);\n    end;  \n \n\n}\ncodeunit 50102 Green implements IGetMyColor\n{\n    procedure GetColor(): Text\n    begin\n        exit(&#039;Green&#039;);\n    end;  \n \n\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Next,&nbsp;we need to implement our interface in our&nbsp;enum&nbsp;object, you must add an Implementation where you define which&nbsp;function&nbsp;you wish to implement and which version of the interface that you wish to implement.&nbsp;<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nenum 50100 &quot;Colors&quot; implements IGetMyColor\n{\n    Extensible = true;\n\n\n    value(0; Blue)\n    {\n        Caption = &#039;Blue&#039;;\n        Implementation = IGetMyColor = Blue;\n    }\n\n\n    value(1; Red)\n    {\n        Caption = &#039;Red&#039;;\n        Implementation = IGetMyColor = Red;\n\n\n    }\n\n\n    value(2; Green)\n    {\n        Caption = &#039;Green&#039;;\n        Implementation = IGetMyColor = Green;\n\n    }\n}\n\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">And that is it, I know this was a very simple implementation of interfaces, and&nbsp;you&nbsp;can do a lot more with interfaces than this example if you are interested in interfaces and to see what you can use them for, please consider taking a look at how Microsoft has implemented interfaces in the Sales Price&nbsp;function.&nbsp;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I am really looking forward to&nbsp;seeing&nbsp;how people are going to start using Interfaces across their extensions to make the code&nbsp;easier&nbsp;to&nbsp;integrate and to see if Microsoft is going to implement even more concepts from OOP &#x1f91e;. Well until next time stay safe.&nbsp;&nbsp;&#x1f637;<\/p>\n\n\n\n<figure><iframe loading=\"lazy\" width=\"560\" height=\"315\" src=\"https:\/\/www.youtube.com\/embed\/m2cH8TAD03E\" allowfullscreen=\"\"><\/iframe><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Interfaces are one of those things that most developers that have worked with other systems than NAV has always been&#8230; <a class=\"read-more\" href=\"https:\/\/fredborg.org\/?p=319\" rel=\"nofollow\"> Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":320,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,1],"tags":[],"class_list":["post-319","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-business-central","category-uncategorized","shapla-grid-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Interfaces in Business Central - Fredborg<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/fredborg.org\/?p=319\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Interfaces in Business Central - Fredborg\" \/>\n<meta property=\"og:description\" content=\"Interfaces are one of those things that most developers that have worked with other systems than NAV has always been... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/fredborg.org\/?p=319\" \/>\n<meta property=\"og:site_name\" content=\"Fredborg\" \/>\n<meta property=\"article:published_time\" content=\"2020-06-14T07:00:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-23T20:45:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/fredborg.org\/wp-content\/uploads\/2020\/06\/Interfaces.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"dfredborg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@dfredborg\" \/>\n<meta name=\"twitter:site\" content=\"@dfredborg\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"dfredborg\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/fredborg.org\/?p=319#article\",\"isPartOf\":{\"@id\":\"https:\/\/fredborg.org\/?p=319\"},\"author\":{\"name\":\"dfredborg\",\"@id\":\"https:\/\/fredborg.org\/#\/schema\/person\/59a5520cbf04c6bd1267f30b4488e71d\"},\"headline\":\"Interfaces in Business Central\",\"datePublished\":\"2020-06-14T07:00:00+00:00\",\"dateModified\":\"2020-12-23T20:45:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/fredborg.org\/?p=319\"},\"wordCount\":582,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/fredborg.org\/#\/schema\/person\/59a5520cbf04c6bd1267f30b4488e71d\"},\"image\":{\"@id\":\"https:\/\/fredborg.org\/?p=319#primaryimage\"},\"thumbnailUrl\":\"https:\/\/fredborg.org\/wp-content\/uploads\/2020\/06\/Interfaces.png\",\"articleSection\":[\"Business Central\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/fredborg.org\/?p=319#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/fredborg.org\/?p=319\",\"url\":\"https:\/\/fredborg.org\/?p=319\",\"name\":\"Interfaces in Business Central - Fredborg\",\"isPartOf\":{\"@id\":\"https:\/\/fredborg.org\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/fredborg.org\/?p=319#primaryimage\"},\"image\":{\"@id\":\"https:\/\/fredborg.org\/?p=319#primaryimage\"},\"thumbnailUrl\":\"https:\/\/fredborg.org\/wp-content\/uploads\/2020\/06\/Interfaces.png\",\"datePublished\":\"2020-06-14T07:00:00+00:00\",\"dateModified\":\"2020-12-23T20:45:48+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/fredborg.org\/?p=319#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/fredborg.org\/?p=319\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/fredborg.org\/?p=319#primaryimage\",\"url\":\"https:\/\/fredborg.org\/wp-content\/uploads\/2020\/06\/Interfaces.png\",\"contentUrl\":\"https:\/\/fredborg.org\/wp-content\/uploads\/2020\/06\/Interfaces.png\",\"width\":1920,\"height\":1080},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/fredborg.org\/?p=319#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/fredborg.org\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Interfaces in Business Central\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/fredborg.org\/#website\",\"url\":\"https:\/\/fredborg.org\/\",\"name\":\"Fredborg\",\"description\":\"Business Central and Azure\",\"publisher\":{\"@id\":\"https:\/\/fredborg.org\/#\/schema\/person\/59a5520cbf04c6bd1267f30b4488e71d\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/fredborg.org\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/fredborg.org\/#\/schema\/person\/59a5520cbf04c6bd1267f30b4488e71d\",\"name\":\"dfredborg\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/fredborg.org\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/fredborg.org\/wp-content\/uploads\/2023\/06\/download.png\",\"contentUrl\":\"https:\/\/fredborg.org\/wp-content\/uploads\/2023\/06\/download.png\",\"width\":256,\"height\":256,\"caption\":\"dfredborg\"},\"logo\":{\"@id\":\"https:\/\/fredborg.org\/#\/schema\/person\/image\/\"},\"sameAs\":[\"https:fredborg.org\",\"https:\/\/www.linkedin.com\/in\/dfredborg\/\",\"https:\/\/x.com\/dfredborg\",\"https:\/\/www.youtube.com\/channel\/UCUNZglLDMjBlOK_T9JtI8UA\"],\"url\":\"https:\/\/fredborg.org\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Interfaces in Business Central - Fredborg","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/fredborg.org\/?p=319","og_locale":"en_US","og_type":"article","og_title":"Interfaces in Business Central - Fredborg","og_description":"Interfaces are one of those things that most developers that have worked with other systems than NAV has always been... Read more","og_url":"https:\/\/fredborg.org\/?p=319","og_site_name":"Fredborg","article_published_time":"2020-06-14T07:00:00+00:00","article_modified_time":"2020-12-23T20:45:48+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/fredborg.org\/wp-content\/uploads\/2020\/06\/Interfaces.png","type":"image\/png"}],"author":"dfredborg","twitter_card":"summary_large_image","twitter_creator":"@dfredborg","twitter_site":"@dfredborg","twitter_misc":{"Written by":"dfredborg","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/fredborg.org\/?p=319#article","isPartOf":{"@id":"https:\/\/fredborg.org\/?p=319"},"author":{"name":"dfredborg","@id":"https:\/\/fredborg.org\/#\/schema\/person\/59a5520cbf04c6bd1267f30b4488e71d"},"headline":"Interfaces in Business Central","datePublished":"2020-06-14T07:00:00+00:00","dateModified":"2020-12-23T20:45:48+00:00","mainEntityOfPage":{"@id":"https:\/\/fredborg.org\/?p=319"},"wordCount":582,"commentCount":0,"publisher":{"@id":"https:\/\/fredborg.org\/#\/schema\/person\/59a5520cbf04c6bd1267f30b4488e71d"},"image":{"@id":"https:\/\/fredborg.org\/?p=319#primaryimage"},"thumbnailUrl":"https:\/\/fredborg.org\/wp-content\/uploads\/2020\/06\/Interfaces.png","articleSection":["Business Central"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/fredborg.org\/?p=319#respond"]}]},{"@type":"WebPage","@id":"https:\/\/fredborg.org\/?p=319","url":"https:\/\/fredborg.org\/?p=319","name":"Interfaces in Business Central - Fredborg","isPartOf":{"@id":"https:\/\/fredborg.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/fredborg.org\/?p=319#primaryimage"},"image":{"@id":"https:\/\/fredborg.org\/?p=319#primaryimage"},"thumbnailUrl":"https:\/\/fredborg.org\/wp-content\/uploads\/2020\/06\/Interfaces.png","datePublished":"2020-06-14T07:00:00+00:00","dateModified":"2020-12-23T20:45:48+00:00","breadcrumb":{"@id":"https:\/\/fredborg.org\/?p=319#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/fredborg.org\/?p=319"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/fredborg.org\/?p=319#primaryimage","url":"https:\/\/fredborg.org\/wp-content\/uploads\/2020\/06\/Interfaces.png","contentUrl":"https:\/\/fredborg.org\/wp-content\/uploads\/2020\/06\/Interfaces.png","width":1920,"height":1080},{"@type":"BreadcrumbList","@id":"https:\/\/fredborg.org\/?p=319#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/fredborg.org\/"},{"@type":"ListItem","position":2,"name":"Interfaces in Business Central"}]},{"@type":"WebSite","@id":"https:\/\/fredborg.org\/#website","url":"https:\/\/fredborg.org\/","name":"Fredborg","description":"Business Central and Azure","publisher":{"@id":"https:\/\/fredborg.org\/#\/schema\/person\/59a5520cbf04c6bd1267f30b4488e71d"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/fredborg.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/fredborg.org\/#\/schema\/person\/59a5520cbf04c6bd1267f30b4488e71d","name":"dfredborg","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/fredborg.org\/#\/schema\/person\/image\/","url":"https:\/\/fredborg.org\/wp-content\/uploads\/2023\/06\/download.png","contentUrl":"https:\/\/fredborg.org\/wp-content\/uploads\/2023\/06\/download.png","width":256,"height":256,"caption":"dfredborg"},"logo":{"@id":"https:\/\/fredborg.org\/#\/schema\/person\/image\/"},"sameAs":["https:fredborg.org","https:\/\/www.linkedin.com\/in\/dfredborg\/","https:\/\/x.com\/dfredborg","https:\/\/www.youtube.com\/channel\/UCUNZglLDMjBlOK_T9JtI8UA"],"url":"https:\/\/fredborg.org\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/fredborg.org\/index.php?rest_route=\/wp\/v2\/posts\/319","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fredborg.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fredborg.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fredborg.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fredborg.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=319"}],"version-history":[{"count":4,"href":"https:\/\/fredborg.org\/index.php?rest_route=\/wp\/v2\/posts\/319\/revisions"}],"predecessor-version":[{"id":352,"href":"https:\/\/fredborg.org\/index.php?rest_route=\/wp\/v2\/posts\/319\/revisions\/352"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/fredborg.org\/index.php?rest_route=\/wp\/v2\/media\/320"}],"wp:attachment":[{"href":"https:\/\/fredborg.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fredborg.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fredborg.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}