Exercise - Read and write files

Completed

Scenario

You are a developer for CRONUS International Ltd., and you are learning how to work with files in Business Central. You want to be able to upload pictures for an item.

Tasks

  • Create a new extension.

  • Create a codeunit.

  • Create a function to upload images in the Picture field of the Item table.

  • Create a PageExtension with an action that runs the upload function.

Steps

  1. Start Visual Studio Code.

  2. Select View > Extensions (Ctrl+Shift+X).

  3. Enter AL Language in the Search Extensions in the Marketplace search box.

  4. Select the green Install button.

  5. Create a new AL extension project. Select View > Command Palette... (Ctrl+Shift+P).

  6. Enter AL: Go! in the search box and select the command from the list.

  7. Accept the suggested path (or enter another path).

  8. Select the target platform.

  9. Select Microsoft cloud sandbox as the development endpoint.

  10. Download the application symbols. Select View > Command Palette... (Ctrl+Shift+P).

  11. Enter AL: Download symbols in the search box and select the command from the list.

  12. If requested, provide your organizational credentials (Microsoft 365 account or Microsoft Entra ID account).

  13. Open the app.json file and change the Name field to Reading and Writing Files. Change the publisher to Cronus International Ltd.

  14. Remove the HelloWorld.al file.

  15. Select File > New File and then save this file by selecting File > Save. Give it the file name ImageManagement.CodeUnit.al.

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

  17. Change the ID to 50110 and the name to Image Management.

  18. Create a new global procedure with the name ImportItemPicture.

  19. Create the following variables, in the procedure:

    > PicInStream: InStream;
    > FromFileName: Text;
    > OverrideImageQst: Label 'The existing picture will be replaced. Continue?';
    
  20. Add a parameter in the ImportItemPicture procedure, for the Item table.

  21. Add code to the ImportItemPicture function to import a picture in the Picture field of the Item table.

    procedure ImportItemPicture(Item: Record Item)
    var
        PicInStream: InStream;
        FromFileName: Text;
        OverrideImageQst: Label 'The existing picture will be replaced. Continue?';
    begin
        with Item do begin
            if Picture.Count() > 0 then
                if not Confirm(OverrideImageQst) then
                    exit;
    
            if File.UploadIntoStream('Import', '', 'All Files (*.*)|*.*',
                                    FromFileName, PicInStream) then begin
                Clear(Picture);
                Picture.ImportStream(PicInStream, FromFileName);
                Modify(true);
            end;
        end;
    end; 
    
  22. Select File > New File and then save this file by selecting File > Save. Give it the file name ItemCardPictureExtension.PageExt.al.

  23. Create a new page extension in this file by using code snippets. Enter tpageext and then press the Tab key.

  24. Change the ID to 50110 and the name to Item Card Picture Extension.

  25. Change the object to extend the Item Card page.

  26. In the actions section, add a new action with the name ImportItemPicture. Set the caption to Import Item Picture. This action should be added as the last action in the Functions group.

  27. In the OnAction trigger, create a variable called ImageManagement of Codeunit type Image Management. Call the ImportItemPicture procedure and then pass the current record (Rec) as a parameter.

    actions
    {
        // Add changes to page actions here
        addlast(Functions)
        {
            action(ImportItemPicture)
            {
                Caption = 'Import Item Picture';
                ApplicationArea = All;
                Image = Import;
                ToolTip = 'Import Item Picture';
    
                trigger OnAction()
                var
                    ImageManagement: Codeunit ImageManagement;
                begin
                    ImageManagement.ImportItemPicture(Rec);
                end;
            }
        }
    } 
    
    
  28. Open the launch.json file and change the startupObjectId to 31, which is the Item List.

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

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

  31. Open a random item from the list and select the Import Item Picture action in the Functions group, if you want to test the import functionality.