Skip to main content

How to Group and Consolidate General Journal Lines Using Query Object in Business Central.

Hi All,

I received a question from a fellow developer to create consolidation entries from general Journal Lines.

In this blog we will discuss how to customize business central the correct way.

How you can write code to sum up lines with multiple fields in a journal for consolidation.

Short Answer

You can consolidate General Journal Lines in Business Central by using a Query Object to group records by Posting Date, Account No., and Dimension Set ID, aggregate amounts using SUM, and then insert the consolidated results into a target journal batch using AL code.

Why This Problem Comes Up Frequently

Business Central developers often face consolidation requirements when working with General Journal Lines. A typical scenario looks like this:

  • Multiple journal lines exist in a source batch

  • Several lines share the same:

    • Posting Date

    • Account No.

    • Dimension Set ID

  • These lines must be combined into a single line

  • Amounts should be summed

  • Consolidated lines should be inserted into a target batch

  • Source lines should be deleted after consolidation

This exact requirement recently came up in a real Facebook developer discussion, where the developer initially tried solving it using record loops—even after applying strict batch filters.

That approach quickly becomes:

  • Complex

  • Error-prone

  • Hard to maintain

  • Inefficient for larger datasets


The Common (Wrong) Approach: Looping Records

Many developers attempt to solve consolidation by:

  • Looping through Gen. Journal Line

  • Comparing keys manually

  • Using temporary tables or dictionaries

  • Writing nested loops

While this can work, it introduces several problems:

  • Duplicate logic

  • Difficult debugging

  • Poor readability

  • Performance issues as data grows

Most importantly, Business Central already provides a better tool for this exact use case.


The Correct Tool: Query Object in Business Central

A Query Object is designed specifically to:

  • Read data efficiently

  • Group records by one or more fields

  • Aggregate numeric values using functions like:

    • SUM

    • COUNT

    • MIN

    • MAX

Instead of looping records and grouping manually, you let the query engine do the heavy lifting.


Defining the Consolidation Logic

In this scenario, the consolidation key is:

  • Posting Date

  • Account No.

  • Dimension Set ID

And the aggregated field is:

  • Amount (SUM)

The Query Object is configured to:

  • Use Gen. Journal Line as the data source

  • Apply grouping on the consolidation key

  • Apply SUM aggregation on the Amount field

  • Filter by the Source Batch Name

This ensures that:

  • Only relevant records are read

  • Each unique key combination appears once in the result

  • Amounts are already consolidated before AL code processes them


Reading Query Results in AL Code

It is important to clarify a common misconception:

Query Objects are read-only.

You cannot insert or modify records directly from a Query Object.

The correct pattern is:

  1. Execute the Query

  2. Loop through the Query results

  3. Create new Gen. Journal Lines in the Target Batch

  4. Insert consolidated records

  5. Delete source batch lines (if required)

This separation keeps the solution:

  • Clean

  • Predictable

  • Easy to explain and maintain


Moving Data Between Batches Safely

In the demonstrated solution:

  • Source batch remains untouched until consolidation completes successfully

  • Consolidated lines are inserted into a different batch

  • Only after successful insertion are source lines deleted

This reduces the risk of:

  • Partial consolidation

  • Data loss

  • Inconsistent journal states


Why Query Object Is Better Than Record Looping

AspectRecord LoopQuery Object
GroupingManualBuilt-in
AggregationCustom logicNative SUM
ReadabilityComplexClean
PerformanceDegrades with sizeOptimized
MaintainabilityLowHigh

For grouping and aggregation scenarios, Query Object is the correct architectural choice.


When You Should NOT Use a Query Object

To be clear, Query Objects are not a silver bullet.

Avoid them when:

  • You need row-by-row validation logic

  • You must update records directly

  • Simple indexed filtering is sufficient

However, for consolidation and reporting logic, Query Objects are exactly what you should use.


Final Thoughts

This consolidation scenario is not theoretical—it reflects real-world Business Central development challenges.

If you:

  • Work with General Journals

  • Handle financial data consolidation

  • Write AL customizations for performance and clarity

Then mastering Query Objects is non-negotiable.

The accompanying video walks through:

  • The actual Query Object

  • The AL code

  • A full working demo

  • Best practices and pitfalls


Watch the Full Video Tutorial

🎥 Video: How to Group & Consolidate Gen. Journal Lines Using Query Object in Business Central



Want More Business Central Developer Content?

  • Subscribe to the YouTube channel

  • Join the Business Central developer community

  • Explore advanced AL, performance, and API topics


Regards,
Saurav Dhyani
MVP | NAVUG-AllStar | Edhate Consulting

Comments

Popular posts from this blog

Microsoft Dynamics NAV 2016 - How to Configure Phone Client.

Hi All, In this article we will discuss how we can connect Microsoft Dynamics NAV 2016 with New Client Launched i.e. Phone Client. This Article Contain Steps for a Android Phone as I have Only Android Phone. I am doing it having all tiers on my windows 8 machine, steps remain same for multiple servers but issues might be different. What we Need (Other what we discuss in this article) -  The Service Tier should be on Public IP . Some of the Data-card does not Provide you Public IP. check it for sure.

How to Set Up NavUserPassword Authentication in Business Central.

Hi Readers, In this article we will discuss steps to Setup NAVUserPassword in Business Central on-prem. NavUserPassword is an authentication method that's handled by Business Central Server, but isn't based on Windows users or Active Directory. Each user is set up with a username and password that's configured inside Business Central only. The user is prompted for username/password credentials when they start the client. #msdyn365bc, #credentialtype

Dynamics NAV - All that you need to know about MenuSuites.

Hi Readers, This article is based on a request from a blog reader who wanted to understand about MenuSuite in Dynamics NAV. If you have started working with Business central with AL Code then it does not apply to those releases but if you are interested go ahead. Let's start with Future - In Latest and greatest version of product MenuSuite are obsolete and no longer used. So this article applies if you want to learn about C/AL MenuSuite.