Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Microsoft Dynamics 365 Business Central Cookbook

You're reading from   Microsoft Dynamics 365 Business Central Cookbook Effective recipes for developing and deploying applications with Dynamics 365 Business Central

Arrow left icon
Product type Paperback
Published in Aug 2019
Publisher Packt
ISBN-13 9781789958546
Length 380 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Michael Glue Michael Glue
Author Profile Icon Michael Glue
Michael Glue
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Let's Get the Basics out of the Way FREE CHAPTER 2. Customizing What's Already There 3. Let's Go Beyond 4. Testing and Debugging - You Just Gotta Do It 5. Old School, Meet New School 6. Making Your App Extensible 7. Business Central for All 8. DevOps - Don't Live without It 9. Time to Share Your Application! 10. Other Books You May Enjoy

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

You have been reading a chapter from
Microsoft Dynamics 365 Business Central Cookbook
Published in: Aug 2019
Publisher: Packt
ISBN-13: 9781789958546
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime