Exercise - Use logical and relational expressions

Completed

You're a developer at CRONUS International Ltd. You want to test your newly acquired knowledge about logical operators. Additionally, you want to create a page, add several controls and an action to it, and write code that calculates a Boolean variable through a relational expression.

Tasks

  • Create a new page.

  • Define three global variables, two input values, and a result.

  • Create fields and use the variables as source.

  • Create an action and check if input 1 is larger than input 2.

Steps

  1. Select File > New File and immediately save this file by selecting File > Save. Give this file the name ExpressionsCard.Page.al.

  2. Create a new page in this file by using code snippets. Enter tpage and press the Tab key.

  3. Change the ID to 50111 and the name to Expressions Card.

  4. Set the PageType property to Card and set the UsageCategory property to Documents.

  5. Remove the SourceTable property and set the Caption property to Expressions Card.

  6. Create the following global variables and remove the default myInt variable.

    • Value1 Integer

    • Value2 Integer

    • Result Boolean

  7. Verify that a content area is created in the layout section. If not, create a content area.

  8. Change the name of the first group to Input. Set the Caption property of the group to Input.

  9. Create a second group with the name Output and set the Caption property of the group.

  10. Add fields for Value1 and Value2 to the Input group. Add the Result field to the Output group. You can use the tpagefield snippet.

  11. Verify that the ApplicationArea property is set to All for all fields on the page.

  12. Add a tooltip for each column in the page.

  13. Set a caption for each of the fields. Set the Editable property for the Result field to false.

  14. Verify that an action is created in the Processing area. If not, create a new action.

  15. Change the name of the action to Execute and set the following properties on that action:

    • Caption: Execute

    • Image: ExecuteBatch

    • ApplicationArea: All

    • Tooltip: Click to calculate the result.

  16. In the OnAction trigger, enter the following code:

     Result := Value1 > Value2;
    
  17. The code should now resemble this:

    	page 50111 "Expressions Card"
    	{
    	    PageType = Card;
    	    ApplicationArea = All;
    	    UsageCategory = Documents;
    	    Caption = 'Expressions Card';
    
    	    layout
    	    {
    	        area(Content)
    	        {
    	            group(Input)
    	            {
    	                Caption = 'Input';
    	                field(Value1;Value1)
    	                {
    	                    ApplicationArea = All;
    	                    ToolTip = 'Enter a value for Value1.';
    	                    Caption = 'Value1';
    	                }
    	                field(Value2;Value2)
    	                {
    	                    ApplicationArea = All;
    	                    ToolTip = 'Enter a value for Value2.';
    	                    Caption = 'Value2';
    	                }
    	            }
    	            group(Output)
    	            {
    	                Caption = 'Output';
    	                field(Result;Result)
    	                {
    	                    ApplicationArea = All;
    	                    ToolTip = 'The result of the operation.';
    	                    Caption = 'Result';
    	                    Editable = false;
    	                }
    
    	            }
    	        }
    	    }
    
    	    actions
    	    {
    	        area(Processing)
    	        {
    	            action(Execute)
    	            {
    	                ApplicationArea = All;
    	                Caption = 'Execute';
    	                ToolTip = 'Click to calculate the result.';
    	                Image = ExecuteBatch;
    
    	                trigger OnAction()
    	                begin
    	                    Result := Value1 > Value2;
    	                end;
    	            }
    	        }
    	    }
    
    	    var
    	        Value1 : Integer;
    	        Value2 : Integer;
    	        Result : Boolean;
    	}
    
  18. Open the launch.json file in the .vscode folder, and then set the startupObjectId setting to 50111 and the startupObjectType setting to Page.

  19. Publish your extension to the sandbox. Select View > Command Palette....

  20. Enter AL: Publish in the search box (or press the F5 key) and select the command from the list.

  21. Verify that the Dynamics 365 Business Central application launches and that the Expressions Card page appears. In the Value 1 field, enter 10 and in the Value 2 field, enter 2. Select the Execute button on the Actions menu, and then verify that the Result option is selected.