Skip to content
Split multiline comments in Business Central

Split Multiline Text into Records

Daniel Gorski
Daniel Gorski June 28, 2022
3 min read

Or in other words:

Regex is your friend because the world of Regex is so powerful!

Wouldn't it be nice to copy and paste any text you like to save it directly into any table and it's splitted automatically?

It's annoying for users to split the lines on their own, right?

The full source code is available on github: https://github.com/byndit/BeyondAL/tree/main/BeyondAL/src/Editor

Animated demo of the custom AL comment editor: pasting a block of marketing text automatically splits it into separate 80-character Comment Sheet lines

There are sevaral solutions out there to handle text lines in a much more better way than the standard:

You can use a wysiwyg editor or you store your text in blob fields or you leave it as it is.

Regular Expressions are so powerful, there are so many cases where you have to normalize or transform strings, unhandy text files or in our case split strings ;-).

Here are some examples how RegEx can be used:

“Your password must have at least 6 characters, an upper case letter, a lowercase letter, a number and probably a character."

Go for it:

^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*\W).{6,}$

“Your csv or text file is semicolon separated and you need to split line by line." There you go:

(?:^|;)(?=[^"\]|(")?)"?((?(1)[^"\]*|[^;"\]*))"?(?=;|$)

"You need to valiadte the email address correctly."

Also available:

^[a-zA-Z0-9.!#$%&’\*+/=?^_\`{|}~-\]+@[a-zA-Z0-9-\]+(?:\.[a-zA-Z0-9-\]+)*$

"Check for a valid phone no." Very cool:

^\+(?:[0-9] ?){1,13}[0-9]$

Anyway. Do you want to split a long text into multiple lines with a maximum length of 80 by?

In Dynamics 365 Business Central you can store comments at:

"G/L Account" "Customer" "Vendor" "Item" "Resource" "Job" "Resource Group" "Bank Account" "Campaign" "Fixed Asset" "Insurance" "Nonstock Item" "IC Partner" "Vendor Agreement" "Customer Agreement"

"Our scenario to split a string into multiple parts with a maximum length of 80 characters delimited by a space." Awesome:

(?![^\n]{1,80}$)([^\n]{1,80})\s

We just need to loop through all matches of this RegEx and get the first group!

So let's build a very simple pure al editor to load the comments from e.g. a customer and show it!

A Page of type "CardPart" could look like this:

layout
{
    area(content)
    {
        field(EditorText; Msg)
        {
            ApplicationArea = All;
            MultiLine = true;
            ShowCaption = false;
            ExtendedDatatype = None;
        }
    }
}

This is how we load all line from the comment line table into one string called "Msg":

Found := CommentLine.Find(Which);
if Found then
    repeat
        if Msg = '' then
            Msg := CommentLine.Comment
        else
            Msg += GetLF() + CommentLine.Comment;
    until CommentLine.Next() = 0;

And this is how we split the sting in our variable "Msg" into many:

TempGroups: Record Groups temporary;
TempMatches: Record Matches temporary;
RegEx: Codeunit Regex;
RegExPattern: Text;

RegExPattern := '(?![^\n]{1,80}$)([^\n]{1,80})\s';
RegEx.Match(Msg, RegExPattern, TempMatches);

if TempMatches.IsEmpty() then
    exit;

TempMatches.find('-');
repeat
    RegEx.Groups(TempMatches, TempGroups);
    TempGroups.Get(1);
    LineNo += 10000;
    CommentLine."No." := No;
    CommentLine."Table Name" := CommentLineTableName;
    CommentLine.Comment := copystr(TempGroups.ReadValue(), 1, 80);
    CommentLine."Line No." := LineNo;
    CommentLine.Date := Today();
    CommentLine.Insert(true);
until TempMatches.Next() = 0;

And you are fine!

Get the full repo here:

https://github.com/byndit/BeyondAL/tree/main/BeyondAL/src/Editor

Related posts

▸ All posts
TXT2AL conversion guide

How-To Solve your issues after TXT2AL

February 24, 2021 · Daniel Gorski

Learn how to fix common issues after converting C/AL to AL using TXT2AL – with regex, PowerShell scripts, and hands-on troubleshooting tips.

Read More
Read More: How-To Solve your issues after TXT2AL
Upload and download files via BLOB in Business Central

Upload and Download Files in a BC Blob Field

January 29, 2025 · Daniel Gorski

Learn how to upload and download files in Business Central using a custom AL page and a BLOB field—perfect for storing documents, images, and more.

Read More
Read More: How to Upload and Download Files in a Blob Field in Business Central
SETFILTER issue with special characters in Business Central

Why SETFILTER doesn’t work sometimes

May 04, 2023 · Daniel Gorski

SETFILTER is an AL staple, but it fails silently when the filter string contains special characters like * or @. Here is why, and how STRSUBSTNO fixes it.

Read More
Read More: Why SETFILTER doesn’t work sometimes