Microsoft Dynamics 365 Business Central Cookbook
上QQ阅读APP看书,第一时间看更新

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: