Skip to main content

Notifications

Small and medium business | Business Central, N...
Suggested answer

Create Journal Line API Call - Document Type is blank when Journal Line created in Dynamics

Posted on by 20

Hello - we are creating a Journal lines in Dynamics from Salesforce using the Dynamics API.  When creating journal lines in a Commission Journal the Document Type is showing up as Blank in Dynamics.  For the other Journals where we create Journal Lines the Document Type is automatically set to Invoice.

Does anyone know what controls the Document Type getting set automatically in Dynamics or any way to set the Document Type on a Journal Line w/ an API?

  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 73,472 Super User 2024 Season 2 on at
    RE: Create Journal Line API Call - Document Type is blank when Journal Line created in Dynamics

    Hi, Maybe there is another good way, I used to use "Download-Artifacts" command to download the latest package.

    For example,

    Download-Artifacts -artifactUrl (Get-BCArtifactUrl) -includePlatform

    Using this way, you can get all Microsoft's first-party app files in "C:\bcartifacts.cache\sandbox\21.3.51409.52092\w1\Extensions", and you can see the source code after decompression.

    pastedimage1675303714205v1.png

    PS: For details about Download-Artifacts, you can check Freddys' blog, there should be no more detailed content than this.

    https://freddysblog.com/2020/06/25/working-with-artifacts/

    pastedimage1675303816719v2.png

    Hope this helps.

    Thanks.

    ZHU

  • dboyceman Profile Picture
    dboyceman 20 on at
    RE: Create Journal Line API Call - Document Type is blank when Journal Line created in Dynamics

    yzhums

    Thanks for the code investigation.  How were you able to see the code of the API call?  Did you use Telerik JustDecompile or something equivalent?  Git Repo? F12 Developer Tools monitoring network calls?

  • Suggested answer
    YUN ZHU Profile Picture
    YUN ZHU 73,472 Super User 2024 Season 2 on at
    RE: Create Journal Line API Call - Document Type is blank when Journal Line created in Dynamics

    Hi, I checked the standard code of this API. In the trigger OnNewRecord, the Document Type is set to empty.
    And there is no Document Type field in this API. I don't think it is possible to specify Document Type using this standard API.

    pastedimage1675301284125v1.png

    page 30049 "APIV2 - JournalLines"
    {
        APIVersion = 'v2.0';
        EntityCaption = 'Journal Line';
        EntitySetCaption = 'Journal Lines';
        DelayedInsert = true;
        ODataKeyFields = SystemId;
        PageType = API;
        EntityName = 'journalLine';
        EntitySetName = 'journalLines';
        SourceTable = "Gen. Journal Line";
        Extensible = false;
    
        layout
        {
            area(content)
            {
                repeater(Control2)
                {
                    ShowCaption = false;
                    field(id; SystemId)
                    {
                        Caption = 'Id';
                        Editable = false;
                    }
                    field(journalId; "Journal Batch Id")
                    {
                        Caption = 'Journal Id';
                    }
                    field(journalDisplayName; GlobalJournalDisplayNameTxt)
                    {
                        Caption = 'Journal Display Name';
    
                        trigger OnValidate()
                        begin
                            Error(CannotEditBatchNameErr);
                        end;
                    }
                    field(lineNumber; "Line No.")
                    {
                        Caption = 'Line No.';
                    }
                    field(accountType; "Account Type")
                    {
                        Caption = 'Account Type';
                    }
                    field(accountId; "Account Id")
                    {
                        Caption = 'Account Id';
    
                        trigger OnValidate()
                        begin
                            if "Account Id" = BlankGUID then begin
                                "Account No." := '';
                                exit;
                            end;
                            if "Account Type" = "Account Type"::"G/L Account" then begin
                                if not GLAccount.GetBySystemId("Account Id") then
                                    Error(AccountIdDoesNotMatchAnAccountErr);
                                "Account No." := GLAccount."No.";
                            end;
                            if "Account Type" = "Account Type"::"Bank Account" then
                                if BankAccount.GetBySystemId("Account Id") then
                                    "Account No." := BankAccount."No."
                                else
                                    Error(AccountIdDoesNotMatchAnAccountErr);
                        end;
                    }
                    field(accountNumber; "Account No.")
                    {
                        Caption = 'Account No.';
    
                        trigger OnValidate()
                        begin
                            case "Account Type" of
                                "Account Type"::"G/L Account":
                                    UpdateAccountIdForGLAccount();
                                "Account Type"::"Bank Account":
                                    UpdateAccountIdForBankAccount();
                            end;
                        end;
                    }
                    field(postingDate; "Posting Date")
                    {
                        Caption = 'Posting Date';
                    }
                    field(documentNumber; "Document No.")
                    {
                        Caption = 'Document No.';
                    }
                    field(externalDocumentNumber; "External Document No.")
                    {
                        Caption = 'External Document No.';
                    }
                    field(amount; Amount)
                    {
                        Caption = 'Amount';
                    }
                    field(description; Description)
                    {
                        Caption = 'Description';
                    }
                    field(comment; Comment)
                    {
                        Caption = 'Comment';
                    }
                    field(taxCode; TaxCode)
                    {
                        Caption = 'Tax Code';
    
                        trigger OnValidate()
                        var
                            GeneralLedgerSetup: Record "General Ledger Setup";
                        begin
                            if GeneralLedgerSetup.UseVat() then
                                Rec."VAT Prod. Posting Group" := TaxCode
                            else
                                Rec."Tax Group Code" := TaxCode;
                        end;
                    }
    
                    field(balanceAccountType; "Bal. Account Type")
                    {
                        Caption = 'Balance Account Type';
                    }
                    field(balancingAccountId; "Balance Account Id")
                    {
                        Caption = 'Balancing Account Id';
    
                        trigger OnValidate()
                        begin
                            if "Balance Account Id" = BlankGUID then begin
                                "Bal. Account No." := '';
                                exit;
                            end;
                            case "Bal. Account Type" of
                                "Bal. Account Type"::"G/L Account":
                                    begin
                                        if not GLAccount.GetBySystemId("Balance Account Id") then
                                            Error(BalAccountIdDoesNotMatchAnAccountErr);
                                        "Bal. Account No." := GLAccount."No.";
                                    end;
                                "Bal. Account Type"::"Bank Account":
                                    begin
                                        if not BankAccount.GetBySystemId("Balance Account Id") then
                                            Error(BalAccountIdDoesNotMatchAnAccountErr);
                                        "Bal. Account No." := BankAccount."No.";
                                    end;
                            end;
                        end;
                    }
                    field(balancingAccountNumber; "Bal. Account No.")
                    {
                        Caption = 'Balancing Account No.';
                    }
                    field(lastModifiedDateTime; SystemModifiedAt)
                    {
                        Caption = 'Last Modified Date';
                        Editable = false;
                    }
                    part(attachments; "APIV2 - Attachments")
                    {
                        Caption = 'Attachments';
                        EntityName = 'attachment';
                        EntitySetName = 'attachments';
                        SubPageLink = "Document Id" = Field(SystemId), "Document Type" = const(Journal);
                    }
                    part(dimensionSetLines; "APIV2 - Dimension Set Lines")
                    {
                        Caption = 'Dimension Set Lines';
                        EntityName = 'dimensionSetLine';
                        EntitySetName = 'dimensionSetLines';
                        SubPageLink = "Parent Id" = Field(SystemId), "Parent Type" = const("Journal Line");
                    }
                }
            }
        }
    
        actions
        {
        }
    
        trigger OnAfterGetCurrRecord()
        begin
            if not FiltersChecked then begin
                CheckFilters();
                FiltersChecked := true;
            end;
        end;
    
        trigger OnAfterGetRecord()
        begin
            SetCalculatedFields();
        end;
    
        trigger OnInsertRecord(BelowxRec: Boolean): Boolean
        var
            GenJournalBatch: Record "Gen. Journal Batch";
            TempGenJournalLine: Record "Gen. Journal Line" temporary;
        begin
            if IsNullGuid(Rec."Journal Batch Id") then
                CheckFilters();
    
            TempGenJournalLine.Reset();
            TempGenJournalLine.Copy(Rec);
    
            Clear(Rec);
            if IsNullGuid(TempGenJournalLine."Journal Batch Id") then
                GenJournalBatch.GetBySystemId(TempGenJournalLine.GetFilter("Journal Batch Id"))
            else
                GenJournalBatch.GetBySystemId(TempGenJournalLine."Journal Batch Id");
    
            GraphMgtJournalLines.SetJournalLineTemplateAndBatchV2(Rec, GenJournalBatch);
            LibraryAPIGeneralJournal.InitializeLine(
              Rec, TempGenJournalLine."Line No.", TempGenJournalLine."Document No.", TempGenJournalLine."External Document No.");
    
            GraphMgtJournalLines.SetJournalLineValues(Rec, TempGenJournalLine);
    
            SetCalculatedFields();
        end;
    
        trigger OnModifyRecord(): Boolean
        var
            GenJournalLine: Record "Gen. Journal Line";
        begin
            GenJournalLine.GetBySystemId(SystemId);
    
            if Rec."Journal Batch Id" <> GenJournalLine."Journal Batch Id" then
                Error(CannotEditBatchIdErr);
    
            if "Line No." = GenJournalLine."Line No." then
                Modify(true)
            else begin
                GenJournalLine.TransferFields(Rec, false);
                GenJournalLine.Rename("Journal Template Name", "Journal Batch Name", "Line No.");
                TransferFields(GenJournalLine, true);
            end;
    
            SetCalculatedFields();
    
            exit(false);
        end;
    
        trigger OnNewRecord(BelowxRec: Boolean)
        begin
            ClearCalculatedFields();
    
            "Document Type" := "Document Type"::" ";
            "Account Type" := "Account Type"::"G/L Account";
        end;
    
        trigger OnOpenPage()
        begin
            Rec.SetRange("Document Type", Rec."Document Type"::" ");
            Rec.SetRange("Account Type", Rec."Account Type"::"G/L Account", Rec."Account Type"::"Bank Account");
        end;
    
        var
            GLAccount: Record "G/L Account";
            BankAccount: Record "Bank Account";
            GraphMgtJournalLines: Codeunit "Graph Mgt - Journal Lines";
            LibraryAPIGeneralJournal: Codeunit "Library API - General Journal";
            FiltersNotSpecifiedErr: Label 'You must specify a journal batch ID or a journal ID to get a journal line.';
            CannotEditBatchNameErr: Label 'The Journal Batch Display Name isn''t editable.';
            CannotEditBatchIdErr: Label 'The Journal Batch Id isn''t editable.';
            AccountValuesDontMatchErr: Label 'The account values do not match to a specific Account.';
            AccountIdDoesNotMatchAnAccountErr: Label 'The "accountId" does not match to an Account.', Comment = 'accountId is a field name and should not be translated.';
            BalAccountIdDoesNotMatchAnAccountErr: Label 'The "balancingAccountId" does not match to an Account.', Comment = 'balancingAccountId is a field name and should not be translated.';
            AccountNumberDoesNotMatchAnAccountErr: Label 'The "accountNumber" does not match to an Account.', Comment = 'accountNumber is a field name and should not be translated.';
            GlobalJournalDisplayNameTxt: Code[10];
            TaxCode: Code[20];
            FiltersChecked: Boolean;
            BlankGUID: Guid;
    
        local procedure SetCalculatedFields()
        var
            GeneralLedgerSetup: Record "General Ledger Setup";
        begin
            GlobalJournalDisplayNameTxt := "Journal Batch Name";
            if GeneralLedgerSetup.UseVat() then
                TaxCode := "VAT Prod. Posting Group"
            else
                TaxCode := "Tax Group Code";
        end;
    
        local procedure ClearCalculatedFields()
        begin
            Clear(GlobalJournalDisplayNameTxt);
            Clear(TaxCode);
        end;
    
        local procedure CheckFilters()
        begin
            if (GetFilter("Journal Batch Id") = '') and
               (GetFilter(SystemId) = '')
            then
                Error(FiltersNotSpecifiedErr);
        end;
    
        local procedure UpdateAccountIdForGLAccount();
        begin
            if GLAccount."No." <> '' then begin
                if GLAccount."No." <> "Account No." then
                    Error(AccountValuesDontMatchErr);
                exit;
            end;
    
            if "Account No." = '' then begin
                "Account Id" := BlankGUID;
                exit;
            end;
    
            if not GLAccount.Get("Account No.") then
                Error(AccountNumberDoesNotMatchAnAccountErr);
    
            "Account Id" := GLAccount.SystemId;
        end;
    
        local procedure UpdateAccountIdForBankAccount();
        begin
            if BankAccount."No." <> '' then begin
                if BankAccount."No." <> "Account No." then
                    Error(AccountValuesDontMatchErr);
                exit;
            end;
    
            if "Account No." = '' then begin
                "Account Id" := BlankGUID;
                exit;
            end;
    
            if not BankAccount.Get("Account No.") then
                Error(AccountNumberDoesNotMatchAnAccountErr);
    
            "Account Id" := BankAccount.SystemId;
        end;
    }
    
    

    Hopefully other experts can give you better advice.

    Thanks.
    ZHU

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!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

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

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans