Hello and welcome, my name is Daniel
Rimmelzwann and in this video I'll answer the question "How do I add A 
L code to an extension from Microsoft Dynamics 365 Business Central.
To accomplish our goal, we will go through three easy steps.
Code units I use the whole the logic for business
functionality. In step 1, I will show you how to create a new code unit object.
AL code is structured in smaller pieces called "procedures".
In step 2, I will show you how you can add a "procedure" to the code unit.
Extending Dynamics 365 logic is done by subscribing to events in
the standard application. In the final step, I will show you two ways to subscribe to
the same event - one way is from a code unit and the other one is from a table
extension. Let's get started.
We are in Visual Studio code and I have my bookshelf workspace
opened. For the first step, we need to create a new code in an object. Let's create a new
file and we'll call it the "CustomerCode.al".
Coding answer a fairly easy objects, so we can
create it without using a snippet. Just type the word "codeunit".
Give it a number.
Give it a name.
Add curly braces. 
And that is all you need to generate the object. For the second step, we will 
add a procedure. These are a little more complicated, so let's use the "procedure snippets".
Of remove the "local scope indicator" because we want to call this procedure
from other objects, and then we tab over to the procedure name - and 
let's call this "Celebrate.
NewCustomer".
And then tab into
the parameters sections - and we use
two parameters, one for the customer record variable and one for an extra
message. Final tap stop, is into the variable section, because we
don't need any variables in this particular piece of code, I'll just remove it.
The code itself is a simple message that displays the new customers number, plus the
initial message string.
Now you will subscribe to the customer tables on after insert event, which can be done
in two ways. The first is using a procedure in a code unit. An
event subscriber is a regular procedure with an additional subscription attribute. You
already know how to create a new procedure, so I'll just space it in here. To tie the procedure to the event,
we have to decorate the procedure with the subscription attribute. Luckily,
there's a snippet for it - te event 
sub. First we specify the object type - table in this case.
Next we tab over to the object id, where we enter the number of customer table.
Next, we tab over to the event definition, which is called "On
After
Insert
Event". It's important that you spell this
correctly. The element name must be empty and the remaining two parameters are both set to true.
Another way to subscribe to table events, is to add triggers the
table extension objects. Let's open the table extension object that
extends the customer table.
You can add a trigger to this table extension underneath
the field section. You can use the "t trigger
snippet". Intellisense will show you the events that you can subscribe to.
And "on after insert" is one of the available selections. Because we're no longer in the code 
unit we have to declared as a variable.
We can then call the "celebrate new customer procedure".
from there. Let's see what happens in Dynamics 365. When I click
on new customer, and I hit okay,
and get the first message, which is from the table extension,
and the second message is from the code unit.
In summary, first we created a new code unit object
that holds for custom logic to extend Dynamics 365.
Next we added a new procedure to the code unit to "celebrate a new customer". And
finally, we subscribed to the "on after insert event" of the customer table and we added a
call to our new procedure. We 
did this in two different ways and it is up to you to decide, when to use each method.
Topics covered in this video are creating code unit
objects, adding procedures to code units,
subscribing to events. We've also covered how to use snippets in Visual Studio code.
For more information please follow the link
on your screen. Thank you for watching this video and have a nice day.
