web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

Avoid duplicates in temporary table

(4) ShareShare
ReportReport
Posted on by 76
Hey,
 
I have a temporary table which is populated inside an IF condition in the OnOpenPage() trigger on my "Purchase Order Filtered" page. 
I'm getting some duplicates as in the query from which I get the data, a PO may be repeated many times, I can't avoid this, So I'm getting an error as the No. and Line No. are the keys.
I've been trying to find a way to see if the record already exists in the temporary table before doing the rec.insert(), I tried using the rValidate record and the SetRange() and IsEmpty() methods, but they don't find the already inserted record, so I'll appreciate your help figuring out how this should work.
 
 
table 60101 PurchaseOrderFilteredTemp
{
    DataClassification = ToBeClassified;
    TableType = Temporary;
    ColumnStoreIndex = "No.", "Line No.";
 
    fields
    {
        field(1; "Document Type"; Enum "Purchase Document Type")
        {
            DataClassification = ToBeClassified;
        }
        field(2; "No."; Code[20])
        {
            DataClassification = ToBeClassified;
            TableRelation = "Purchase Header";
        }
        field(3; "Line No."; Integer)
        {
            DataClassification = ToBeClassified;
        }
        field(4; Entity; Code[20])
        {
            DataClassification = ToBeClassified;
        }
        field(5; Department; Code[20])
        {
            DataClassification = ToBeClassified;
        }
        field(6; "Project Code"; Code[20])
        {
            DataClassification = ToBeClassified;
        }
        field(7; "Project Name"; Text[50])
        {
            DataClassification = ToBeClassified;
        }
        field(8; "G/L Account"; Code[20])
        {
            DataClassification = ToBeClassified;
        }
        field(9; "Approver User ID"; Code[50])
        {
            DataClassification = ToBeClassified;
        }
 
    }
 
    keys
    {
        key(PK; "No.", "Line No.")
        {
            Clustered = true;
        }
    }
}
 
 
 
page 60101 "Purchase Order Filtered"
{
    PageType = List;
    ApplicationArea = All;
    UsageCategory = Lists;
    SourceTable = PurchaseOrderFilteredTemp;
    SourceTableTemporary = true;
 
    layout
    {
        area(Content)
        {
            repeater(Group)
            {
                field("No."; rec."No.")
                {
                    ApplicationArea = All;
                    Importance = Standard;
 
                    trigger OnDrillDown()
                    var
                        PurchaseOrderListPage: Page "Purchase Order";
                        PurchaseOrderRecord: Record "Purchase Header";
                    begin
                        PurchaseOrderRecord.SetRange(PurchaseOrderRecord."No.", rec."No.");
                        PurchaseOrderListPage.SetTableView(PurchaseOrderRecord);
                        PurchaseOrderListPage.Editable(false);
                        PurchaseOrderListPage.Run();
                    end;
 
                }
                field("Line No."; rec."Line No.")
                {
                    ApplicationArea = All;
                    Importance = Standard;
                }
                field("Entity"; rec."Entity")
                {
                    ApplicationArea = All;
                    Importance = Standard;
                }
                field("Department"; rec."Department")
                {
                    ApplicationArea = All;
                    Importance = Standard;
                }
                field("Project Code"; rec."Project Code")
                {
                    ApplicationArea = All;
                    Importance = Standard;
                }
                field("Project Name"; rec."Project Name")
                {
                    ApplicationArea = All;
                    Importance = Standard;
                }
                field("G/L Account"; rec."G/L Account")
                {
                    ApplicationArea = All;
                    Importance = Standard;
                }
                field("ApproverUserID"; rec."Approver User ID")
                {
                    ApplicationArea = All;
                    Importance = Standard;
                }
            }
        }
    }
 
    trigger OnOpenPage()
    var
        qListToFilter: Query "Purch.Order and CriteriaFilter";
        glaccount: Enum "Purchase Line Type";
        rValidate: Record PurchaseOrderFilteredTemp temporary;
 
    begin
 
        glaccount := glaccount::"G/L Account";
 
        if qListToFilter.Open() then begin
            while qListToFilter.Read() do begin
                if (searchCriteria(qListToFilter.LineProjectCode, qListToFilter.Project))
                        or (searchCriteria(qListToFilter.LineTypeNo, qListToFilter.GLAccount))
 
                then begin
 
                    rValidate.Reset();
                    rValidate.SetRange("No.", qListToFilter.No);
                    rValidate.SetRange("Line No.", qListToFilter.LineNo);
 
                    if rvalidate.IsEmpty then begin
                        rec.Init();
                        rec."Document Type" := qListToFilter.DocumentType;
                        rec."No." := qListToFilter.No;
                        rec."Line No." := qListToFilter.LineNo;
                        rec.Entity := qListToFilter.LineEntityCode;
                        rec.Department := qListToFilter.LineDepartmentCode;
                        rec."Project Code" := qListToFilter.LineProjectCode;
                        rec."Project Name" := qListToFilter.LineProjectName;
                        rec."G/L Account" := qListToFilter.LineTypeNo;
                        rec."Approver User ID" := qListToFilter.ApproverUserID;
 
                        rec.Insert();
                    end;
 
                end;
            end;
        end;
        qListToFilter.Close();
    end;
I have the same question (0)
  • Suggested answer
    Sohail Ahmed Profile Picture
    11,144 Super User 2025 Season 2 on at
    **Code Review - Purchase Order Filtering Logic:**
     
    **Issues Found:**
     
    **1. Variable Naming:**
    ```al
    // Poor naming
    rValidate: Record PurchaseOrderFilteredTemp temporary;
    // Should be
    tempFilterRecord: Record PurchaseOrderFilteredTemp temporary;
    ```
     
    **2. Logic Issue - Missing Reset:**
    ```al
    // Current - may cause issues
    rValidate.Reset();
    // Should reset Rec before operations
    Rec.Reset();
    ```
     
    **3. Inefficient Duplicate Check:**
    ```al
    // Current approach
    rValidate.SetRange("No.", qListToFilter.No);
    if rvalidate.IsEmpty then begin
    // Better approach - use Get()
    if not rValidate.Get(qListToFilter.DocumentType, qListToFilter.No, qListToFilter.LineNo) then begin
    ```
     
    **4. Missing Error Handling:**
    ```al
    if qListToFilter.Open() then begin
        // Add try-catch or proper error handling
    end else
        Error('Failed to open query');
    ```
     
    **5. Improved Structure:**
    ```al
    trigger OnOpenPage()
    var
        purchQuery: Query "Purch.Order and CriteriaFilter";
        tempRecord: Record PurchaseOrderFilteredTemp temporary;
    begin
        if not purchQuery.Open() then
            exit;
            
        while purchQuery.Read() do
            ProcessQueryRecord(purchQuery, tempRecord);
            
        purchQuery.Close();
    end;
    ```
     
    **Overall:** Code works but needs cleanup for maintainability and performance.
     
    **Mark below checkbox to make this answer Verified if it helps you.**
  • Suggested answer
    Kamal Khakhkhar Profile Picture
    1,276 on at
    Hii There, 
    as per @sohail ahmed suggest you need to update the logic, also you can improve naming by using TableName_LRec, etc. Prefix, Postfix so you will get more clarity. 
    you found answer mark it answered.
     
    Thank you.
    Kamal Khakhkhar
  • Suggested answer
    YUN ZHU Profile Picture
    95,508 Super User 2025 Season 2 on at
    Hi, hope the following can give you some hints.
    Dynamics 365 Business Central: How to find duplicates via AL (Filter for duplicate values)
    Dynamics 365 Business Central: How to delete Duplicate Lines (records) – Customization
     
     
    Thanks.
    ZHU

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 3,098

#2
Jainam M. Kothari Profile Picture

Jainam M. Kothari 1,556 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 1,108 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans