Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Business Central forum

Create api with deep insert

(0) ShareShare
ReportReport
Posted on by

I try to create web api extension with 2 page in Ms Dynamic 365 BC. my goal is create api like sales order which is can insert sales order header and sales order line in one post, below are my AL code.

page 50103 ScannedShipmentMstEntity
{
    PageType = API;
    Caption = 'ScannedShipmentMstEntity';
    EntityName = 'ScannedShipmentMst';
    EntitySetName = 'ScannedShipmentMsts';
    SourceTable = ScannedShipmentMst;
    DelayedInsert = true;
    ODataKeyFields = id;
    UsageCategory = Lists;

    layout
    {
        area(Content)
        {
            repeater(General)
            {
                field(id; id)
                {
                    Caption = 'id';
                    ApplicationArea = All;
                }
                field(header; header)
                {
                    Caption = 'Header';
                    ApplicationArea = All;
                }
                part(ScannedShipmentDataEntity; ScannedShipmentDataEntity)
                {
                    ApplicationArea = All;
                    Caption = 'Lines', Locked = true;
                    EntityName = 'ScannedShipmentDataEntity';
                    EntitySetName = 'ScannedShipmentDataEntities';
                    SubPageLink = BatchId = FIELD (header);
                }
            }
        }
    }

    trigger OnInsertRecord(BelowxRec: Boolean): Boolean
    begin
        Insert(true);
        exit(false);
    end;

    trigger OnDeleteRecord(): Boolean
    begin
        Delete(true);
    end;
}

page 50101 ScannedShipmentDataEntity
{
    Caption = 'ScannedShipmentDataEntity';
    DelayedInsert = true;
    PageType = ListPart;
    SourceTable = ScannedShipmentData;
    SourceTableTemporary = true;

    layout
    {
        area(Content)
        {
            repeater(General)
            {
                field(id; id)
                {
                    Caption = 'Id';
                    ApplicationArea = All;
                }
                field(Transdate; Transdate)
                {
                    Caption = 'Transdate';
                    ApplicationArea = All;
                }
                field(OrderNum; OrderNum)
                {
                    Caption = 'OrderNum';
                    ApplicationArea = All;
                }
                field(OrderLine; OrderLine)
                {
                    Caption = 'OrderLine';
                    ApplicationArea = All;
                }
                field(item; item)
                {
                    Caption = 'item';
                    ApplicationArea = All;
                }
                field(Desc; Desc)
                {
                    Caption = 'Desc';
                    ApplicationArea = All;
                }
                field(um; um)
                {
                    Caption = 'um';
                    ApplicationArea = All;
                }
                field(ShipQty; ShipQty)
                {
                    Caption = 'ShipQty';
                    ApplicationArea = All;
                }
                field(ScanQty; ScanQty)
                {
                    Caption = 'ScanQty';
                    ApplicationArea = All;
                }
                field(ScanBox; ScanBox)
                {
                    Caption = 'ScanBox';
                    ApplicationArea = All;
                }

                field(BatchId; BatchId)
                {
                    ApplicationArea = All;
                    Caption = 'BatchId';
                }
            }
        }
    }

    trigger OnInsertRecord(BelowxRec: Boolean): Boolean
    begin
        Insert(true);
        exit(false);
    end;

    trigger OnDeleteRecord(): Boolean
    begin
        Delete(true);
    end;
}

but when i try to post it in postman with body

{
    "header" : "Test2",
    "scannedShipmentDataEntities" :[
            {
            "OrderNum": "C9",
            "OrderLine": 1,
            "item": "Item 1",
            "um": "KG",
            "ShipQty": 200
            },
            {
            "OrderNum": "C8",
            "OrderLine": 2,
            "item": "Item 2",
            "um": "KG",
            "ShipQty": 200
            }
        ]
}

and the response

{
    "@odata.context": "webapiurl/api/beta/$metadata#scannedShipmentMsts/$entity",
    "@odata.etag": "W/\"JzQ0O1pJZWY3RlpOUmo3MHBXZTkzQkUwWlpzcXM0UkdqSjBDNithYnBES3pYQWc9MTswMDsn\"",
    "id": "d605d939-7c57-4e7a-9313-1f589d129759",
    "header": "Test2"
}

then run Get request to see the result

{{WebAPIUrl}}/scannedShipmentMsts(d605d939-7c57-4e7a-9313-1f589d129759)?$expand=scannedShipmentDataEntities

and the response

{
    "@odata.context": "webapiurl/api/beta/$metadata#scannedShipmentMsts/$entity",
    "@odata.etag": "W/\"JzQ0O1pJZWY3RlpOUmo3MHBXZTkzQkUwWlpzcXM0UkdqSjBDNithYnBES3pYQWc9MTswMDsn\"",
    "id": "d605d939-7c57-4e7a-9313-1f589d129759",
    "header": "Test2",
    "scannedShipmentDataEntities": []
}

looks like only header can be saved but not the line.
Is there any step that i miss? please your suggestion to solve this problem.

Thank You

Categories:
  • Suggested answer
    DAnny3211 Profile Picture
    9,276 Moderator on at
    RE: Create api with deep insert

    hello

    the part name must be equal to the EntitySetName

    like this

    part(ScannedShipmentDataEntities; ScannedShipmentDataEntity)

                   {

                       ApplicationArea = All;

                       Caption = 'Lines', Locked = true;

                       EntityName = 'ScannedShipmentDataEntity';

                       EntitySetName = 'ScannedShipmentDataEntities';

                       SubPageLink = BatchId = FIELD (header);

                   }

    but I think your main problem is that the subpage is temporary

    DAniele

  • Khadija  Profile Picture
    27 on at
    RE: Create api with deep insert

    Hi  Yudi,

    if you solve your problem could you share the solution with us. I have the same requirements

    best regard

  • Jhonatan Ramirez Profile Picture
    40 on at
    RE: Create api with deep insert

    the problem could be because your entity is declared as temporal record, then you need to add the process to fetch the real records from your target table

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 > Business Central forum

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans