Skip to main content

Notifications

Announcements

No record found.

Business Central forum
Suggested answer

Custom Api - Deep Insert

Posted on by 12
I am developing a custom apipage  which follows a header / lines pattern as such following the example I am able to create a PART page on the header api so I can make a deep insert. This works as expected.
 
Question: I want to verify on the validation code that the sum of the line quantities equal the totalquantity value in the header - how can i access the lineitems from the header record so i can verify that TotalQuantity equals the sum of the lines?  Is there a way to directy access the post jsonbody from the page triggers of the header?
 
Thanks so much!
 
Example
{
No: 1,
Name: order 1,
TotalQuantity: 12
[
{LineNumber:1, Qty: 5},
{LineNumber:2, Qty: 7}
}
Categories:
  • Suggested answer
    Nyakinyua Profile Picture
    Nyakinyua 17 on at
    Custom Api - Deep Insert
    This is the approach I'd take
     
    [EventSubscriber(ObjectType::Table, Database::<Table Name>, 'OnBeforeInsertEvent', '', <SkipOnMissingLicense>, <SkipOnMissingPermission>)]
        local procedure OnBeforeInsert(var Rec: Record "YourHeaderTable"; var xRec: Record "YourHeaderTable"; RunTrigger: Boolean)
        var
            JsonObject: JsonObject;
            LineItemsArray: JsonArray;
            LineItem: JsonObject;
            TotalQuantity: Integer;
            SumQty: Integer;
        begin
            // Parse the JSON body from the API request
            JsonObject := JsonObject.Parse(Rec.RequestBody);
     
            // Extract TotalQuantity from the header
            TotalQuantity := JsonObject.GetValue('TotalQuantity');
     
            // Extract LineItems array
            LineItemsArray := JsonObject.GetArray('LineItems');
            SumQty := 0;
     
            // Loop through LineItems and sum quantities
            foreach LineItem in LineItemsArray do begin
                SumQty += LineItem.GetValue('Qty');
            end;
     
            // Validate that the sum of quantities matches the TotalQuantity
            if SumQty <> TotalQuantity then
                Error('The sum of line quantities (%1) must equal the TotalQuantity (%2).', SumQty, TotalQuantity);
        end;

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,537 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,520 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans