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

Community site session details

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

Use Query in Page API in AL code

(1) ShareShare
ReportReport
Posted on by 163
Hi All,

Good Morning.

I am using On-Prem Business Central.

I have created on query and then i used the query in my page API to fetch data. But i am facing the blow error.

Query Code : 
 
query 50112 GetAllItemsDetails
{
    QueryType = API;
    APIPublisher = 'a';
    APIGroup = 'b';
    EntityName = 'Item';
    EntitySetName = 'Items';
    elements
    {
        dataitem(ItemDetails; Item)
        {
            column(ItemNo; "No.")
            {
            }
            column(Description; Description)
            {
            }
        }
    }
    var
        jsonText: Text;
   
}

Page API code : 
 
page 50101 "Get Item Detail Test"
{
    DelayedInsert = true;
    PageType = API;
    APIVersion = 'v1.0';
    APIPublisher = 'q';
    APIGroup = 'q1';
    EntityName = 'PriceListLine';
    EntitySetName = 'PriceListLines';
    ODataKeyFields = SystemId;
    SourceTable = "Item";
    SourceTableTemporary = true;
    layout
    {
        area(content)
        {
            repeater(Group)
            {
                field(Id; Rec.SystemId)
                {
                    Caption = 'Id';
                }
                field("No"; Rec."No.")
                {
                    Caption = 'ItemNo';
                }
                field(Description; Rec.Description)
                {
                    Caption = 'Description';
                }

            }
        }
    }
    trigger OnOpenPage()
    var
        ItemQuery: Query "GetAllItemsDetails";
        TempItem: Record Item temporary;
    begin
        // Run the query and store results in the temporary table
        ItemQuery.Open();
        while ItemQuery.Read() do begin
            TempItem."No." := ItemQuery.ItemNo;
            TempItem.Description := ItemQuery.Description;
            TempItem.Insert();
        end;
        ItemQuery.Close();
        // Loop through the temporary records and insert into Rec
        if TempItem.FindSet() then
            repeat
                Rec.TransferFields(TempItem);  // Copy the fields from TempItem to Rec
                // Rec.:= TempItem;  // This one also used.
                Rec.Insert(false); // Insert the record into the page dataset
            until TempItem.Next() = 0;
    end;
     
    var
        TotalRows: Integer;
 
}

Error : 
 
{
    "error": {
        "code": "Internal_EntityWithSameKeyExists",
        "message": "The record in table Item already exists. Identification fields and values: No.='10010500017'  CorrelationId:  8aadcac6-c9c7-40d8-9af0-f16fcab47841."
    }
}


Does anyone have any idea why this is not working in my On-Prem BC. and If anyone have code snip please share.

Thanks for your Help.
I have the same question (0)
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    19,517 Super User 2025 Season 2 on at
    Use Query in Page API in AL code
    The error you're encountering, Internal_EntityWithSameKeyExists, is due to the fact that the record in the Item table with the No.='10010500017' already exists in your dataset when you're trying to insert the same record again. In the context of API pages, Rec.Insert(false) will attempt to insert a new record, but if the primary key already exists, it throws this error
    To fix this, check if the record exists before inserting it by using Rec.Get to prevent duplicates.
  • Gerardo Rentería García Profile Picture
    23,590 Most Valuable Professional on at
    Use Query in Page API in AL code
  • Suggested answer
    YUN ZHU Profile Picture
    93,438 Super User 2025 Season 2 on at
    Use Query in Page API in AL code
    I'm curious, what's the difference between this and using the item table directly?
    If you want to use Query, you can just set QueryType = API; The rest is no different from page.
     
    Hope this helps.
    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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

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

#1
Sumit Singh Profile Picture

Sumit Singh 2,204

#2
Rishabh Kanaskar Profile Picture

Rishabh Kanaskar 1,933

#3
YUN ZHU Profile Picture

YUN ZHU 1,885 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans