Dynamics 365 Business Central: How to set multi-line text for a Blob Data Type field via AL

Dynamics 365 Business Central

Hi, Readers.
Today I would like to talk about how to set multiline text in a Blob Data Type field via AL.

I saw an interesting post on the Dynamics 365 Business Central Forum Community Forum yesterday. More details: Set a default value for the new field type text across the page. – Dynamics 365 Business Central Forum Community Forum

There are two questions here.

1. How to set a default value for a Blob Data Type field

This is not difficult, we have already mentioned it when discussing How to set the initial value (Automatically set default value for field), because it is the Blob Data Type field, so we can use the second method mentioned in the article.

For example, the “Work Description” field on table 36 “Sales Header”.

Source Code:

tableextension 50111 SalesHeaderExt extends "Sales Header"
{
    trigger OnInsert()
    var
        String: Text[2048];
        OutS: OutStream;
    begin
        String := 'Hello World';
        Clear("Work Description");
        "Work Description".CreateOutStream(OutS, TextEncoding::UTF8);
        OutS.WriteText(String);
    end;
}

PS: Using procedure Write() will also work. More details: OutStream Data Type

PS: Write adds a zero byte at the end of the stream. This is differs from WriteText, which does not. For more information about how zero bytes and line endings are written and read, see Write, WriteText, Read, and ReadText Method Behavior Regarding Line Endings and Zero Terminators.

2. How to set a default multi-line text for a Blob Data Type field

This is very interesting question. As you know, the Work Description field on the Sales Order supports multi-line entry, and this is printed on multiple lines in the standard report.
For example,

How to do it via AL? First, you might think of using the line break

used in Dialog data type, for example in Message(Text [, Any,…]), Confirm(Text [, Boolean] [, Any,…]), Error(Text [, Any,…]), etc.

But unfortunately, this symbol will only be transferred to the blob field as text.

This time we use CRLF (Carriage Return / Line Feed). You can find some examples in Write, WriteText, Read, and ReadText Method Behavior Regarding Line Endings and Zero Terminators.
PS: Chr(13) is the Carriage Return character and Chr(10) is the Line Feed character.

This is defined in codeunit 10 “Type Helper”, so we can call it directly.

Let me update the code a little.

Looks good😁!

Test Video:

Source Code:

tableextension 50111 MyExtension extends "Sales Header"
{
    trigger OnInsert()
    var
        TypeHelper: Codeunit "Type Helper";
        Crlf: Text[2];
        String: Text[2048];
        OutS: OutStream;
    begin
        Crlf := TypeHelper.CRLFSeparator();
        String := 'This is line 1' + Crlf + 'This is line 2' + Crlf + 'This is line 3';
        Clear("Work Description");
        "Work Description".CreateOutStream(OutS, TextEncoding::UTF8);
        OutS.WriteText(String);
        //OutS.Write(String);
    end;
}

Update a new method from Jose Joaquin Madrigal.

Using TextBuilder Data Type👏.
PS: Using TextBuilder Data Type to export data to a text file

For example,

Source Code:

tableextension 50111 MyExtension extends "Sales Header"
{
    trigger OnInsert()
    var
        OutS: OutStream;
        TxtBuilder: TextBuilder;
    begin
        TxtBuilder.AppendLine('This is line 1');
        TxtBuilder.AppendLine('This is line 2');
        TxtBuilder.AppendLine('This is line 3');
        Clear("Work Description");
        "Work Description".CreateOutStream(OutS, TextEncoding::UTF8);
        OutS.WriteText(TxtBuilder.ToText());
    end;
}

END

Hope this will help.

Thanks for reading.

ZHU

コメント

Copied title and URL