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

AL scripting. API Page for POST Return Sales order lines

(0) ShareShare
ReportReport
Posted on by

We want to build AL API page to POST Return Sales order lines.
We succesfully POST Return Sales Order using API Page for Source table = "Sales header" with document type = "Return order"
But then we try to do POST call with correct Document No(which exists in MBC after first POST call to Sales header API page) and Document Type = "Return order" and receive following error:
{"code":"Internal_InvalidTableRelation","message":"The field Document No. of table Sales Line contains a value (1029) that cannot be found in the related table (Sales Header). CorrelationId: 11a32c7c-6f5e-44da-9f2f-59deeaab9cd3."}}

Our AL Page looks foloowing:

page 50129 ApiPageSalesHeadersLines
{
    PageType = API;
    Caption = 'ApiPageSalesHeadersLines';
    APIPublisher = 'Tests';
    APIGroup = 'TestApp';
    APIVersion = 'v1.0';
    EntityName = 'SalesHeaderLine';
    EntitySetName = 'SalesHeaderLines';
    SourceTable = "Sales Line";
    DelayedInsert = true;
    InsertAllowed = true;
    ModifyAllowed = true;

    layout
    {
        area(Content)
        {
            repeater(GroupName)
            {
                field(number; "Document No.")
                {
                    Caption = 'documentNumber';
                }

                field(documentType; "Document Type")
                {
                    Caption = 'documentType';
                }

                field("itemCode"; "No.")
                {
                    Caption = 'itemCode';
                }

                field(description; Description)
                {
                    Caption = 'description';
                }

                field("unitOfMeasure"; "Unit of Measure")
                {
                    Caption = 'unitOfMeasure';
                }

                field(quantity; Quantity)
                {
                    Caption = 'quantity';
                }

                field("unitPrice"; "Unit Price")
                {
                    Caption = 'unitPrice';
                }

                field("quantityShipped"; "Quantity Shipped")
                {
                    Caption = 'quantityShipped';
                }

                field("shipmentDate"; "Shipment Date")
                {
                    Caption = 'shipmentDate';
                }

                field("lineDiscountPercent"; "Line Discount %")
                {
                    Caption = 'lineDiscountPercent';
                }

                field("vatPercent"; "VAT %")
                {
                    Caption = 'vatPercent';
                }

                field("type"; Type)
                {
                    Caption = 'type';
                }

                field("amount"; Amount)
                {
                    Caption = 'amount';
                }

                field("sellToCustomerNo"; "Sell-to Customer No.")
                {
                    Caption = 'sellToCustomerNo';
                }
            }
        }
    }
}

We did POST request to route:
/v2.0/Production/api/Tests/TestApp/v1.0/companies(b35e5010-ded7-ea11-96a6-000d3aac12ef)/SalesHeaderLines

Can, please, someone explain what we need to do for succesfull sales line POST?
Thanks in advance!

 

I have the same question (0)
  • Suggested answer
    JAngle Profile Picture
    133 on at

    Have you tried swapping the parameters round? Following primary key of the table is best. Document Type is before document no. in the case of header and line

  • Community Member Profile Picture
    on at

    Thank you for answer

    This did the trick

    Thanks again!

  • Jack Lin Profile Picture
    10 on at

    i have the same error.

    error: { "code": "Internal_InvalidTableRelation", "message": "The field Document No. of table Sales Line contains a value (S-INVXXXXX) that cannot be found in the related table (Sales Header). CorrelationId: 4f54d106-d4b5-42ef-9915-01fb91b8826b." }

    but my case is little bit different, the issue only happens intermittent. After we re-run the program and it went through. Anyone know what would cause this and how to debug it?

    Thanks

  • Suggested answer
    Suresh Kulla Profile Picture
    50,243 Super User 2025 Season 2 on at

    You API page references sales line, if you are trying to create return order or invoice you need to create the header and then the line.

  • Jack Lin Profile Picture
    10 on at

    Thanks for your reply.

    Actually I created the header first. As you can see in my previous comment, I got the sales invoice number and I just changed it in to "X" in this post manually. Besides this, I checked this sales invoice number on Business Central right after I got this error and the sales invoice header is in there. The most interesting thing is that this issue some times happened on when my program was trying to generate after the first sales invoice line, which mean sometimes I got this error on generating the 10th line, the 20th line or even the 200th line. I used default header page and line page from Business Central (43 - Sales Invoice and 6043 - Sales Document Line Entity).

  • CU24051328-0 Profile Picture
    on at
    Hi Jack Lin,

    what was the solution for this issue. ?

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,010

#2
Jainam M. Kothari Profile Picture

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

#3
YUN ZHU Profile Picture

YUN ZHU 1,085 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans