web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Answered

I have a problem entering the Currency Code value via XML

(3) ShareShare
ReportReport
Posted on by 322
Hi 
I have a problem entering the Currency Code value via XML
 
When I enter the currency value code according to XML    next "" it does not enter on the "General Journals" screen why?
 
 
General Journals" screen
 
Please help from experienced people
 
AL Code AL
===================================================================
codeunit 50000 "ReadJsonCodeunit"
{
    procedure ReadAndInsertJson(JsonText: Text)
    var
        JsonObject: JsonObject;
        JsonArray: JsonArray;
        JsonToken: JsonToken;
        LineObject: JsonObject;
        DimensionArray: JsonArray;
        DimensionObject: JsonObject;
        GenJournalLine: Record "Gen. Journal Line";
        GenJournalBatch: Record "Gen. Journal Batch";
        TempDimSetEntry: Record "Dimension Set Entry" temporary;
        DimMgt: Codeunit DimensionManagement;
        JournalTemplateName: Code[10];
        JournalBatchName: Code[10];
        LineNo: Integer;
        i, j : Integer;
        DimCode: Code[20];
        DimValueCode: Code[20];
        DimensionSetID: Integer;
        DocumentNo: Code[20];
        DocumentType: Option " ","Invoice","Payment","CreditMemo","DebitMemo";
        DocumentTypeText: Text;
        CurrencyCode: Code[10];
        DebitAmount: Decimal;
        CreditAmount: Decimal;
        AmountLCY: Decimal;
        AccountTypeText: Text;
        AccountType: Enum "Gen. Journal Account Type";
    begin
        Message('Starting ReadAndInsertJson Procedure.');
 
        if not JsonObject.ReadFrom(JsonText) then
            Error('Invalid JSON format.');
 
        if not JsonObject.Get('JournalTemplate', JsonToken) then
            Error('JournalTemplate is missing.');
        JournalTemplateName := JsonToken.AsValue().AsText();
 
        if not JsonObject.Get('JournalBatch', JsonToken) then
            Error('JournalBatch is missing.');
        JournalBatchName := JsonToken.AsValue().AsText();
 
        if not GenJournalBatch.Get(JournalTemplateName, JournalBatchName) then
            Error('Journal batch not found: %1 - %2', JournalTemplateName, JournalBatchName);
 
        if JsonObject.Get('Lines', JsonToken) then begin
            JsonArray := JsonToken.AsArray();
 
            for i := 0 to JsonArray.Count() - 1 do begin
                JsonArray.Get(i, JsonToken);
                LineObject := JsonToken.AsObject();
 
                /*     if GenJournalLine.FindLast() then
                         LineNo := GenJournalLine."Line No." + 10000
                     else
                         LineNo := 10000; */
 
                LineNo := GetLastLineNo(JournalTemplateName, JournalBatchName) + 10000;
 
                // التحقق من أن DocumentNo ليس فارغًا
                if not LineObject.Get('DocumentNo', JsonToken) then
                    Error('DocumentNo is missing at line %1', i + 1);
                DocumentNo := JsonToken.AsValue().AsText();
                if DocumentNo = '' then
                    Error('DocumentNo cannot be empty at line %1', i + 1);
 
                // التحقق من أن DocumentType ليس فارغًا
                if LineObject.Get('DocumentType', JsonToken) then begin
                    DocumentTypeText := JsonToken.AsValue().AsText();
 
                    case DocumentTypeText of
                        'Invoice':
                            DocumentType := DocumentType::Invoice;
                        'Payment':
                            DocumentType := DocumentType::Payment;
                        'CreditMemo':
                            DocumentType := DocumentType::CreditMemo;
                        else
                            DocumentType := DocumentType::" ";
                    end;
                end else begin
                    DocumentType := DocumentType::" ";
                end;
 
               
                if LineObject.Get('CurrencyCode', JsonToken) then begin
                    CurrencyCode := JsonToken.AsValue().AsText();
                end else begin
                    CurrencyCode := '';
                end;
 
                GenJournalLine.Init();
                GenJournalLine.Validate("Journal Template Name", JournalTemplateName);
                GenJournalLine.Validate("Journal Batch Name", JournalBatchName);
                GenJournalLine.Validate("Line No.", LineNo);
                GenJournalLine.Validate("Document No.", DocumentNo);
                GenJournalLine.Validate("Document Type", DocumentType);
                GenJournalLine.Validate("Currency Code", CurrencyCode);
 
                // التحقق من أن PostingDate ليس فارغًا
                if not LineObject.Get('PostingDate', JsonToken) then
                    Error('PostingDate is missing at line %1', i + 1);
                GenJournalLine.Validate("Posting Date", JsonToken.AsValue().AsDate());
                if GenJournalLine."Posting Date" = 0D then
                    Error('PostingDate cannot be empty at line %1', i + 1);
 
                // التحقق من أن ExternalDocumentNo ليس فارغًا
                if not LineObject.Get('ExternalDocumentNo', JsonToken) then
                    Error('ExternalDocumentNo is missing at line %1', i + 1);
                GenJournalLine.Validate("External Document No.", JsonToken.AsValue().AsText());
 
                // التحقق من أن AccountType ليس فارغًا
                if not LineObject.Get('AccountType', JsonToken) then
                    Error('AccountType is missing at line %1', i + 1);
                AccountTypeText := JsonToken.AsValue().AsText();
 
                case AccountTypeText of
                    'G/L Account':
                        AccountType := AccountType::"G/L Account";
                    'Customer':
                        AccountType := AccountType::Customer;
                    'Vendor':
                        AccountType := AccountType::Vendor;
                    else
                        Error('Invalid AccountType at line %1', i + 1);
                end;
 
                GenJournalLine.Validate("Account Type", AccountType);
 
                // التحقق من أن AccountNo ليس فارغًا
                if not LineObject.Get('AccountNo', JsonToken) then
                    Error('AccountNo is missing at line %1', i + 1);
                GenJournalLine.Validate("Account No.", JsonToken.AsValue().AsText());
                if GenJournalLine."Account No." = '' then
                    Error('AccountNo cannot be empty at line %1', i + 1);
 
                // التحقق من أن Description ليس فارغًا
                if not LineObject.Get('Description', JsonToken) then
                    Error('Description is missing at line %1', i + 1);
                GenJournalLine.Validate(Description, JsonToken.AsValue().AsText());
 
                // التحقق من أن Debit و Credit ليسا فارغين
                if LineObject.Get('Debit', JsonToken) then
                    DebitAmount := JsonToken.AsValue().AsDecimal()
                else
                    DebitAmount := 0.0;
 
                if LineObject.Get('Credit', JsonToken) then
                    CreditAmount := JsonToken.AsValue().AsDecimal()
                else
                    CreditAmount := 0.0;
 
                // التحقق من أن Amount LCY ليس فارغًا
                if not LineObject.Get('Amount LCY', JsonToken) then
                    Error('Amount LCY is missing at line %1', i + 1);
                AmountLCY := JsonToken.AsValue().AsDecimal();
                if AmountLCY = 0.0 then
                    Error('Amount LCY cannot be zero at line %1', i + 1);
 
                GenJournalLine.Validate("Debit Amount", DebitAmount);
                GenJournalLine.Validate("Credit Amount", CreditAmount);
                GenJournalLine.Validate("Amount (LCY)", AmountLCY);
 
                // التحقق من الأبعاد (Dimensions)
                Clear(TempDimSetEntry);
                if LineObject.Get('Dimensions', JsonToken) then begin
                    DimensionArray := JsonToken.AsArray();
 
                    for j := 0 to DimensionArray.Count() - 1 do begin
                        DimensionArray.Get(j, JsonToken);
                        DimensionObject := JsonToken.AsObject();
 
                        if not DimensionObject.Get('DimensionCode', JsonToken) then
                            Error('DimensionCode is missing at line %1', i + 1);
                        DimCode := JsonToken.AsValue().AsText();
 
                        if not DimensionObject.Get('DimensionValue', JsonToken) then
                            Error('DimensionValue is missing at line %1', i + 1);
                        DimValueCode := JsonToken.AsValue().AsText();
 
                        if not TempDimSetEntry.Get(0, DimCode) then begin
                            TempDimSetEntry.Init();
                            TempDimSetEntry.Validate("Dimension Code", DimCode);
                            TempDimSetEntry.Validate("Dimension Value Code", DimValueCode);
                            TempDimSetEntry.Insert();
                        end;
                    end;
 
                    DimensionSetID := DimMgt.GetDimensionSetID(TempDimSetEntry);
                    GenJournalLine.Validate("Dimension Set ID", DimensionSetID);
                end;
 
                GenJournalLine.Insert();
            end;
 
            Message('Data imported successfully.');
        end else
            Error('Invalid JSON structure. Lines array is missing.');
    end;
 
    local procedure GetLastLineNo(JournalTemplateName: Code[10]; JournalBatchName: Code[10]): Integer
    var
        GenJournalLine: Record "Gen. Journal Line";
    begin
        GenJournalLine.SetRange("Journal Template Name", JournalTemplateName);
        GenJournalLine.SetRange("Journal Batch Name", JournalBatchName);
        if GenJournalLine.FindLast() then
            exit(GenJournalLine."Line No.")
        else
            exit(0);
    end;
}
 
 
 
I have the same question (0)
  • CU09091051-0 Profile Picture
    322 on at
    Another very strange problem
     
    This part of the following code does not work.
     
    When I send the value "DocumentType": "Payment", I get the screen "Invoice" ؟؟؟؟؟؟؟؟؟
     
     if not LineObject.Get('AccountType', JsonToken) then
                        Error('AccountType is missing at line %1', i + 1);
                    AccountTypeText := JsonToken.AsValue().AsText();
     
                    case AccountTypeText of
                        'G/L Account':
                            AccountType := AccountType::"G/L Account";
                        'Customer':
                            AccountType := AccountType::Customer;
                        'Vendor':
                            AccountType := AccountType::Vendor;
                        else
                            Error('Invalid AccountType at line %1', i + 1);
                    end;
     
                    GenJournalLine.Validate("Account Type", AccountType);
     
     
    And when I send "Invoice" it arrives "Payment"
     
    why ???????????
     
     
     
     
     
     
     
     
     
     
  • Suggested answer
    Nitin Verma Profile Picture
    21,708 Moderator on at
    You can try this code :
     
    Here LCYCurrencyCode you can take code 10 var, and CurrencyCodeTxt is your currency code value.
    GraphMgtGeneralTools: Codeunit "Graph Mgt - General Tools";
    Rec."Currency Code" := GraphMgtGeneralTools.TranslateCurrencyCodeToNAVCurrencyCode(LCYCurrencyCode, CopyStr(CurrencyCodeTxt, 1, MaxStrLen(LCYCurrencyCode)));
     
  • Verified answer
    YUN ZHU Profile Picture
    95,763 Super User 2025 Season 2 on at
    First question: Is there code to clear or re-obtain currency in the validate trigger of other fields?
    For the second question, is the value passed incorrectly?
     
    Hope this can give you some hints.
    Thanks.
    ZHU
  • Khushbu Rajvi. Profile Picture
    20,971 Super User 2025 Season 2 on at
    confirm that the value exists in the "Currencies" table.
     
    if not Currency.Get(CurrencyCode) then
    Error('Invalid Currency Code: %1', CurrencyCode);
     
    Add Message statements before and after GenJournalLine.Validate("Currency Code", CurrencyCode); to confirm that the value is being assigned correctly.
    Message('Currency Code being inserted: %1', CurrencyCode);

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,116

#2
Khushbu Rajvi. Profile Picture

Khushbu Rajvi. 764 Super User 2025 Season 2

#3
YUN ZHU Profile Picture

YUN ZHU 635 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans