Skip to main content

Notifications

Announcements

No record found.

Business Central forum
Suggested answer

AL scripting. API Page for POST Return Sales order lines

Posted on by Microsoft Employee

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!

 

Categories:
  • CU24051328-0 Profile Picture
    CU24051328-0 7 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
    Jack Lin 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
    Suresh Kulla 43,638 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
    Jack Lin 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
    Community Member Microsoft Employee 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
    JAngle 33,135 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

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 287,696 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,490 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans