Book Image

Microsoft Dynamics 365 Business Central Cookbook

By : Michael Glue
Book Image

Microsoft Dynamics 365 Business Central Cookbook

By: Michael Glue

Overview of this book

Microsoft Dynamics 365 Business Central is a complete business management solution that can help you streamline business processes, connect individual departments in your company, and enhance customer interactions. Ok. That first part was really professional sounding, right? Now, let’s get into what this cookbook is going to do for you: put simply, it’s going to help you get things done. This book will help you get to grips with the latest development features and tools for building applications using Business Central. You’ll find recipes that will guide you in developing and testing applications that can be deployed to the cloud or on-premises. For the old-schoolers out there, you’ll also learn how to take your existing Dynamics NAV customizations and move them to the new AL language platform. Also, if you haven’t figured it out already, we’re going to be using very normal language throughout the book to keep things light. After all, developing applications is fun, so why not have fun learning as well!
Table of Contents (11 chapters)

Creating new business logic

Adding a place to store data and interact with it is essential, but in all likelihood, you're going to need to manipulate the data or provide some logic for the user to interact with. In this recipe, we will create some business logic for our application.

Getting ready

We're going to need an AL project that's connected to a development sandbox to work in. We will continue to build on the project that we started in this chapter. You can download it from the GitHub link at the start of this chapter.

How to do it...

  1. Open your AL project folder in Visual Studio Code.
  2. In the Visual Studio Code Explorer, select the Television Show.al file. In the Editor tab, add two new fields named Last Aired and Created By, as follows:
field(6; "Last Aired"; Date)
{
}
field(7; "Created By"; Code[50])
{
Editable = false;
}
  1. When a new record is added to the Television Show table, we want to know who created it. We can do that by adding code to the OnInsert() trigger of the table so that our logic executes every time a record is inserted.

In the Television Show.al file, add the trigger and logic after the keys section. All we need to do is set the Created By field to the ID of the current user, as follows:

trigger OnInsert()
begin
"Created By" := UserId();
end;
You can use the trigger snippet to create a new trigger.
  1. Our next requirement is to make sure that if the user enters the Last Aired date, they choose a date that is not earlier than the First Aired date. This is simple logic, but it controls our data integrity.

This is done by adding code to the InValidate() trigger of both date fields, since either can be changed independently of the other.

Since we don't want to write the same code twice, we'll create a new procedure and call it from both fields. The new procedure will validate the dates.

Start by creating a new procedure. Add this new procedure after the OnInsert() trigger you added in the preceding step. The procedure will look like this:

local procedure VerifyDates()
var
FirstAiredDateCannotBeLaterErr: Label '%1 cannot be earlier
than %2';
begin
if "Last Aired" = 0D then
exit;

if "First Aired" > "Last Aired" then
Error(FirstAiredDateCannotBeLaterErr,
FieldCaption("Last Aired"),
FieldCaption("First Aired"));
end;

Like I said, it's simple logic, right?

Notice the Label variable. These are new to AL and take the place of text constants. You can read more about them at https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/label/label-data-type.
  1. Now that we have our new procedure, we need to call it from each of the date fields. We do that by adding a call to the procedure in the OnValidate() trigger of the First Aired and Last Aired fields. The trigger will look like this:
trigger onValidate()
begin
VerifyDates();
end;

Now that we have all of our logic in place, the Television Show.al file should look like this:

table 50100 "Television Show"
{
fields
{
field(1; Code; Code[20])
{
NotBlank = true;
}
field(2; Name; Text[80])
{
}
field(3; Synopsis; Text[250])
{
}
field(4; Status; Option)
{
OptionCaption = 'Active,Finished';
OptionMembers = Active,Finished;
}
field(5; "First Aired"; Date)
{
trigger onValidate()
begin
VerifyDates();
end;
}
field(6; "Last Aired"; Date)
{
trigger onValidate()
begin
VerifyDates();
end;
}
field(7; "Created By"; Code[50])
{
Editable = false;
}
}

keys
{
key(PK; Code)
{
Clustered = true;
}
}

trigger OnInsert()
begin
"Created By" := UserId();
end;

local procedure VerifyDates()
var
FirstAiredDateCannotBeLaterErr: Label '%1 cannot be
earlier than %2';
begin
if "Last Aired" = 0D then
exit;

if "First Aired" > "Last Aired" then
Error(FirstAiredDateCannotBeLaterErr,
FieldCaption("Last Aired"),
FieldCaption("First Aired"));
end;
}
  1. Before we can test our new logic, we need to add the Last Aired field to the Television Show Card.
    In Explorer, select Television Show Card.al and add the following code at the end of the group(General) section:
field("Last Aired"; "Last Aired")
{
ApplicationArea = All;
}
  1. We also need to add the Created By field to the Television Show List.

In Explorer, select Television Show List.al and add the following code at the end of the repeater(Group) section:

field("Created By"; "Created By")
{
ApplicationArea = All;
}
  1. Press F5 to build and deploy your application. Once your web browser opens and you log in to your sandbox, you will be on the Television Show List page.

Perform the following steps:

    1. Click +New to open the Television Show Card.
    2. Populate the Code field and press Tab.
    3. Select any date you want in the First Aired field.
    4. In the Last Aired field, select a date that is earlier than the date you selected in First Aired.
    5. Click on any other field on the page.

You should see the following error message:

  1. Close the Television Show Card. You will see that the Created By field has been populated for the new record that you just entered:

How it works...

Triggers are functions that happen based on when predetermined actions occur. When code is placed in these triggers, it is executed whenever the trigger is executed. It is important to note, however, that a trigger can be bypassed using business logic, so you cannot 100% guarantee that a trigger will fire.

The examples we looked at were table triggers that executed when a record was inserted into the table and any time a field was validated. There are many other triggers that are available for you to use in your AL coding. Each different AL object type has its own set of triggers.

See also