Skip to main content

Notifications

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!

 

  • CU24051328-0 Profile Picture
    on at
    AL scripting. API Page for POST Return Sales order lines
    Hi Jack Lin,

    what was the solution for this issue. ?
  • Jack Lin Profile Picture
    10 on at
    RE: AL scripting. API Page for POST Return Sales order lines

    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).

  • Suggested answer
    Suresh Kulla Profile Picture
    46,792 on at
    RE: AL scripting. API Page for POST Return Sales order lines

    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
    RE: AL scripting. API Page for POST Return Sales order lines

    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

  • Community Member Profile Picture
    on at
    RE: AL scripting. API Page for POST Return Sales order lines

    Thank you for answer

    This did the trick

    Thanks again!

  • Suggested answer
    JAngle Profile Picture
    83 on at
    RE: AL scripting. API Page for POST Return Sales order lines

    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

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,033 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,854 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans