Dynamic 365 Business central: How to Automatically Insert Line Breaks After a Specific Character Limit
Suppose, you may have a requirement to break long text into multiple lines after a certain number of characters in Business Central.
For example, when dealing with lengthy descriptions in fields like "Work Description," it's important to ensure the text wraps properly to maintain readability. In this scenario, I have implemented a solution using a tableextension to automatically insert line breaks after a specified character length (45 characters in this case). The code takes the input string, breaks it into lines, and formats it accordingly before writing it back to the "Work Description" field. This ensures that the text is displayed in a neat and user-friendly manner in the system.
Code:
tableextension 50140 MyExtension extends "Sales Header"
{
trigger OnInsert()
var
TypeHelper: Codeunit "Type Helper";
Crlf: Text[2];
InputString: Text[2048];
FormattedString: Text[2048];
MaxLineLength: Integer;
CurrentLine: Text[2048];
CharIndex: Integer;
OutS: OutStream;
begin
// Constants
Crlf := TypeHelper.CRLFSeparator();
MaxLineLength := 45; // Maximum characters per line
// Input string
InputString := 'Business Central is a comprehensive, cloud-based enterprise resource planning (ERP) solution that helps small and medium-sized businesses streamline operations, enhance customer experiences, improve financial visibility, and facilitate data-driven decisions for better organizational growth.';
FormattedString := '';
// Add line breaks
CharIndex := 1;
while CharIndex <= StrLen(InputString) do begin
CurrentLine := CopyStr(InputString, CharIndex, MaxLineLength);
FormattedString := FormattedString + CurrentLine + Crlf;
CharIndex += MaxLineLength;
end;
// Clear and write to "Work Description"
Clear("Work Description");
"Work Description".CreateOutStream(OutS, TextEncoding::UTF8);
OutS.WriteText(FormattedString);
end;
}
Hope this helps...!!
Regards,
Khushbu Rajvi
Comments
Post a Comment