Microsoft Dynamics 365 Business Central – Sorting Algorithms: Bubble, Merge, and Quick

The other day I felt my computer files were a little disorganized, so I went through some old files to do some hard-core cleanup. I wanted to purge and archive old programs, documents, and repos. While reviewing the files, I found some old C# code (from the days of when I tried to stay sharp through CodeWars), I wrote to test different sorting algorithms. Sorting algorithms are used to organize data in a specific order, and in my code, there was the good ole Bubble Sort, the Merge Sort, and their friend, the Quick Sort.

There are several options when sorting data, and depending on the data set, some are better than others. Once I came across the sorting code, For no other reason than wanting to, I strayed from my task and converted them to AL for Microsoft Dynamics 365 Business Central.

Bubble Sort

One such sorting algorithm is called Bubble Sort. It’s a basic comparison-based method that gets its name from how smaller or larger elements “bubble” to the top of the list. With a Bubble Sort, start at the beginning of the list and compare the first element to the second. If the first element is greater than the second, swap their positions. If not, leave them as they are. Move one position to the right and compare the second and third elements. Continue this process for each pair of adjacent numbers until the end of the list is reached. It’s important to note that Bubble Sort is not very efficient, especially for large lists.

    procedure BubbleSort(List: List of [Integer])
    var
        i, j : Integer;
        ListItem: Integer;
    begin
        for i := 1 to List.Count do begin
            for j := 1 to List.Count - 1 do begin
                if List.Get(i) < List.Get(j) then begin
                    ListItem := List.Get(i);
                    List.Set(i, List.Get(j));
                    List.Set(j, ListItem);
                end;
            end;
        end;
    end;

Merge Sort

Another option, if you need to sort data, is Merge Sort. This type of comparison-based sorting algorithm is effective for most practical data sets, especially on larger lists. The strategy is to divide and conquer. The data set is split in half continuously until each set contains only one element, which is considered a sorted list. Then, the adjacent lists are merged back together while maintaining sorted order. This is done by comparing the first elements of each list and adding the smaller one to the new list, then moving on to the next element in the list from which the element was taken. The process is repeated until all the elements have been merged back into a single sorted list. Compared to Bubble Sort, Merge Sort is more efficient.

    procedure MergeSort(List: List of [Integer])
    begin
        MergeSort(List, 1, List.Count);
    end;

    local procedure Merge(List: List of [Integer]; Left: Integer; Middle: Integer; Right: Integer)
    var
        i, j, k : Integer;
        LeftList: List of [Integer];
        RightList: List of [Integer];
    begin
        for i := Left to Middle do begin
            LeftList.Add(List.Get(i));
        end;
        for i := Middle + 1 to Right do begin
            RightList.Add(List.Get(i));
        end;

        i := 1;
        j := 1;
        k := Left;

        while (i <= LeftList.Count) and (j <= RightList.Count) do begin
            if LeftList.Get(i) <= RightList.Get(j) then begin
                List.Set(k, LeftList.Get(i));
                i := i + 1;
            end else begin
                List.Set(k, RightList.Get(j));
                j := j + 1;
            end;
            k := k + 1;
        end;

        while i <= LeftList.Count do begin
            List.Set(k, LeftList.Get(i));
            i := i + 1;
            k := k + 1;
        end;

        while j <= RightList.Count do begin
            List.Set(k, RightList.Get(j));
            j := j + 1;
            k := k + 1;
        end;
    end;

    local procedure MergeSort(List: List of [Integer]; Left: Integer; Right: Integer)
    var
        Middle: Integer;
    begin
        if Left < Right then begin
            Middle := (Left + Right) div 2;
            MergeSort(List, Left, Middle);
            MergeSort(List, Middle + 1, Right);
            Merge(List, Left, Middle, Right);
        end;
    end;

Quick Sort

Quicksort is another efficient and commonly used sorting algorithm that uses a divide-and-conquer approach. It’s widely used due to its efficiency and ease of implementation. The first step in the Quicksort algorithm is to choose a pivot. The pivot can be any element from the array, but one commonly chooses the first, last, or middle elements. The role of the pivot is to assist in splitting the array. Next, rearrange the array elements so that all elements less than or equal to the pivot come before (to the left of) the pivot and all elements greater than the pivot come after (to the right of) the pivot. At this point, the pivot is in its final position in the sorted array.

    procedure QuickSort(List: List of [Integer])
    var
        i: Integer;
    begin
        QuickSort(List, 1, List.Count);
    end;

    local procedure Partition(List: List of [Integer]; Left: Integer; Right: Integer): Integer
    var
        i, j : Integer;
        Pivot: Integer;
    begin
        Pivot := List.Get(Right);
        i := Left - 1;
        for j := Left to Right - 1 do begin
            if List.Get(j) <= Pivot then begin
                i := i + 1;
                Swap(List, i, j);
            end;
        end;
        Swap(List, i + 1, Right);
        exit(i + 1);
    end;

    local procedure QuickSort(List: List of [Integer]; Left: Integer; Right: Integer)
    var
        Pivot: Integer;
    begin
        if Left < Right then begin
            Pivot := Partition(List, Left, Right);
            QuickSort(List, Left, Pivot - 1);
            QuickSort(List, Pivot + 1, Right);
        end;
    end;

    local procedure Swap(List: List of [Integer]; i: Integer; j: Integer)
    var
        ListItem: Integer;
    begin
        ListItem := List.Get(i);
        List.Set(i, List.Get(j));
        List.Set(j, ListItem);

    end;

Note: The code and information discussed in this article are for informational and demonstration purposes only. This content was created referencing Microsoft Dynamics 365 Business Central 2023 Wave 1 online.

Real things I’m asked: Microsoft Dynamics 365 Business Central How to Import a Budget?

Q. What is the process for importing G/L Budgets into Business Central?

A. A General Ledger (G/L) budget in Business Central is a financial plan that outlines projected income and expenses for a given period, typically the fiscal year. The Budget generally includes all company revenue, expense, capital, and indirect costs.
Microsoft Dynamics 365 Business Central supports multiple budgets, which can be as detailed or general as desired and created for different scenarios. For example, you could create a detailed budget for certain departments, certain kinds of revenue or expense, or a general budget for the entire business.
By creating budgets, you can plan your business’s finances based on your projections and compare actual and budgeted amounts.
Those responsible for the financial budgets may often want to work on them outside the ERP application and import them when completed. With Microsoft Dynamics Business Central, you can easily Import and Export your Budgets to/from Excel.

To Import a Budget into Business Central:

  1. Search for G/L Budgets using the Search feature within Business Central.
  2. Choose the related G/L Budgets Lists link in the pages and task section of the results.
  3. Select an existing Budget (or create a new one), then select the Edit Budget Action.
  4. Select the Export to Excel action in the menu bar and enter the desired export options.
  5. Edit the exported Excel Budget document and save the changes.
  6. Select the Import from Excel action in the menu bar and enter the desired import options.
  7. Click Ok
  8. Select the updated budget file to import.
  9. Follow the prompts within Business Central to update your Budget.

Note: The code and information discussed in this article are for informational and demonstration purposes only. This content was created referencing Microsoft Dynamics 365 Business Central 2023 Wave 1 online.

July 2023 Cumulative Updates for Dynamics 365 Business Central

The July updates for Microsoft Dynamics 365 Business Central are now available.

Before applying the updates, you should confirm that your implementation is ready for the upgrade and ensure compatibility with your modifications. Work with a Microsoft Partner to determine if you are ready and what is needed for you to apply the update.

Please note that Online customers will automatically be upgraded to version 22.3 over the coming days/weeks and should receive an email notification when upgraded.

Direct links to the cumulative updates are listed here:

Dynamics 365 Business Central On-Premises 2023 Release Wave 1 Updates – 22.3 (July 2023)

Dynamics 365 Business Central On-Premises 2022 Release Wave 2 Updates – Update 21.9 (July 2023)

Dynamics 365 Business Central On-Premises 2022 Release Wave 1 Updates – Update 20.15 (July 2023)

Dynamics 365 Business Central On-Premises 2021 Release Wave 2 Updates – Update 19.18 (April 2023)

Dynamics 365 Business Central On-Premises 2021 Release Wave 1 Updates – Update 18.18 (October 2022)

Dynamics 365 Business Central On-Premises 2020 Release Wave 2 Updates – Update 17.17 (April 2022)

Dynamics 365 Business Central On-Premises 2020 Release Wave 1 Updates – Update 16.19 (January 2022)

Dynamics 365 Business Central On-Premises 2019 Release Wave 2 Updates – Update 15.17 (April 2021)

Dynamics 365 Business Central On-Premises Spring 2019 Updates – Update 49 (July 2023)

Dynamics 365 Business Central On-Premises October’18 Updates – Update 18 (April 2020)

Microsoft Dynamics NAV 2018 – Update 60 (January 2023)

Microsoft Dynamics NAV 2017 – Update Update 61 (January 2022)

Microsoft Dynamics NAV 2016 – Update 67 (July 2021)

June 2023 Cumulative Updates for Dynamics 365 Business Central

The June updates for Microsoft Dynamics 365 Business Central are now available.

Before applying the updates, you should confirm that your implementation is ready for the upgrade and ensure compatibility with your modifications. Work with a Microsoft Partner to determine if you are ready and what is needed for you to apply the update.

Please note that Online customers will automatically be upgraded to version 22.2 over the coming days/weeks and should receive an email notification when upgraded.

Direct links to the cumulative updates are listed here:

Dynamics 365 Business Central On-Premises 2023 Release Wave 1 Updates – 22.2 (June 2023)

Dynamics 365 Business Central On-Premises 2022 Release Wave 2 Updates – Update 21.8 (June 2023)

Dynamics 365 Business Central On-Premises 2022 Release Wave 1 Updates – Update 20.14 (June 2023)

Dynamics 365 Business Central On-Premises 2021 Release Wave 2 Updates – Update 19.18 (April 2023)

Dynamics 365 Business Central On-Premises 2021 Release Wave 1 Updates – Update 18.18 (October 2022)

Dynamics 365 Business Central On-Premises 2020 Release Wave 2 Updates – Update 17.17 (April 2022)

Dynamics 365 Business Central On-Premises 2020 Release Wave 1 Updates – Update 16.19 (January 2022)

Dynamics 365 Business Central On-Premises 2019 Release Wave 2 Updates – Update 15.17 (April 2021)

Dynamics 365 Business Central On-Premises Spring 2019 Updates – Update 48 (June 2023)

Dynamics 365 Business Central On-Premises October’18 Updates – Update 18 (April 2020)

Microsoft Dynamics NAV 2018 – Update 60 (January 2023)

Microsoft Dynamics NAV 2017 – Update Update 61 (January 2022)

Microsoft Dynamics NAV 2016 – Update 67 (July 2021)

Microsoft Dynamics 365 Business Central – Saved Views and a DateFormula

With Microsoft Dynamics 365 Business Central, users can create and save personalized views of data in different lists and pages through the Saved Views feature. Saved Views allows users to filter, sort, and organize the data in a way that is relevant to their needs. By using Saved Views, users can improve their efficiency, productivity and gain insights by focusing on the data that is important to them.

To create a Saved View in Business Central:

  1. Navigate to the List Page where you would like a  Saved View.
  2. Show the Filter Pane by clicking the Show Filter Pane Icon on the action ribbon.
  3. Sort the columns to the desired View
  4. Enter the filter that you’d like saved with the View
  5. Click the Save As icon in the Filter Pane and enter a name for the Saved View
  6. Enter a name for your View
  7. The Saved View is now available on the Filter Pane

To edit a Saved View in Business Central:

  1. Navigate to the List Page where you have a  Saved View.
  2. Show the Filter Pane by clicking the Show Filter Pane Icon on the action ribbon.
  3. Select the saved View that you would like to edit
  4. Select the Edit action in the Filter Pane
  5. Adjust the filters and sort for the View
  6. Select the save icon adjacent to the Saved View’s name

Many list pages contain flowfields that use Date Filters. The filters used in Date Filters are also saved when creating a Saved View. One nicety to the filters is that instead of using a fixed date, you can enter a DateFormula. For example, If you’d like the FlowFields to calculate through last week, you can enter the formula “..CW-1W”.

You can see the calculated value when you look at the filter on the Filter Pane. However, if you make changes to it, the DateFormula will be shown and can be adjusted. Whenever the Saved View is chosen, the FlowFields will be updated with the new date calculated by the formula.

Learn more about DateFormulas here.

Note: The code and information discussed in this article are for informational and demonstration purposes only. This content was created referencing Microsoft Dynamics 365 Business Central 2023 Wave 1 online.

Microsoft Dynamics 365 Business Central – Split Delimited Value in AL

In the AL language for Microsoft Dynamics 365 Business Central, the List Data Type represents a strongly typed list of ordered objects accessible by index. Lists are unbounded, meaning their dimension (size) is not specified when declared.

A List can only be declared with simple types (Byte, Boolean, Char, Code, Date, DateFormula, DateTime, Decimal, Text, Time, etc.) and does not support holding instantiated records.

Lists are an efficient way to create and manage unbounded data structures with many practical uses.

Only a few days pass when I do not find a reason to use lists. One of my recent uses for a List was to store and process each unique value found in a delimited text value. For this, I split the delimited text into separate values and then copied them to a new list removing duplicates.

In this example, a value delimited by a comma (‘,’) or pipe (‘|’) is split into a list of unique values.

    procedure SplitValues()
    var
        UniqueValues: List of [Text];
        DelimitedText: Text;
        value: Text;
    begin
        DelimitedText := '12,34,56,24|12,56,89,56,23|12,34,22,34';
        UniqueValues := SplitUniqueValues(DelimitedText);

        foreach value in UniqueValues do
            Message(value);
    end;

    local procedure SplitUniqueValues(DelimitedText: Text): List of [Text]
    var
        UniqueValues: List of [Text];
        Values: List of [Text];
        Delimiters: Text;
        value: Text;
    begin
        Delimiters := ', |';
        Values := DelimitedText.Split(Delimiters.Split(' '));
        foreach value in Values do
            if not UniqueValues.Contains(value) then
                UniqueValues.Add(value);

        exit(UniqueValues);
    end;

Learn more about the List Data Type Here

Note: The code and information discussed in this article are for informational and demonstration purposes only. This content was created referencing Microsoft Dynamics 365 Business Central 2023 Wave 1 online.

Microsoft Dynamics 365 Business Central – Recurring Purchase Lines

Businesses often order the same products and/or services from a supplier. In the many years I have been implementing Microsoft Dynamics 365 Business Central, I have frequently been asked how to add default purchase lines (often as a default G/L Account modification) to purchase documents for a vendor. Fortunately, this feature is accessible through Business Central.

Recurring Purchase Lines are useful for businesses with regular purchase documents of the same items or services from certain suppliers. By setting up recurring purchase lines, a company can automate creating these documents with predefined purchase lines, saving time and reducing the chance of errors.

To create Recurring Purchase Lines:

1. Search for recurring purchase lines using the Search of Business Central.

2. Select the Recurring Purchase Line Administration Page

3. Select the New page action (or Manage to modify or view an existing Recurring Purchase Line)

4. On the Standard Purchase Code Card, enter a code and description to reference the Recurring Purchase Line(s)

5. In the Lines group of the card, enter the lines and desired values to include for purchase documents created for an assigned vendor.

6. Enter the Line Type – you can enter Comment, G/L Account, Item, Resource, Fixed Asset, or Item (Charge) lines.

7. Enter the appropriate No. of for the line.

8. Enter a description,

9. Enter a default quantity.

10. The Amount, Variant, and Unit of Measure fields are unavailable by default; if you’d like to enter any of these values, you must personalize the page and add them.

With the Standard Purchase Codes created, they are ready to be assigned to vendors.

  1. Search for Vendors using the search feature of Microsoft Dynamics 365 Business Central.
  2. Open the Vendor List Page and navigate to the Vendor you’d like to assign Recurring Purchase Lines.
  3. Select the Related->Purchases->Recurring Purchase Lines action from the menu.
  4. Select the desired purchase code on the Vendor’s Recurring Purchase Lines List Page and specify how you want to use standard purchase codes on purchase quotes, orders, invoices, and credit memos.
    • Manual – The user can use the Get Recurring Purchase Lines action from the functions menu on the document.
    • Automatic – the Standard Purchase Code lines are added to a new purchase document. 
    • Always Ask – the user will be alerted that recurring purchase lines exist and asked to select the lines they’d like to include (if applicable)

Note: The code and information discussed in this article are for informational and demonstration purposes only. This content was created referencing Microsoft Dynamics 365 Business Central 2023 Wave 1 online.

Microsoft Dynamics 365 Business Central – Base64 Encoding and Decoding

Base64 is a group of binary-to-text encoding schemes representing binary data in an ASCII string format. Base64 data encoding is designed to survive transport through transport layers that are not 8-bit clean, such as mail bodies. Encoding the binary data to ASCII text helps ensure that the data remains intact without loss or modification during transport.

The most common use case for base64 encoding is when binary data must be sent over text-based systems, such as email, HTTP, or complex data stored in XML or JSON.

The principle behind Base64 encoding takes 3 bytes of binary data and represents them as 4 ASCII characters. This increases the size of the data by 33%, which is a significant drawback, but sometimes this is acceptable given the benefits.

Base64 Encoding and Decoding with AL for Business Central via codeunit 4110 “Base64 Convert”. The “Base64 Convert” codeunit has many overload methods to convert text to and from its base-64 representation.

page 50100 "Base64 Encoding"
{
    ApplicationArea = All;
    Caption = 'Base64 Encoding';
    PageType = Card;

    layout
    {
        area(content)
        {
            group(General)
            {
                Caption = 'General';
                grid(Columns)
                {
                    ShowCaption = false;
                    group(text)
                    {
                        ShowCaption = false;
                        field(FromText; FromText)
                        {
                            ApplicationArea = All;
                            Caption = 'From Text';
                            Editable = true;
                            MultiLine = true;
                        }
                        field(ToText; ToText)
                        {
                            ApplicationArea = All;
                            Caption = 'To Text';
                            Editable = false;
                            MultiLine = true;
                        }
                    }
                }
            }
        }
    }
    actions
    {
        area(Promoted)
        {
            actionref(Encoderef; Encode)
            {
            }
            actionref(Dencoderef; Decode)
            {
            }
        }
        area(Processing)
        {
            action(Encode)
            {
                ApplicationArea = All;
                Caption = 'Encode';
                Image = EncryptionKeys;

                trigger OnAction()
                begin
                    ToText := Base64Convert.ToBase64(FromText);
                end;


            }
            action(Decode)
            {
                ApplicationArea = All;
                Caption = 'Decode';
                Image = Text;

                trigger OnAction()
                begin
                    ToText := Base64Convert.FromBase64(FromText);
                end;
            }
        }
    }

    var
        FromText: Text;
        ToText: Text;
        Base64Convert: Codeunit "Base64 Convert";
}

Note: The code and information discussed in this article are for informational and demonstration purposes only. This content was created referencing Microsoft Dynamics 365 Business Central 2023 Wave 1 online.

May 2023 Cumulative Updates for Dynamics 365 Business Central and Microsoft Dynamics NAV

The May updates for Microsoft Dynamics NAV and Microsoft Dynamics 365 Business Central are now available.

Before applying the updates, you should confirm that your implementation is ready for the upgrade and ensure compatibility with your modifications. Work with a Microsoft Partner to determine if you are ready and what is needed for you to apply the update.

Please note that Online customers will automatically be upgraded to version 22.1 over the coming days/weeks and should receive an email notification when upgraded.

Direct links to the cumulative updates are listed here:

Dynamics 365 Business Central On-Premises 2023 Release Wave 1 Updates – 22.1 (May 2023)

Dynamics 365 Business Central On-Premises 2022 Release Wave 2 Updates – Update 21.7 (May 2023)

Dynamics 365 Business Central On-Premises 2022 Release Wave 1 Updates – Update 20.13 (May 2023)

Dynamics 365 Business Central On-Premises 2021 Release Wave 2 Updates – Update 19.18 (April 2023)

Dynamics 365 Business Central On-Premises 2021 Release Wave 1 Updates – Update 18.18 (October 2022)

Dynamics 365 Business Central On-Premises 2020 Release Wave 2 Updates – Update 17.17 (April 2022)

Dynamics 365 Business Central On-Premises 2020 Release Wave 1 Updates – Update 16.19 (January 2022)

Dynamics 365 Business Central On-Premises 2019 Release Wave 2 Updates – Update 15.17 (April 2021)

Dynamics 365 Business Central On-Premises Spring 2019 Updates – Update 47 (May 2023)

Dynamics 365 Business Central On-Premises October’18 Updates – Update 18 (April 2020)

Microsoft Dynamics NAV 2018 – Update 60 (January 2023)

Microsoft Dynamics NAV 2017 – Update Update 61 (January 2022)

Microsoft Dynamics NAV 2016 – Update 67 (July 2021)

Real Things I’m Asked – Microsoft Dynamics 365 Business Central Copy Document

I am asked many questions about Business Central on any given day. The questions vary and cover various topics, from development to administration to integration to general application use, to name a few. I want to post them all, but that is not feasible; From time to time, I pick one to write about.

Q. In Microsoft Dynamics 365 Business Central, is there a way to copy a sales document to create a new one?

A. In Microsoft Dynamics 365 Business Central, the “Copy Document” functionality allows users to create new sales or purchase documents based on existing ones. This feature can save time and effort when you need to create similar documents, such as sales orders, quotes, purchase orders, or invoices.

To use the “Copy Document” functionality in Business Central, follow these steps:

  1. Open the list page for the type of document you want to create, such as the Sales Orders or Purchase Orders list.
  2. Click on “New” to create a new document, which will be the destination document for the “Copy Document” action.
  3. Click on the “Actions” tab in the top menu in the new document.
  4. Under the “Actions” tab, click on “Functions,” and then select “Copy Document.”
  5. On the “Copy Document” request page, enter the source “Document Type” and “No.” for the document that you would like to copy from.
  6. Toggle the “Include Header” switch to indicate if the source document header should replace the destination document’s header information.
  7. Toggle the “Recalculate Lines” switch to indicate if the copied document line values are recalculated when inserted into the destination document.
  8. Modify the new document as needed.

Note that the availability and behavior of the “Copy Document” functionality might vary depending on your Business Central version, customization, or extensions.

Note: The code and information discussed in this article are for informational and demonstration purposes only. This content was created referencing Microsoft Dynamics 365 Business Central 2023 Wave 1 online.