How to Search for the first occurrence of substring inside a string in Business Central - AL Extension Development

Hi BC Lovers,

In this article, I will discuss how you can check a text string containing some desired text in it, or I can say that this article will help you to write logic based on a substring search in a given text-string. 
To do this I will use STRPOS function. You can learn more about STRPOS function here on MSDN URL.

Demo: I will do it in my demo AL project, If you don't know how to create AL project for Business Central Extension Development then follow below video link:
Video: Build and Deploy an Extension in Dynamics 365 Business Central

For this demo, I have written following code in OnOpenPage() trigger of Customer List page extension my code will execute when users open Customers List page in Business Central.

Code Snippet:

// Code Snippet
pageextension 50101 CustomeList extends "Customer List"
{
    trigger OnOpenPage()
    var
        String: Text;
        SubStr: Text;
        POS: Integer;
    begin
        // My String Content
        String := 'tabrez|ajaz|farheen|fatima|tectree|kunal|metaoption|microsoft';

        // My Search String
        SubStr := 'google';

        POS := StrPos(String, SubStr);
        if POS > 0 then
            Message('Given String: ' + String + 'Outcome: \Wallah, ' + SubStr + ' is available at Position: ' + Format(POS))
        else
            Message('Given String: ' + String + '\So Sad ' + SubStr + ' is not available in the given string.');

        // My Search String
        SubStr := 'microsoft';

        POS := StrPos(String, SubStr);
        if POS > 0 then
            Message('Given String: ' + String + 'Outcome: \Wallah, ' + SubStr + ' is available at Position: ' + Format(POS))
        else
            Message('Given String: ' + String + '\So Sad ' + SubStr + ' is not available in the given string.');
    end;
}

Outcome:

 

Done!

I hope this article is helpful for you NAV/Business Central begginers.

Stay Tuned!

Add comment