Exercise - Use conditional and compound statements

Completed

You are a developer working for CRONUS International Ltd. The company has decided to start selling Microsoft Dynamics 365 Business Central training courses as its business.

You want to create a page that gives a training level and suggestion based on the difficulty of a course.

Tasks

  • Create a new page.

  • Define three global variables: one input value and two output values

  • Create fields and use the variables as source.

  • Create a local procedure that calculates which course is the best according to Difficulty level.

  • Get the course info when you enter a value in the Difficulty field.

Steps

  1. Select File > New File and immediately save this file by selecting File > Save. Enter StatementsCard.Page.al as the file name.

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

  3. Change the ID to 50112 and the name to Statements 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 Statements Card.

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

    • Level Text[30]

    • Suggestion Text[80]

    • Difficulty Integer

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

  8. Remove the actions section, if present.

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

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

  11. Add a field for Difficulty to the Input group. Add the Suggestion field and the Level field to the Output group. You can use the tpagefield snippet.

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

  13. Set a caption and tooltip for each of the fields. Set the Editable property for the Level field and the Suggestion to false.

  14. Create a local procedure called GetSuggestion under the global variables. You can use the tprocedure snippet.

  15. Add the following code between the begin and end block:

    Code Example

    Level := '';
    Suggestion := '';
    
    case Difficulty of
       1..5:
           begin
    Level := 'Beginner';
    Suggestion := 'Take e-Learning or remote training';
           end;
       6..8:
          begin
    Level := 'Intermediate';
    Suggestion := 'Attend instructor-Led';
          end;
       9..10:
          begin
    Level := 'Advanced';
    Suggestion := 'Attend instructor-Led and self study';
          end;
    end;
    
  16. Create a trigger called OnValidate within the Difficulty field. You can use the trigger snippet.

  17. Call the procedure GetSuggestion from within the OnValidate trigger.

  18. The code should look similar to this:

    page 50112 "Statements Card"
    {
    	PageType = Card;
    	ApplicationArea = All;
    	UsageCategory = Documents;
    	Caption = 'Statements Card';
    
    	layout
    	{
    		area(Content)
    		{
    			group(Input)
    			{
    				Caption = 'Input';
    				field(Difficulty; Difficulty)
    				{
    					ApplicationArea = All;
    					Caption = 'Difficulty';
    					ToolTip = 'Select the Difficulty.';
    					trigger OnValidate()
    					begin
    						GetSuggestion();
    					end;
    				}
    			}
    			group(Output)
    			{
    				Caption = 'Output';
    				field(Suggestion; Suggestion)
    				{
    					ApplicationArea = All;
    					Caption = 'Suggestion';
    					ToolTip = 'Suggestion.';
    					Editable = false;
    				}
    				field(Level; Level)
    				{
    					ApplicationArea = All;
    					Caption = 'Level';
    					ToolTip = 'Level.';
    					Editable = false;
    				}
    			}
    		}
    	}
    
    	var
    		Level: Text[30];
    		Suggestion: Text[80];
    		Difficulty: Integer;
    
    	local procedure GetSuggestion()
    	begin
    		Level := '';
    		Suggestion := '';
    
    		case Difficulty of
    			1 .. 5:
    				begin
    					Level := 'Beginner';
    					Suggestion := 'Take e-Learning or remote training';
    				end;
    			6 .. 8:
    				begin
    					Level := 'Intermediate';
    					Suggestion := 'Attend instructor-Led';
    				end;
    			9 .. 10:
    				begin
    					Level := 'Advanced';
    					Suggestion := 'Attend instructor-Led and self study';
    				end;
    		end;
    	end;
    }
    
  19. Open the launch.json file in the .vscode folder. Set the startupObjectId setting to 50112 and the startupObjectType setting to Page.

  20. Publish your extension to the sandbox. Select View > Command Palette... or use the Ctrl+Shift+P shortcut keys.

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

  22. Verify that the Microsoft Dynamics 365 Business Central application launches and that the Statements Card page appears. Enter the value 9 in the Difficulty field and then press Enter.

  23. Verify that the Level and Suggestion text boxes were changed.