Skip to main content

Notifications

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 151
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.
  • Suggested answer
    YUN ZHU Profile Picture
    84,274 Super User 2025 Season 1 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
  • Gerardo Rentería García Profile Picture
    19,644 Most Valuable Professional on at
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    17,377 Super User 2025 Season 1 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.

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

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard >

Featured topics

Product updates

Dynamics 365 release plans