Trim your data if you want to avoid trouble!

In this video, I explore how you can improve data quality and avoid errors due to whitespace polluting your data. Check it out:

https://youtu.be/2lBp0C7_7ok

In this video, Erik demonstrates why trimming your data matters in Business Central AL development. From leading and trailing spaces in text fields to inconsistent URL formatting with trailing slashes, untrimmed data can cause subtle but frustrating bugs. Erik walks through practical examples of using AL’s built-in Trim(), TrimEnd(), and chaining techniques to keep your data clean — whether at the point of input or at the point of use.

The Problem: Invisible Spaces and Inconsistent Data

Erik starts by showing a simple Customer Card in Business Central. At first glance, everything looks fine. But when he places the cursor in the Name field, there’s a telltale sign: extra blue highlighting at the beginning and end of the text. Those are leading and trailing spaces — and they’re surprisingly common.

These invisible spaces can wreak havoc in many scenarios. Sorting might behave unexpectedly, string comparisons can fail, and when you use that data to construct other values (like URLs, file paths, or API requests), those extra spaces can cause real problems that are difficult to debug.

The Solution: Trim at the Source

The simplest approach is to trim data right where it’s entered. Erik creates a page extension for the Customer Card and adds an OnBeforeValidate trigger to the Name field:

pageextension 50100 CustomerListExt extends "Customer Card"
{
    layout
    {
        modify(Name)
        {
            trigger OnBeforeValidate()
            begin
                Rec.Name := Rec.Name.Trim();
            end;
        }
    }
}

The Trim() function returns a new string with all leading and trailing whitespace characters removed. So no matter how many spaces a user accidentally adds before or after the name, they’ll be cleaned up automatically when the field is validated.

Trimming Specific Characters with TrimEnd

Trimming isn’t just for spaces. Erik moves on to a more practical scenario: working with URLs. When users copy and paste URLs, they often come with trailing slashes. If you need to append a path to that URL, you could end up with double slashes or other formatting issues.

The TrimEnd() function lets you specify exactly which character to remove from the end of a string:

modify("Home Page")
{
    trigger OnBeforeValidate()
    begin
        Rec."Home Page" := Rec."Home Page".Trim().TrimEnd('/') + '/contact.html';
    end;
}

Notice the chaining here — Erik first calls Trim() to remove any leading or trailing whitespace, then calls TrimEnd('/') to strip any trailing forward slash. This ensures the URL is clean before appending the path.

The Trim-and-Re-Add Pattern

Erik highlights a pattern he uses constantly: trim away a character, then immediately add it back. This is incredibly useful when building URLs or file paths where you need a consistent separator between parts.

For example, if you’re building a URL from a base host and a path:

  • The base might be ibm.com or ibm.com/ — you don’t know
  • You need to add /contact.html at the end
  • Simply concatenating could give you ibm.com//contact.html

By trimming the trailing slash first and then adding it back as part of the concatenation, you always get a correctly formed URL regardless of the input format. The code becomes less prone to errors without needing conditional logic to check whether the slash exists.

The Full Source Code

Here’s the complete page extension Erik builds during the video:

namespace DefaultPublisher.Trim;

using Microsoft.Sales.Customer;

pageextension 50100 CustomerListExt extends "Customer Card"
{
    layout
    {
        modify(Name)
        {
            trigger OnBeforeValidate()
            begin
                Rec.Name := Rec.Name.Trim();
            end;
        }
        modify("Home Page")
        {
            trigger OnBeforeValidate()
            begin
                Rec."Home Page" := Rec."Home Page".Trim().TrimEnd('/') + '/contact.html';
            end;
        }
    }
    trigger OnOpenPage();
    begin
        Message('App published: Hello world');
    end;
}

When to Trim

Erik wraps up with practical guidance on when and where to apply trimming:

  1. Trim on input — Use OnBeforeValidate triggers to clean data as users enter it
  2. Trim on use — When consuming data to build URLs, file paths, or other constructed strings, trim before concatenating
  3. Trim on import — When receiving data from external sources (APIs, SharePoint, integrations), don’t trust the formatting. Erik mentions his SharePoint connector as a real-world example: he trims trailing slashes from URLs received from SharePoint before building new paths, because even though Microsoft should be consistent, why take the chance?
  4. Do both — There’s nothing wrong with trimming at input and at the point of use for extra safety

Summary

The Trim(), TrimStart(), and TrimEnd() functions in AL are small tools that can prevent big headaches. Leading and trailing spaces are nearly invisible to the eye but very visible to code. Trailing slashes in URLs are a classic source of subtle bugs. By making trimming a habit — whether on user input, imported data, or during string construction — you make your code more robust and far less prone to the kind of errors that are maddening to track down. It’s a simple practice, but one that pays off every time.