Exercise - Create a notification

Completed

Scenario

You want to request basic company information by using the Assisted Setup feature when information is missing in Microsoft Dynamics 365 Business Central.

Tasks

  • Create a codeunit.

  • Add a function to open the Company Info Wizard.

  • Create a subscriber to the OnOpenPageEvent of the Sales Order page.

  • Send a notification if the company information has no name or email value.

Steps

  1. Select File > New File and save this file by selecting File > Save. Use the file name NotificationCompanyInfo.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 50131 and the name to Notification Company Info.

  4. Create a public procedure with the name ShowCompanyWizard.

  5. Add a parameter called TheNotification of type Notification.

  6. Add code to the procedure to open the Company Info Wizard page.

    procedure ShowCompanyInfoWizard(TheNotification: Notification)
    begin
       Page.Run(Page::"Company Info Wizard");
    end;
    
  7. Create an eventsubscriber for the function OnOpenPageEvent in page 42, Sales Order.

  8. Add code that will check if the Name or Email field in the Company Information table is empty. If it's empty, you need to send a notification.

  9. Add an action to the notification that calls the ShowCompanyInfoWizard.

    [EventSubscriber(ObjectType::Page, Page::"Sales Order", 'OnOpenPageEvent', 
                     '', false, false)]
    local procedure CheckInfoOnOpenSalesOrder(var Rec: Record "Sales Header")
    var
        CompanyInformation: Record "Company Information";
        CompanyNotification: Notification;
    begin
        if CompanyInformation.Get() then
            if (CompanyInformation.Name <> '') and 
               (CompanyInformation."E-mail" <> '') then
                exit;
    
        CompanyNotification.Id := CreateGuid();
        CompanyNotification.Message := 'Company Information is missing.';
        CompanyNotification.AddAction('Open Company Information', 
                                      Codeunit::"Notification Company Info",  
                                      'ShowCompanyInfoWizard');
        CompanyNotification.Send();
    end;
    
  10. Open the launch.json file in the .vscode folder. Set the startupOjbectId setting to 42 and the startupObjectType to Page.

  11. Publish your extension to the sandbox. Select View > Command Palette... (Ctrl+Shift+P).

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

  13. Verify that the Microsoft Dynamics 365 Business Central application launches and that the Sales Order page appears.

  14. You can test your extension by removing the email value from the company information page. Search for company information and remove the email value and then reopen a sales order. A notification should appear.