Exercise - Events and triggers

Completed

You are a developer working for CRONUS International Ltd. You want to create a codeunit that performs extra validation when a user enters an address in the customer card. When the user enters a plus (+) sign in the address, a message needs to be shown to the user.

Tasks

  • Create a codeunit.

  • Create a local procedure called CheckForPlusSign.

  • Check if the parameter contains a plus (+) sign.

  • Create an event subscriber that subscribes to the OnBeforeValidate event on the Address field of the customer card.

Steps

  1. Select File > New File and then save this file by selecting File > Save. Name this file Validations.CodeUnit.al.

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

  3. Change the ID to 50111 and the name to Validations.

  4. Add a local procedure called CheckForPlusSign in the codeunit under the var section. You can use the tprocedure snippet to do that.

  5. Remove the var section for the global variables of the codeunit.

  6. Add a parameter with the name TextToVerify of type Text to the procedure.

  7. Remove the var section for the local variables of the procedure.

  8. Display a message box if a plus (+) sign is found in the TextToVerify parameter. You can use the Contains function to do so.

  9. Compare your code to this example. Your code should resemble this:

    codeunit 50111 Validations
    {
        trigger OnRun()
        begin
    
        end;
    
        local procedure CheckForPlusSign(TextToVerify: Text)
        begin
            if TextToVerify.Contains('+') then
                Message('A + sign has been found.');
        end;
    }
    
  10. Create an event subscriber for the OnBeforeValidate event on the Address field of the Customer Card page. Use the teventsub snippet.

    • Set the objecttype to Table.

    • Set the page to Database::Customer.

    • Set OnSomeEvent to OnAfterValidateEvent.

    • Set ElementName to Address.

    • Set SkipMissingLicense and SkipOnMissingPermission to false.

    • Rename MyProcedure to TableCustomerOnAfterValidateAddress.

    • Add a parameter: Rec: Record Customer

  11. Compare your code to this example. Your code should resemble this:

    codeunit 50111 Validations
    {
        trigger OnRun()
        begin
    
        end;
    
        [EventSubscriber(ObjectType::Table, Database::Customer, 'OnAfterValidateEvent', 'Address', false, false)]
        local procedure TableCustomerOnAfterValidateEventAddress(var Rec: Record Customer)
        begin
            CheckForPlusSign(Rec.Address);
        end;
    
        local procedure CheckForPlusSign(TextToVerify: Text)
        begin
            if TextToVerify.Contains('+') then
                Message('A + sign has been found.');
        end;
    }
    
  12. Call the CheckForPlusSign within the event subscriber. Pass Rec.Address as the parameter for the CheckForPlusSign function.

  13. Open the launch.json file in the .vscode folder. Set the startupObjectId setting to 22 and the startupObjectType to Page.

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

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

  16. Verify that the Microsoft Dynamics 365 Business Central application launches and that the Customer List page appears.

  17. Open a random customer card and enter a plus (+) sign in the address field of the customer. Press Enter. A message box should appear.