Skip to main content

Notifications

Announcements

No record found.

Business Central forum

Link one central table to two other tables

Posted on by

Dear Business Central experts,

I want to create an API query object that links one central table (Sales Header) to two related tables:

- Contact, for showing ship-to contact name, email, phone

- Sales Line, for showing a sum of amounts (Excl. VAT)

When I try it, the following error message appears: "You cannot combine two DataItems at the same level because unions are not supported".

The code for the query object is like below:

====================================

query 70101 "Shipping Postal Address"
{
    QueryType = API;
    APIPublisher = 'Entocare';
    APIGroup = 'E';
    APIVersion = 'v1.0';
    EntityName = 'shippingPostalAddress';
    EntitySetName = 'shippingPostalAddresses';

    elements
    {
        dataitem(shippingPostalAddress; "Sales Header")
        {
            column(Number; "No.")
            {
            }
            //shipping Address
            //....7 columnns omitted for brevity....

            //ship-to Contact plus details
            column(shipToContact; "Ship-to Contact")
            {
            }
            dataitem(Contact; Contact)
            {
                DataItemLink = "No." = shippingPostalAddress."Ship-to Contact";

                //...3 columns omitted for brevity...
            }

            //summary of sales lines
            dataitem(SalesLines; "Sales Line")
            {
                DataItemLink = "Document No." = shippingPostalAddress."No.";

                column(amountExclVAT; Amount)
                {
                    Method = Sum;
                }
            }
        }
    }
}

=================================================

The two dataitems (Contact and SalesLine) are indeed on the same level. They both show a red error mark with the message quoted above.

How to combine these tables in a single query?

In SQL this is easy and there is no union involved. It is just one central table joined to two related tables.

Thanks for your help!

With best regards,

Wolter Kaper

Categories:
  • Suggested answer
    Ian Frantz Profile Picture
    Ian Frantz 15 on at
    RE: Link one central table to two other tables

    If you nest your dataitems, this error will clear.

    Here is the example from the documentation: docs.microsoft.com/.../devenv-query-object

    query ID Name
    
    {
    
       elements
    
       {
    
           dataitem(DataItem1; Table1)
    
           {
    
               column(Column1; Field1)
    
               {
    
               }
    
               column(Column2; Field2)
    
               {
    
               }
    
               dataitem(DataItem2; Table2)
    
               {
    
                   // Sets a link between FieldY of Table2 and FieldX of Table1.
    
                   DataItemLink = FieldY = DataItem1.FieldX;
    
                   //The dataset contains records from Table1 and Table2 where a match is found between FieldY and FieldX.
    
                   SqlJoinType = InnerJoin;
    
                   column(Column1; Field1)
    
                   {
    
                   }
    
                   dataitem(DataItem3; Table3)
    
                   {
    
                       DataItemLink = FieldZ = DataItem2.FieldY;
    
                       SqlJoinType = InnerJoin;
    
                       column(Column1; Field1)
    
                       {
    
                       }
    
                   }
    
               }
    
           }
    
       }
    
    }

  • Ian Frantz Profile Picture
    Ian Frantz 15 on at
    RE: Link one central table to two other tables

    Thanks Josh.

    Here is an example of code that will show this error under "VendorTable" and "PurchaseLineTable".

    Can I work around this somehow?

    Thanks!

    -Ian

    =================

    elements
        {
            dataitem(PurchaseHeaderTable; "Purchase Header")
            {
                dataitem(VendorTable; "Vendor")
                {
                }
                dataitem(PurchaseLineTable; "Purchase Line")
                {                
                }
            }
        }

    ==================

    PS. Here is a github post on the same issue: github.com/.../940

  • Suggested answer
    JAngle Profile Picture
    JAngle 33,159 on at
    RE: Link one central table to two other tables

    The data item link for sales header and sales line may need a review. I would typically do this with the primary key of the table as a safe guard. Also I would try and produce this as a regular query object and then publish it as a web service. It’s exactly like your circumstance but I do something similar in this blog post: d365bcangle.wordpress.com/.../

  • Ian Frantz Profile Picture
    Ian Frantz 15 on at
    RE: Link one central table to two other tables

    Did you ever figure this out?

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans