Dynamics 365 Business Central: How to save large text/string in the table

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about how to save large text/string in the Business Central table.
I was asked this question the other day, they want to save a large text in the table. e.g. length of the text is 10,000 characters. However, because of system limitations, it is not possible to create such a long text field.

The maximum length for a field of type ‘Text’ is 2048

More details: Specifications for Tables

PS:
1. There is also a special data type in Business Central, BigText Data Type (Handles large text documents). But it cannot be created as a field in the table.

2. For variables of Text Data Type, there is no 2048 limit. More details: Specifications for variables

So, how to do it? This time we need to use Blob Data Type. Blob Data Type is a complex data type. Variables of this data type differ from normal numeric and string variables in that BLOBs have a variable length. The maximum size of a BLOB(binary large object) is 2 GB. Use BLOBs to store memos (text), pictures (bitmaps), or user-defined types.

You cannot view text that is stored in BLOBs from the development environment.

But if you add a field of Blob Data Type to the table and just add it to the page, it will be shown as non-editable.

You can read from and write to BLOBs by creating input and output streams, respectively. To do so, use CreateInStream method (BLOB) and CreateOutStream method (BLOB).

Here is a standard pattern for reference, Work Description field in Sales Order page.

Let’s see more details.

1. Add a Blob Data Type field to the table. “Work Description”; BLOB

2. Create a function that reads Blob data and returns a Text data type. procedure GetWorkDescription() WorkDescription: Text

3. Create a function that writes Blob data and pass in the Text data type. procedure SetWorkDescription(NewWorkDescription: Text)

4. Create a text type variable on the page and add it to the page as a field to enter and display data in the Blob. “WorkDescription: Text”

5. Use the function created in step 3 in the OnValidate trigger of the variable field to write to the blob

6. Use the function created in step 2 in the OnAfterGetRecord trigger to read from the Blob.

Okay, next, let’s customize a simple solution to save the large text in the Item table, following the standard pattern above.

Looks good👏👏.

Test video:

Give it a try!!!😁

Source Code: Github (Please note that the source code is for reference only, you can improve it according to your own needs)

tableextension 50114 ZYItemExt extends Item
{
    fields
    {
        field(50101; "Large Text"; Blob)
        {
            Caption = 'Large Text';
            DataClassification = CustomerContent;
        }
    }
}

pageextension 50114 ZYItemCardExt extends "Item Card"
{
    layout
    {
        addlast(Item)
        {
            field(LargeText; LargeText)
            {
                Caption = 'Large Text';
                ApplicationArea = All;
                MultiLine = true;
                ShowCaption = false;

                trigger OnValidate()
                begin
                    SetLargeText(LargeText);
                end;
            }
        }
    }
    
    var
        LargeText: Text;

    trigger OnAfterGetRecord()
    begin
        LargeText := GetLargeText();
    end;

    procedure SetLargeText(NewLargeText: Text)
    var
        OutStream: OutStream;
    begin
        Clear(Rec."Large Text");
        Rec."Large Text".CreateOutStream(OutStream, TEXTENCODING::UTF8);
        OutStream.WriteText(LargeText);
        Rec.Modify();
    end;

    procedure GetLargeText() NewLargeText: Text
    var
        TypeHelper: Codeunit "Type Helper";
        InStream: InStream;
    begin
        Rec.CalcFields("Large Text");
        Rec."Large Text".CreateInStream(InStream, TEXTENCODING::UTF8);
        exit(TypeHelper.TryReadAsTextWithSepAndFieldErrMsg(InStream, TypeHelper.LFSeparator(), Rec.FieldName("Large Text")));
    end;
}

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL