alguidelines.dev - Business Central Design Patterns – Querieshttps://alguidelines.dev/docs/navpatterns/patterns/queries/Recent content in Queries on alguidelines.dev - Business Central Design PatternsHugo -- gohugo.ioen-usDocs: SELECT DISTINCT with Querieshttps://alguidelines.dev/docs/navpatterns/patterns/queries/select-distinct-with-queries/Mon, 01 Jan 0001 00:00:00 +0000https://alguidelines.dev/docs/navpatterns/patterns/queries/select-distinct-with-queries/ <p><em>By Bogdana Botez, at Microsoft Development Center Copenhagen</em></p> <h3 id="abstract">Abstract</h3> <p>This pattern explains how to perform SELECT DISTINCT by using queries in Dynamics NAV.</p> <p><strong>Description</strong></p> <p>When working with tables, sometimes a developer needs to perform a SELECT DISTINCT (also known as SELECT UNIQUE) from a table. As NAV does not provide this out of the box, we present below a way to select unique records by using queries.</p> <p><strong>Problem statement</strong></p> <p>Let&rsquo;s consider the <strong>VAT Entry</strong> table as below: <a href="https://microsoft.sharepoint.com/teams/DynamicsNAV/Wiki/Nav%20Wiki%20Documents/NAV%20App%20Patterns/NAV%20App%20Patterns%20for%20Review/Table%20Select%20Distinct.docx#_msocom_1"> </a></p> <p><a href="6521.clip_5F00_image001.png"><img src="6521.clip_5F00_image001.png" alt=" "></a></p> <p>The goal is to select one line for each separate document that produced VAT Entries. In other words, we want records grouped by <strong>Type, Document Type</strong> and <strong>Document No.</strong>. However, if there are multiple lines with the same value of the triad <strong>Type, Document Type</strong> and <strong>Document No.</strong> in the <strong>VAT Entry</strong> table, we only want to see one of them.</p> <h4 id="solution">Solution</h4> <p>Create a new query object <strong>VAT Entry Distinct Document No.</strong>, with a single DataItem sourced from <strong>VAT Entry</strong> table. Add the three desired group-by fields <strong>Type, Document Type</strong> and <strong>Document No.</strong> as columns.</p> <p><a href="clip_5F00_image002.png"><img src="clip_5F00_image002.png" alt=" "></a></p> <p>To enable grouping, add one more column, with <strong>Method Type</strong> = <strong>Totals</strong>. This will automatically set the <strong>Group By</strong> checkbox to TRUE on the three precedent fields.</p> <p>Note that the <strong>Group By</strong> field is read-only and trying to set it by hand will clarify that:</p> <p><a href="clip_5F00_image003.png"><img src="clip_5F00_image003.png" alt=" "></a></p> <p>Running the query yields a single record per document. You can notice in the second line below for example, how the sales invoice number 103001 had 2 VAT Entries, but it shows up only once in the query:</p> <p><a href="clip_5F00_image004.png"><img src="clip_5F00_image004.png" alt=" "></a></p> <h3 id="example">Example</h3> <p>One thing is to be noted: there is a limitation to how much information you can take out from the records. For example, if we need to extract more information than just the one we already have in the columns, then the following apply: adding one more column of <strong>Method Type</strong> = <strong>None</strong> will indeed show more information, but it <em>might</em> affect the grouping. More details below.</p> <p><strong>The grouping is affected</strong> for example when the additional column is the <strong>VAT Entry No</strong>. In this case, this column brings up additional grouping criteria and one group for each entry number will end up being created.</p> <p><a href="clip_5F00_image005.png"><img src="clip_5F00_image005.png" alt=" "></a></p> <p>You can notice that both lines of document 103001 are not visible, which contradicts the goal of SELECT DISTINCT.</p> <p><a href="clip_5F00_image006.png"><img src="clip_5F00_image006.png" alt=" "></a></p> <p><strong>The grouping is not affected</strong> when the additional column does not influence grouping (has variation identical with one of the other existing columns). For example, adding <strong>Posting Date</strong> as a new column, does not change anything because posting date is the same for all lines of a document, so the query result is identical with the initial one:</p> <p><a href="clip_5F00_image007.png"><img src="clip_5F00_image007.png" alt=" "></a></p> <p>Below is the result of this query, where you can notice that the initial grouping is preserved and correct. Bonus, we can now read the <strong>Posting Date</strong> of the document too.</p> <p><a href="clip_5F00_image008.png"><img src="clip_5F00_image008.png" alt=" "></a></p> <p><strong>The grouping is also not affected</strong> when adding more columns of <strong>Method Type</strong> = <strong>Totals</strong>. However, this can only be done with columns of Decimal data type.</p> <h3 id="nav-usages"><strong>NAV Usages</strong></h3> <p>This pattern is used in Query 19: <strong>VAT Entries Base Amt. Sum</strong>. This query is used in Report 19: <strong>VAT- VIES Declaration Tax Auth</strong>.</p> <p><a href="Untitled-picture.png"><img src="Untitled-picture.png" alt=" "></a></p> <p>The pattern is also used in Query 1511: <strong>User IDs by Notification Type</strong>.</p> <h3 id="related-topics"><strong>Related Topics</strong></h3> <p>A variation using loops could be described, for C/AL development on NAV 6 where query objects are not available.</p>Docs: Use Queries to Detect Duplicate Recordshttps://alguidelines.dev/docs/navpatterns/patterns/queries/use-queries-to-detect-duplicate-records/Mon, 01 Jan 0001 00:00:00 +0000https://alguidelines.dev/docs/navpatterns/patterns/queries/use-queries-to-detect-duplicate-records/ <p><em>Originally by Abshishek Ghosh and Bogdan Sturzoiu at Microsoft Development Center Copenhagen</em></p> <h2 id="abstract">Abstract</h2> <p>This pattern uses queries to create an efficient way to detect duplicate entries in a table. This is, for example, useful when trying to find out which customers or contacts have the same names, so we can merge them later.</p> <h2 id="description">Description</h2> <p>Duplicate detection has several requirements in Microsoft Dynamics NAV. One method to eliminate duplication is by defining the relevant field as the primary key. However, this method is not always practical either due to the size of the field or due to business requirements that dictate how duplicates are detected but not necessarily how they are eliminated. An example of this method is to detect contacts with the same name and take action to merge them if they are.</p> <p>Before Dynamics NAV 2013, the only possibility was to iterate through the table in a loop and then create a sub-loop where another instance of the same table is filtered to check for duplicates. For example, to check for duplicate names in the Customer table, the code would look like this:</p> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-al" data-lang="al"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">PROCEDURE</span><span style="color:#f8f8f8;text-decoration:underline"> </span>HasDuplicateCustomers@<span style="color:#0000cf;font-weight:bold">26</span><span style="color:#ce5c00;font-weight:bold">() </span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">Boolean</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">VAR</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>Customer@<span style="color:#0000cf;font-weight:bold">1000</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">Record</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">18</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>Customer2@<span style="color:#0000cf;font-weight:bold">1001</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">Record</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">18</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">BEGIN</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">IF</span><span style="color:#f8f8f8;text-decoration:underline"> </span>Customer<span style="color:#ce5c00;font-weight:bold">.</span>FINDSET<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">THEN</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">REPEAT</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>Customer2<span style="color:#ce5c00;font-weight:bold">.</span>SETRANGE<span style="color:#ce5c00;font-weight:bold">(</span>Name<span style="color:#000;font-weight:bold">,</span>Customer<span style="color:#ce5c00;font-weight:bold">.</span>Name<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">IF</span><span style="color:#f8f8f8;text-decoration:underline"> </span>Customer2<span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#204a87;font-weight:bold">COUNT</span><span style="color:#f8f8f8;text-decoration:underline"> </span>\&gt;<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">THEN</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">EXIT</span><span style="color:#ce5c00;font-weight:bold">(</span>TRUE<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">UNTIL</span><span style="color:#f8f8f8;text-decoration:underline"> </span>Customer<span style="color:#ce5c00;font-weight:bold">.</span>NEXT<span style="color:#f8f8f8;text-decoration:underline"> </span>=<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">EXIT</span><span style="color:#ce5c00;font-weight:bold">(</span>FALSE<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">END</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span></code></pre></div><p>This code would involve setting filters on the <strong>Customer</strong> table as many times as there are records in the table. This is an expensive operation.</p> <p>Starting with Dynamics NAV 2013, we can use queries to create a more efficient implementation of the same logic.</p> <h2 id="usage">Usage</h2> <p>The solution involves that you create a query to return duplicates, and then invoke it from a method that would test the value of the query to identify if duplicates were found.</p> <p><strong>Step 1</strong> &ndash; <strong>Creating the Query</strong></p> <ul> <li>The query must be created with the table we want to search in as the dataitem.</li> <li>The field we want to search for must be created as a grouped field.</li> <li>Create a totaling field on the count, and add a filter for Count &gt; 1. This ensures that only records with more than one instance of the field that we selected in the previous step are included in the query result.</li> </ul> <p>Continuing with our Customer Name example, here is how the query would look:</p> <p><a href="clip_5F00_image002.jpg"><img src="clip_5F00_image002.jpg" alt=" "></a></p> <h4 id="heading"></h4> <pre><code>ELEMENTS { { 1 ; ;DataItem; ; DataItemTable=Table18 } { 2 ;1 ;Column ; ; DataSource=Name } { 3 ;1 ;Column ; ; ColumnFilter=Count_=FILTER(\&gt;1); MethodType=Totals; Method=Count } } </code></pre> <p><strong>Step 2 &ndash; Invoking the Query to Check for Duplicates</strong></p> <p>Now that the query is created, all we need to do is to invoke the query and check if any records are returned, which would mean that there are duplicates.</p> <p>Here is an alternate implementation of the <strong>HasDuplicateCustomers</strong> method using the query that we created:</p> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-al" data-lang="al"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">PROCEDURE</span><span style="color:#f8f8f8;text-decoration:underline"> </span>HasDuplicateCustomersWithQuery@<span style="color:#0000cf;font-weight:bold">27</span><span style="color:#ce5c00;font-weight:bold">() </span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">Boolean</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">VAR</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>CustomerDuplicate@<span style="color:#0000cf;font-weight:bold">1000</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">Query</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">70000</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">BEGIN</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>CustomerDuplicate<span style="color:#ce5c00;font-weight:bold">.</span>OPEN<span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">EXIT</span><span style="color:#ce5c00;font-weight:bold">(</span>CustomerDuplicate<span style="color:#ce5c00;font-weight:bold">.</span>READ<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">END</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span></code></pre></div><p><strong>Examples</strong></p> <ul> <li><strong>Acc. Sched. Chart Management</strong> codeunit (762),<br> methods CheckDuplicateAccScheduleLineDescription and CheckDuplicateColumnLayoutColumnHeader</li> <li><strong>Analysis Report Chart Mgt.</strong> codeunit (770),<br> methods CheckDuplicateAnalysisLineDescription and CheckDuplicateAnalysisColumnHeader</li> </ul> <h2 id="consequences"><strong>Consequences</strong></h2> <p>A new query object is needed for every type of duplicate check. This could easily explode and create a maintenance problem.</p>Docs: Use Queries to Replace Nested Loopshttps://alguidelines.dev/docs/navpatterns/patterns/queries/use-queries-to-replace-nested-loops/Mon, 01 Jan 0001 00:00:00 +0000https://alguidelines.dev/docs/navpatterns/patterns/queries/use-queries-to-replace-nested-loops/ <p><em>Originally by Bogdan Sturzoiu, Microsoft Development Center Copenhagen</em></p> <h2 id="abstract">Abstract</h2> <p>This pattern shows how the new query object type introduced in NAV 2013 allows you to replace costly loops when inspecting data from two or more tables.</p> <h3 id="description">Description</h3> <p>One of the core operations in a relational database is joining two or more tables. For example, you might need to extract all sales lines in the database together with information regarding the related sales header. This requires joining the Sales Header and Sales Line tables using Sales Header No. as the connecting field.;</p> <p>The join operation has traditionally been done in C/AL by record looping. When NAV 2013 introduced the query object, it allowed us to produce a data set that is the result of a join operation between two or more tables. This simplifies the problem of finding related records in two tables linked through a foreign key.</p> <h4 id="pattern-elements">Pattern Elements</h4> <ol> <li>Two or more tables that contain records linked through a foreign key: Table 1, Table 2, Table n.</li> <li>A query object Query X, that joins Table 1, Table 2, etc. based on the connecting key.</li> <li>A processing codeunit that loops through the query records (or any other code-bearing object).</li> </ol> <h4 id="pattern-steps">Pattern Steps</h4> <ol> <li>Run the query on the connected tables.</li> <li>Loop through the records returned by the query.</li> <li>Process the records.</li> </ol> <p><a href="5040.clip_5F00_image002.png"><img src="5040.clip_5F00_image002.png" alt=" "></a></p> <p><em>Figure 1. The pattern elements</em></p> <h3 id="usage">Usage</h3> <h4 id="sample-problem">Sample Problem</h4> <p>The Bank Acc. Reconciliation Line table (274) and the Bank Account Ledger Entry table (271) are connected through the Bank Account No. field. Identify the matching pairs of records based on having the same remaining amount and transaction date.</p> <h4 id="solution-using-nested-loops">Solution Using Nested Loops</h4> <p>The classic C/AL approach is to:</p> <ol> <li>Set the necessary filters on the left table, i.e. table 274.</li> <li>Loop through the filtered records.</li> <li>For each record in the filter, find the related records in the right table (table 271) and set the required filters on it.</li> <li>For each pair of records from the left and right table, decide if they are a solution and if so, apply them to each other.</li> </ol> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-al" data-lang="al"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">PROCEDURE</span><span style="color:#f8f8f8;text-decoration:underline"> </span>MatchSingle@<span style="color:#0000cf;font-weight:bold">5</span><span style="color:#ce5c00;font-weight:bold">(</span>BankAccReconciliation@<span style="color:#0000cf;font-weight:bold">1003</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">Record</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">273</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">VAR</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankAccRecLine@<span style="color:#0000cf;font-weight:bold">1005</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">Record</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">274</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankAccLedgerEntry@<span style="color:#0000cf;font-weight:bold">1006</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">Record</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">271</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankAccEntrySetReconNo@<span style="color:#0000cf;font-weight:bold">1007</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">Codeunit</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">375</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">BEGIN</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankAccRecLine<span style="color:#ce5c00;font-weight:bold">.</span>SETRANGE<span style="color:#ce5c00;font-weight:bold">(</span>&#34;Bank Account No.&#34;<span style="color:#000;font-weight:bold">,</span>BankAccReconciliation<span style="color:#ce5c00;font-weight:bold">.</span>&#34;Bank Account No.&#34;<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankAccRecLine<span style="color:#ce5c00;font-weight:bold">.</span>SETRANGE<span style="color:#ce5c00;font-weight:bold">(</span>&#34;Statement No.&#34;<span style="color:#000;font-weight:bold">,</span>BankAccReconciliation<span style="color:#ce5c00;font-weight:bold">.</span>&#34;Statement No.&#34;<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankAccRecLine<span style="color:#ce5c00;font-weight:bold">.</span>SETFILTER<span style="color:#ce5c00;font-weight:bold">(</span>Difference<span style="color:#000;font-weight:bold">,</span><span style="color:#4e9a06">&#39;&lt;&gt;%1&#39;</span><span style="color:#000;font-weight:bold">,</span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankAccRecLine<span style="color:#ce5c00;font-weight:bold">.</span>SETRANGE<span style="color:#ce5c00;font-weight:bold">(</span><span style="color:#204a87;font-weight:bold">Type</span><span style="color:#000;font-weight:bold">,</span>BankAccRecLine<span style="color:#ce5c00;font-weight:bold">.</span><span style="color:#204a87;font-weight:bold">Type</span><span style="color:#000;font-weight:bold">::</span>&#34;Bank Account Ledger Entry&#34;<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">IF</span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankAccRecLine<span style="color:#ce5c00;font-weight:bold">.</span>FINDSET<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">THEN</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">REPEAT</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankAccLedgerEntry<span style="color:#ce5c00;font-weight:bold">.</span>SETRANGE<span style="color:#ce5c00;font-weight:bold">(</span>&#34;Bank Account No.&#34;<span style="color:#000;font-weight:bold">,</span>BankAccRecLine<span style="color:#ce5c00;font-weight:bold">.</span>&#34;Bank Account No.&#34;<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankAccLedgerEntry<span style="color:#ce5c00;font-weight:bold">.</span>SETRANGE<span style="color:#ce5c00;font-weight:bold">(</span>Open<span style="color:#000;font-weight:bold">,</span>TRUE<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankAccLedgerEntry<span style="color:#ce5c00;font-weight:bold">.</span>SETRANGE<span style="color:#ce5c00;font-weight:bold">(</span>&#34;Statement Status&#34;<span style="color:#000;font-weight:bold">,</span>BankAccLedgerEntry<span style="color:#ce5c00;font-weight:bold">.</span>&#34;Statement Status&#34;<span style="color:#000;font-weight:bold">::</span>Open<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankAccLedgerEntry<span style="color:#ce5c00;font-weight:bold">.</span>SETFILTER<span style="color:#ce5c00;font-weight:bold">(</span>&#34;Remaining Amount&#34;<span style="color:#000;font-weight:bold">,</span><span style="color:#4e9a06">&#39;&lt;&gt;%1&#39;</span><span style="color:#000;font-weight:bold">,</span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">IF</span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankAccLedgerEntry<span style="color:#ce5c00;font-weight:bold">.</span>FINDSET<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">THEN</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">REPEAT</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">IF</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#ce5c00;font-weight:bold">(</span>BankAccRecLine<span style="color:#ce5c00;font-weight:bold">.</span>Difference<span style="color:#f8f8f8;text-decoration:underline"> </span>=<span style="color:#f8f8f8;text-decoration:underline"> </span>BankAccLedgerEntry<span style="color:#ce5c00;font-weight:bold">.</span>&#34;Remaining Amount&#34;<span style="color:#ce5c00;font-weight:bold">) </span><span style="color:#204a87;font-weight:bold">AND</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#ce5c00;font-weight:bold">(</span>BankAccRecLine<span style="color:#ce5c00;font-weight:bold">.</span>&#34;Transaction Date&#34;<span style="color:#f8f8f8;text-decoration:underline"> </span>=<span style="color:#f8f8f8;text-decoration:underline"> </span>BankAccLedgerEntry<span style="color:#ce5c00;font-weight:bold">.</span>&#34;Posting Date&#34;<span style="color:#ce5c00;font-weight:bold">) </span><span style="color:#204a87;font-weight:bold">THEN</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankAccEntrySetReconNo<span style="color:#ce5c00;font-weight:bold">.</span>ApplyEntries<span style="color:#ce5c00;font-weight:bold">( </span></span></span><span style="display:flex;"><span><span style="color:#ce5c00;font-weight:bold"> </span>BankAccRecLine<span style="color:#000;font-weight:bold">,</span>BankAccLedgerEntry<span style="color:#000;font-weight:bold">,</span><span style="color:#f8f8f8;text-decoration:underline"> </span>Relation<span style="color:#000;font-weight:bold">::</span>&#34;One-to-One&#34;<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">UNTIL</span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankAccLedgerEntry<span style="color:#ce5c00;font-weight:bold">.</span>NEXT<span style="color:#f8f8f8;text-decoration:underline"> </span>=<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">UNTIL</span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankAccRecLine<span style="color:#ce5c00;font-weight:bold">.</span>NEXT<span style="color:#f8f8f8;text-decoration:underline"> </span>=<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">0</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">END</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span></code></pre></div><p><strong>Solution Using Query</strong></p> <p>The new query-based approach involves:</p> <ol> <li>Define a query that returns the full filtered join of tables 271 and 274.</li> <li>Loop through the records returned by the query.</li> <li>For each query record, decide if it represents a solution and then connect the two table records that formed it through an application.</li> </ol> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-al" data-lang="al"><span style="display:flex;"><span><span style="color:#204a87;font-weight:bold">PROCEDURE</span><span style="color:#f8f8f8;text-decoration:underline"> </span>MatchSingle@<span style="color:#0000cf;font-weight:bold">5</span><span style="color:#ce5c00;font-weight:bold">(</span>BankAccReconciliation@<span style="color:#0000cf;font-weight:bold">1003</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">Record</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">273</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">VAR</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankRecMatchCandidates@<span style="color:#0000cf;font-weight:bold">1001</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">Query</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">1252</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankAccEntrySetReconNo@<span style="color:#0000cf;font-weight:bold">1007</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">:</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">Codeunit</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">375</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">BEGIN</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankRecMatchCandidates<span style="color:#ce5c00;font-weight:bold">.</span>SETRANGE<span style="color:#ce5c00;font-weight:bold">(</span>Rec_Line_Bank_Account_No<span style="color:#000;font-weight:bold">,</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankAccReconciliation<span style="color:#ce5c00;font-weight:bold">.</span>&#34;Bank Account No.&#34;<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankRecMatchCandidates<span style="color:#ce5c00;font-weight:bold">.</span>SETRANGE<span style="color:#ce5c00;font-weight:bold">(</span>Rec_Line_Statement_No<span style="color:#000;font-weight:bold">,</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankAccReconciliation<span style="color:#ce5c00;font-weight:bold">.</span>&#34;Statement No.&#34;<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">IF</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">NOT</span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankRecMatchCandidates<span style="color:#ce5c00;font-weight:bold">.</span>OPEN<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">THEN</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">EXIT</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">WHILE</span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankRecMatchCandidates<span style="color:#ce5c00;font-weight:bold">.</span>READ<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">DO</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">BEGIN</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankAccLedgerEntry<span style="color:#ce5c00;font-weight:bold">.</span>GET<span style="color:#ce5c00;font-weight:bold">(</span>BankRecMatchCandidates<span style="color:#ce5c00;font-weight:bold">.</span>Entry_No<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankAccRecLine<span style="color:#ce5c00;font-weight:bold">.</span>GET<span style="color:#ce5c00;font-weight:bold">(</span>BankAccRecLine<span style="color:#ce5c00;font-weight:bold">.</span>&#34;Statement Type&#34;<span style="color:#000;font-weight:bold">::</span>&#34;Bank Reconciliation&#34;<span style="color:#000;font-weight:bold">,</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankRecMatchCandidates<span style="color:#ce5c00;font-weight:bold">.</span>Rec_Line_Bank_Account_No<span style="color:#000;font-weight:bold">,</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankRecMatchCandidates<span style="color:#ce5c00;font-weight:bold">.</span>Rec_Line_Statement_No<span style="color:#000;font-weight:bold">,</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankRecMatchCandidates<span style="color:#ce5c00;font-weight:bold">.</span>Rec_Line_Statement_Line_No<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>BankAccEntrySetReconNo<span style="color:#ce5c00;font-weight:bold">.</span>ApplyEntries<span style="color:#ce5c00;font-weight:bold">(</span>BankAccRecLine<span style="color:#000;font-weight:bold">,</span>BankAccLedgerEntry<span style="color:#000;font-weight:bold">,</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>Relation<span style="color:#000;font-weight:bold">::</span>&#34;One-to-One&#34;<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">END</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"></span><span style="color:#204a87;font-weight:bold">END</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span></code></pre></div><p>where the query 1252 is defined as:</p> <div class="highlight"><pre tabindex="0" style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-al" data-lang="al"><span style="display:flex;"><span>OBJECT<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">Query</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">1252</span><span style="color:#f8f8f8;text-decoration:underline"> </span>Bank<span style="color:#f8f8f8;text-decoration:underline"> </span>Rec<span style="color:#ce5c00;font-weight:bold">. </span>Match<span style="color:#f8f8f8;text-decoration:underline"> </span>Candidates<span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"></span>{<span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>OBJECT<span style="color:#ce5c00;font-weight:bold">-</span>PROPERTIES<span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>{<span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">Date</span>=<span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">Time</span>=<span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">Version</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">List</span>=<span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>}<span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>PROPERTIES<span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>{<span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>}<span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">ELEMENTS</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>{<span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>{<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#204a87;font-weight:bold">DataItem</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>DataItemTable=Table274<span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>DataItemTableFilter=Difference=<span style="color:#204a87;font-weight:bold">FILTER</span><span style="color:#ce5c00;font-weight:bold">(</span>&lt;&gt;<span style="color:#0000cf;font-weight:bold">0</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#000;font-weight:bold">,</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">Type</span>=<span style="color:#204a87;font-weight:bold">FILTER</span><span style="color:#ce5c00;font-weight:bold">(</span>=Bank<span style="color:#f8f8f8;text-decoration:underline"> </span>Account<span style="color:#f8f8f8;text-decoration:underline"> </span>Ledger<span style="color:#f8f8f8;text-decoration:underline"> </span>Entry<span style="color:#ce5c00;font-weight:bold">) </span>}<span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>{<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">2</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#204a87;font-weight:bold">Column</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span>Rec_Line_Bank_Account_No<span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>DataSource=Bank<span style="color:#f8f8f8;text-decoration:underline"> </span>Account<span style="color:#f8f8f8;text-decoration:underline"> </span>No<span style="color:#ce5c00;font-weight:bold">. </span>}<span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>{<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">3</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#204a87;font-weight:bold">Column</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span>Rec_Line_Statement_No<span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>DataSource=Statement<span style="color:#f8f8f8;text-decoration:underline"> </span>No<span style="color:#ce5c00;font-weight:bold">. </span>}<span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>{<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">4</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#204a87;font-weight:bold">Column</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span>Rec_Line_Statement_Line_No<span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>DataSource=Statement<span style="color:#f8f8f8;text-decoration:underline"> </span>Line<span style="color:#f8f8f8;text-decoration:underline"> </span>No<span style="color:#ce5c00;font-weight:bold">. </span>}<span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>{<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">5</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#204a87;font-weight:bold">Column</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span>Rec_Line_Transaction_Date<span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>DataSource=Transaction<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">Date</span><span style="color:#f8f8f8;text-decoration:underline"> </span>}<span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>{<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">6</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#204a87;font-weight:bold">Column</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span>Rec_Line_Difference<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>DataSource=Difference<span style="color:#f8f8f8;text-decoration:underline"> </span>}<span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>{<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">7</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#0000cf;font-weight:bold">1</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#204a87;font-weight:bold">DataItem</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>DataItemTable=Table271<span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>DataItemTableFilter=Remaining<span style="color:#f8f8f8;text-decoration:underline"> </span>Amount=<span style="color:#204a87;font-weight:bold">FILTER</span><span style="color:#ce5c00;font-weight:bold">(</span>&lt;&gt;<span style="color:#0000cf;font-weight:bold">0</span><span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#000;font-weight:bold">,</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>Open=<span style="color:#204a87;font-weight:bold">CONST</span><span style="color:#ce5c00;font-weight:bold">(</span>Yes<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#000;font-weight:bold">,</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>Statement<span style="color:#f8f8f8;text-decoration:underline"> </span>Status=<span style="color:#204a87;font-weight:bold">FILTER</span><span style="color:#ce5c00;font-weight:bold">(</span>Open<span style="color:#ce5c00;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>DataItemLink=Bank<span style="color:#f8f8f8;text-decoration:underline"> </span>Account<span style="color:#f8f8f8;text-decoration:underline"> </span>No<span style="color:#ce5c00;font-weight:bold">.</span>=Bank_Acc_Reconciliation_Line<span style="color:#ce5c00;font-weight:bold">.</span>&#34;Bank Account o.&#34;<span style="color:#000;font-weight:bold">,</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>Remaining<span style="color:#f8f8f8;text-decoration:underline"> </span>Amount=Bank_Acc_Reconciliation_Line<span style="color:#ce5c00;font-weight:bold">.</span>Difference<span style="color:#000;font-weight:bold">,</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>Posting<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">Date</span>=Bank_Acc_Reconciliation_Line<span style="color:#ce5c00;font-weight:bold">.</span>&#34;Transaction Date&#34;<span style="color:#f8f8f8;text-decoration:underline"> </span>}<span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>{<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">8</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#0000cf;font-weight:bold">2</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#204a87;font-weight:bold">Column</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>DataSource=Entry<span style="color:#f8f8f8;text-decoration:underline"> </span>No<span style="color:#ce5c00;font-weight:bold">. </span>}<span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>{<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">9</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#0000cf;font-weight:bold">2</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#204a87;font-weight:bold">Column</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span>Bank_Account_No<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>DataSource=Bank<span style="color:#f8f8f8;text-decoration:underline"> </span>Account<span style="color:#f8f8f8;text-decoration:underline"> </span>No<span style="color:#ce5c00;font-weight:bold">. </span>}<span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>{<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">10</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#0000cf;font-weight:bold">2</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#204a87;font-weight:bold">Column</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>DataSource=Posting<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">Date</span><span style="color:#f8f8f8;text-decoration:underline"> </span>}<span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>{<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">11</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#0000cf;font-weight:bold">2</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#204a87;font-weight:bold">Column</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>DataSource=Remaining<span style="color:#f8f8f8;text-decoration:underline"> </span>Amount<span style="color:#f8f8f8;text-decoration:underline"> </span>}<span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>{<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">12</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#0000cf;font-weight:bold">2</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#204a87;font-weight:bold">Column</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span>Bank_Ledger_Entry_Open<span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>DataSource=Open<span style="color:#f8f8f8;text-decoration:underline"> </span>}<span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>{<span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#0000cf;font-weight:bold">13</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#0000cf;font-weight:bold">2</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#204a87;font-weight:bold">Column</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#000;font-weight:bold">;</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>DataSource=Statement<span style="color:#f8f8f8;text-decoration:underline"> </span>Status<span style="color:#f8f8f8;text-decoration:underline"> </span>}<span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>}<span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">CODE</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span>{<span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">BEGIN</span><span style="color:#f8f8f8;text-decoration:underline"> </span></span></span><span style="display:flex;"><span><span style="color:#f8f8f8;text-decoration:underline"> </span><span style="color:#204a87;font-weight:bold">END</span><span style="color:#ce5c00;font-weight:bold">. </span></span></span><span style="display:flex;"><span><span style="color:#ce5c00;font-weight:bold"> </span>}<span style="color:#f8f8f8;text-decoration:underline"> </span></span></span></code></pre></div><p>When comparing the two implementations, we notice the following advantages of using a query instead of two loops:Comparison</p> <ol> <li>A query produces the Cartesian product of tables 1 and 2 faster than by looping through both of them. The advantage grows as there are more tables linked.</li> </ol> <p>The query object leverages the power of SQL Server (as it basically executes a SQL query directly against the database server), and as such it only requires one trip to the database to fetch the data, instead of getting the related records on the right side of the join one by one.</p> <ol> <li>A query is scalable in the sense that it allows reusing its definition by applying filters when looping through its dataset. So a generic query can be reused for a variety of purposes just by adapting the filters to the programming need. Duplicating traditional record loops in different functions, on the other side, requires extensive code copy-pasting, which can introduce code defects.</li> <li>Using a query only requires one loop, whereas joining two or more tables requires multiple code loops that quickly become difficult to read and follow.</li> <li>Queries are easy to create and maintain and generally provide a cleaner design.</li> </ol> <h3 id="nav-specific-examples">NAV Specific Examples</h3> <p>In Microsoft Dynamics NAV 2013 R2, we can see the query object used in the bank account reconciliation matching algorithm. The object is query Bank Rec. Match Candidates query (1252), and it is called by the matching engine in the Match Bank Rec. Lines codeunit (1252).</p> <h3 id="ideas-for-improvement">Ideas for Improvement</h3> <p>The query object type could be improved to allow the passing of parameters at runtime, or, in general, being built dynamically at runtime. This will remove the need for multiple static definitions of the same base query used in slightly different contexts.</p>