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

Notifications

Announcements

Community site session details

Community site session details

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

Query API to fetch single record from child dataitem in al code.

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

Good Morning All,

I am using On-Prem Business Central.

I have created one query API which used to fetch Item detail along with the Bar code of item, but if any item contains more than one bar code then it is returning duplicate row.
If there any way/condition where i restrict the child data item to fetch single record only. I
I can only use query API here, because later have to add filter on this bar code also like $filter=barcode eq 'XX'.

I just want to return if any any item have barcode then return 1 barcode, else blank string. So which join i need to use here, please suggest me and please have a look on the below code snip.

Does anyone have any idea about it, please share if you have any code snip.

Code :
query 50118 TestQuery
{
    QueryType = API;
    APIPublisher = 'QueryV2';
    APIGroup = 'QueryGroup';
    EntityName = 'Item';
    EntitySetName = 'Items';
    elements
    {
        dataitem(ItemDetails; Item)
        {
            column(SystemId; SystemId)
            {
            }
            column(ItemNo; "No.")
            {
            }
            column(Name; Description)
            {
            }
            column(Base_Unit_Price; "Unit Price")
            {
            }
            
            column(Country_of_Origin; "Country/Region of Origin Code")
            {
            }
            dataitem(Item_Reference; "Item Reference")
            {
                DataItemLink = "Item No." = ItemDetails."No.", "Unit of Measure" = ItemDetails."Base Unit of Measure";
                DataItemTableFilter = "Reference Type No." = filter('BARCODE');
                SqlJoinType = LeftOuterJoin;
                column(BarCodeNo; "Reference No.")
                {
                }
            }
        }
    }
}

Thanks for your Help.
I have the same question (0)
  • Suggested answer
    Vahid Ghafarpour Profile Picture
    12,204 Super User 2025 Season 2 on at
    I'd use the MIN or MAX function to select either the first or the last barcode for each item.
     
                    column(BarCodeNo; MIN("Reference No."))
     
  • Suggested answer
    Khushbu Rajvi. Profile Picture
    21,048 Super User 2025 Season 2 on at
     AL doesn’t directly support limiting the child data item to one ecord per parent in a query. However, here’s a potential workaround using a calculated column. This allows you to conditionally fetch only one barcode and avoid duplicates
     
      column(BarCodeNo; MAX("Reference No.")) {}
     
    The MAX("Reference No.") will select only one record, effectively returning a single barcode per item. You may use other aggregation functions as per your needs.
     
  • Suggested answer
    YUN ZHU Profile Picture
    96,039 Super User 2025 Season 2 on at
    No logic or control can be added to the query.
    I'm sorry I haven't tested it in detail, but based on your requirements, it may be possible to use "Page + temporary table".
     
    Hope this can give you some hints.
    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

News and Announcements

Season of Giving Solutions is Here!

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 1,585

#2
Khushbu Rajvi. Profile Picture

Khushbu Rajvi. 780 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 712 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans