microsoft / AL

Home of the Dynamics 365 Business Central AL Language extension for Visual Studio Code. Used to track issues regarding the latest version of the AL compiler and developer tools available in the Visual Studio Code Marketplace or as part of the AL Developer Preview builds for Dynamics 365 Business Central.
MIT License
732 stars 243 forks source link

TenantMedia.Content as Instream, looses it's value when used as parameter #7575

Closed TaskletFactory closed 10 months ago

TaskletFactory commented 10 months ago
  1. Describe the bug

Record TenantMedia.Content can successfully be read using an InStream. BUT: An Instream will lose its instance beyond the scope of the TenantMedia record variable.

  1. To Reproduce

  2. Use any Item but add an Image to it. I used Cronus and Item 1000.

  3. Execute the "TestTenantMedia" from the WebClient.

local procedure TestTenantMedia()
    var
        Item: Record Item;
        TenantMedia: Record "Tenant Media";
        ImageStream: InStream;
        MyText: Text;
    begin

        // REQUIRED: Make SURE you have an Image on an Item, e.g. 1000  

        // Read item picture MediaID 
        Item.Get('1000');

        // Works
        GetTenantMediaAsStreamWorks(ImageStream, TenantMedia, Item.Picture.MediaId()); // This version works, but requires TenantMedia record to be parameter
        ImageStream.ReadText(MyText);
        Message('Value is OK:' + MyText);

        // Fails
        Clear(ImageStream);
        Clear(MyText);
        GetTenantMediaAsStreamFails(ImageStream, Item.Picture.MediaId());
        ImageStream.ReadText(MyText);
        Message('Value is gone:' + MyText);

    end;

    internal procedure GetTenantMediaAsStreamWorks(var _ImageStream: InStream; var _TenantMedia: Record "Tenant Media"; _MediaId: Text)
    var
        TenantMediaSet: Record "Tenant Media Set";
        MediaGuid: Guid;
        MyText: Text;
    begin
        TenantMediaSet.SetRange("Company Name", CompanyName());
        TenantMediaSet.SetRange(ID, _MediaId);
        if not TenantMediaSet.FindFirst() then
            exit;

        // Select the Image
        MediaGuid := SelectStr(1, Format(TenantMediaSet."Media ID"));

        // Get media
        _TenantMedia.SetRange("Company Name", CompanyName());
        _TenantMedia.SetRange(ID, MediaGuid);
        _TenantMedia.SetAutoCalcFields(Content);
        if not _TenantMedia.FindFirst() then
            exit;
        _TenantMedia.Content.CreateInStream(_ImageStream);

        _ImageStream.ReadText(MyText);
        //error(MyText); <---- Okay, it has value
    end;

    // This function refuses to share Instream as VAR
    // Also, the same problem if Stream is ReturnParameter
    // It wont do it
    // Something happens when TenantMedia.Content is the source of an Instream
    internal procedure GetTenantMediaAsStreamFails(var _ImageStream: InStream; _MediaId: Text)

    var
        TenantMediaSet: Record "Tenant Media Set";
        TenantMedia: Record "Tenant Media";
        MediaGuid: Guid;
        MyText: Text;
    begin
        TenantMediaSet.SetRange("Company Name", CompanyName());
        TenantMediaSet.SetRange(ID, _MediaId);
        if not TenantMediaSet.FindFirst() then
            exit;

        // Select the Image
        MediaGuid := SelectStr(1, Format(TenantMediaSet."Media ID"));

        // Get media
        TenantMedia.SetRange("Company Name", CompanyName());
        TenantMedia.SetRange(ID, MediaGuid);
        TenantMedia.SetAutoCalcFields(Content);
        if not TenantMedia.FindFirst() then
            exit;
        TenantMedia.Content.CreateInStream(_ImageStream);

        _ImageStream.ReadText(MyText);
        // error(MyText); // <---- Okay, it has value
    end;
  1. Expected behavior

An Instream based on TenantMedia.Content should not lose its value for no reason.

  1. Actual behavior An Instream based on TenantMedia.Content loose its instance and value if used as VAR parameter - even as Returnparameter.

  2. Versions: AL Language. 12.1.887908 Visual Studio Code: 1.78.2 BC W1 23.0 (Platform 23.0.13032.0 + Application 23.0.12034.13092)

List of Visual Studio Code extensions that you have installed. N/A

pri-kise commented 10 months ago

I think this has nothing todo with TenantMedia. I think this is how InStream of Blob are working currently. Check out the following issues:

https://github.com/microsoft/AL/issues/7172 https://github.com/microsoft/AL/issues/7329

TaskletFactory commented 10 months ago

Thanks @pri-kise

Anyway, it is what it is.

So I wil close this issue.

TaskletFactory commented 10 months ago

-