Skip to main content

Notifications

Small and medium business | Business Central, N...
Unanswered

Business Central - Create and post Journal batch in a $batch

Posted on by 5

Hi there,

I have created an API endpoint for posting a $batch on api.businesscentral.dynamics.com/.../$batch
When I call it with the payload below, the Journal Header and lines create with no issue, but the postBatch call always fails when its part of the $batch.
But when I call it in a separate $batch entirely it works and posts the Journal Batch (assuming no errors).

{
    "requests": [
        {
            "method": "POST",
            "id": "r1",
            "url": "journalBatches?companyName=CBP",
            "headers": {
                "Content-Type": "application/json"
            },
            "body": {
                "JournalTemplate": "GENERAL",
                "JournalBatchName": "QWERTY19",
                "Description": "Default Journal Batch",
                "CopyVAT": false
            }
        },
        {
            "method": "POST",
            "id": "r2",
            "url": "journalLines?companyName=CBP",
            "headers": {
                "Content-Type": "application/json"
            },
            "body": {
                "JournalTemplate": "GENERAL",     
                "LineNo": 1,           
                "JournalBatchName": "QWERTY19",
                "PostingDate": "2023-04-13",
                "AccountType": "G/L Account",
                "AccountNo": "9100",
                "DocumentNo": "1",
                "ExternalDocumentNo": "5",
                "DocumentType": "Invoice",
                "Amount": 5,
                "Description": "Default Journal Batch",
                "Comment": "comment",
                "ShortcutDimension1Code": "115",
                "ShortcutDimension2Code": "210"
            }
        },
        {
            "method": "POST",
            "id": "r3",
            "url": "journalLines?companyName=CBP",
            "headers": {
                "Content-Type": "application/json"
            },
            "body": {
                "JournalTemplate": "GENERAL",     
                "LineNo": 2,                      
                "JournalBatchName": "QWERTY19",
                "PostingDate": "2023-04-13",
                "AccountType": "G/L Account",
                "AccountNo": "9100",
                "DocumentNo": "2",
                "ExternalDocumentNo": "6",
                "DocumentType": "Invoice",
                "Amount": -5,
                "Description": "Default Journal Batch",
                "Comment": "comment",
                "ShortcutDimension1Code": "115",
                "ShortcutDimension2Code": "210"
            }
        },
        {
            "method": "POST",
            "id": "r4",
            "url": "journalBatches(JournalTemplate='GENERAL',JournalBatchName='QWERTY19')/Microsoft.NAV.postBatch?companyName=CBP",
            "headers": {
                "Content-Type": "application/json"
            }            
        }
    ]
}

Below is the code response when I call the above in Postman

{
    "responses": [
        {
            "id": "r1",
            "status": 201,
            "headers": {
                "location": "https://api.businesscentral.dynamics.com/v2.0/f519b312-7578-4094-a321-3615690bed23/sandbox/api/Thyme/JournalBatchGroup/v2.0/journalBatches(JournalTemplate='GENERAL',JournalBatchName='QWERTY19')",
                "content-type": "application/json; odata.metadata=minimal; odata.streaming=true",
                "odata-version": "4.0"
            },
            "body": {
                "@odata.context": "https://api.businesscentral.dynamics.com/v2.0/f519b312-7578-4094-a321-3615690bed23/sandbox/api/Thyme/JournalBatchGroup/v2.0/$metadata#journalBatches/$entity",
                "@odata.etag": "W/\"JzIwOzE0NTM3ODEzOTQwMjAwNjExNzMwMTswMDsn\"",
                "JournalTemplate": "GENERAL",
                "JournalBatchName": "QWERTY19",
                "Description": "Default Journal Batch",
                "CopyVAT": false
            }
        },
        {
            "id": "r2",
            "status": 201,
            "headers": {
                "location": "https://api.businesscentral.dynamics.com/v2.0/f519b312-7578-4094-a321-3615690bed23/sandbox/api/Thyme/JournalBatchGroup/v2.0/journalLines(JournalTemplate='GENERAL',JournalBatchName='QWERTY19',LineNo=1)",
                "content-type": "application/json; odata.metadata=minimal; odata.streaming=true",
                "odata-version": "4.0"
            },
            "body": {
                "@odata.context": "https://api.businesscentral.dynamics.com/v2.0/f519b312-7578-4094-a321-3615690bed23/sandbox/api/Thyme/JournalBatchGroup/v2.0/$metadata#journalLines/$entity",
                "@odata.etag": "W/\"JzE5OzU0NjE2NzkyNTMzODkyMjkxNzYxOzAwOyc=\"",
                "JournalTemplate": "GENERAL",
                "JournalBatchName": "QWERTY19",
                "LineNo": 1,
                "PostingDate": "2023-04-13",
                "AccountType": "G/L Account",
                "AccountNo": "9100",
                "DocumentNo": "1",
                "ExternalDocumentNo": "5",
                "DocumentType": "Invoice",
                "Amount": 5.00,
                "Description": "Default Journal Batch",
                "Comment": "comment",
                "ShortcutDimension1Code": "115",
                "ShortcutDimension2Code": "210"
            }
        },
        {
            "id": "r3",
            "status": 201,
            "headers": {
                "location": "https://api.businesscentral.dynamics.com/v2.0/f519b312-7578-4094-a321-3615690bed23/sandbox/api/Thyme/JournalBatchGroup/v2.0/journalLines(JournalTemplate='GENERAL',JournalBatchName='QWERTY19',LineNo=2)",
                "content-type": "application/json; odata.metadata=minimal; odata.streaming=true",
                "odata-version": "4.0"
            },
            "body": {
                "@odata.context": "https://api.businesscentral.dynamics.com/v2.0/f519b312-7578-4094-a321-3615690bed23/sandbox/api/Thyme/JournalBatchGroup/v2.0/$metadata#journalLines/$entity",
                "@odata.etag": "W/\"JzE5OzE4Njg3MTYzNzM4NzE4MjA4NzIxOzAwOyc=\"",
                "JournalTemplate": "GENERAL",
                "JournalBatchName": "QWERTY19",
                "LineNo": 2,
                "PostingDate": "2023-04-13",
                "AccountType": "G/L Account",
                "AccountNo": "9100",
                "DocumentNo": "2",
                "ExternalDocumentNo": "6",
                "DocumentType": "Invoice",
                "Amount": -5.00,
                "Description": "Default Journal Batch",
                "Comment": "comment",
                "ShortcutDimension1Code": "115",
                "ShortcutDimension2Code": "210"
            }
        },
        {
            "id": "r4",
            "status": 400,
            "headers": {
                "content-type": "application/json",
                "content-length": "220"
            },
            "body": {
                "error": {
                    "code": "Internal_ServerError",
                    "message": "An error occurred and the transaction is stopped. Contact your administrator or partner for further assistance.  CorrelationId:  7d7aae9a-604d-4a21-95da-9038ab73fe5f."
                }
            }
        }
    ]
}

Below is my AL code relevant to this project

page 60100 JournalBatchAPI
{
    PageType = API;
    Caption = 'JournalBatch';
    APIPublisher = 'Thyme';
    APIGroup = 'JournalBatchGroup';
    APIVersion = 'v2.0';
    EntityName = 'JournalBatch';
    EntitySetName = 'JournalBatches';
    SourceTable = "Gen. Journal Batch";
    DelayedInsert = true;
    SourceTableView = where("Journal Template Name" = const('GENERAL'));

    layout
    {
        area(Content)
        {
            repeater(GroupName)
            {
                field(JournalTemplate; rec."Journal Template Name")
                {
                    ApplicationArea = All;
                    Caption = 'JournalTemplate';
                }
                field(JournalBatchName; rec.Name)
                {
                    ApplicationArea = All;
                    Caption = 'JournalBatchName';
                }
                field(Description; Rec.Description)
                {
                    ApplicationArea = All;
                    Caption = 'Description';
                }
                field(CopyVAT; Rec."Copy VAT Setup to Jnl. Lines")
                {
                    ApplicationArea = All;
                    Caption = 'CopyVAT';
                }
            }
        }
    }

    [ServiceEnabled]
    procedure PostBatch(var ActionContext: WebServiceActionContext)
    var
        GenJournalPostBatch: Codeunit "Gen. Jnl.-B.Post";

    begin
        GenJournalPostBatch.Run(Rec);

        ActionContext.SetObjectType(ObjectType::Page);
        ActionContext.SetObjectId(Page::JournalBatchAPI);
        ActionContext.AddEntityKey(rec.FieldNo(Name), Rec.Name);
        ActionContext.SetResultCode(WebServiceActionResultCode::Created);
    end;
}

page 60101 JournalLineAPI
{
    PageType = API;
    Caption = 'JournalLine';
    APIPublisher = 'Thyme';
    APIGroup = 'JournalBatchGroup';
    APIVersion = 'v2.0';
    EntityName = 'JournalLine';
    EntitySetName = 'JournalLines';
    SourceTable = "Gen. Journal Line";
    DelayedInsert = true;
    
    layout
    {
        area(Content)
        {
            repeater(GroupName)
            {
                field(JournalTemplate; rec."Journal Template Name")
                {
                    Caption = 'JournalTemplate';
                }
                field(JournalBatchName; rec."Journal Batch Name")
                {
                    Caption = 'JournalBatchName';
                }
                field(LineNo; rec."Line No.")
                {
                    Caption = 'LineNo';
                }
                field(PostingDate; rec."Posting Date")
                {
                    Caption = 'PostingDate';
                }
                field(AccountType; rec."Account Type")
                {
                    Caption = 'AccountType';
                }
                field(AccountNo; rec."Account No.")
                {
                    Caption = 'AccountNo';
                }
                field(DocumentNo; rec."Document No.")
                {
                    Caption = 'DocumentNo';
                }
                field(ExternalDocumentNo; rec."External Document No.")
                {
                    Caption = 'ExternalDocumentNo';
                }
                field(DocumentType; rec."Document Type")
                {
                    Caption = 'DocumentType';
                }
                field(Amount; rec.Amount)
                {
                    Caption = 'Amount';
                }
                field(Description; rec.Description)
                {
                    Caption = 'Description';
                }
                field(Comment; rec.Comment)
                {
                    Caption = 'Comment';
                }
                field("ShortcutDimension1Code"; Rec."Shortcut Dimension 1 Code")
                {
                    Caption = '"ShortcutDimension1Code";Rec."Shortcut Dimension 1 Code"';
                }
                field("ShortcutDimension2Code"; Rec."Shortcut Dimension 2 Code")
                {
                    Caption = '"ShortcutDimension2Code";Rec."Shortcut Dimension 1 Code"';
                }
            }
        }
    }
}

Categories:

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

New! Quick response templatesâš¡

Save time with the new custom templates!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,151 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 229,963 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans