Search code examples
importdynamics-business-central

Business Central - xmlport import csv


I have a CSV file as below and I'm trying and failing to import it into BC table: "Gen. Journal Line" (81) using an XmlPort.

"GENERAL","DEFAULT","TLPUK0099","","","95.460","01/01/2018"
"GENERAL","DEFAULT","TLPUK0099","Revalued Source","","-5.370","31/01/2018"
"GENERAL","DEFAULT","TLPUK0099","Revalued Source","REVERSING ENTRY","5.370","01/02/2018"
"GENERAL","DEFAULT","TLPUK0099","Revalued Source","","-8.500","28/02/2018"
"GENERAL","DEFAULT","TLPUK0099","Revalued Source","REVERSING ENTRY","8.500","01/03/2018"
"GENERAL","DEFAULT","TLPUK0099","Revalued Source","","-5.760","10/10/2019"
"GENERAL","DEFAULT","TLPUK0099","Revalued Source","","-5.760","10/10/2019"
"GENERAL","DEFAULT","TLPUK0099","Revalued Source","REVERSING ENTRY","5.760","01/05/2019"
"GENERAL","DEFAULT","TLPUK0099","Revalued Source","REVERSING ENTRY","5.760","01/05/2019"

Here's my XmlPort:-

xmlport 50000 XmlPortGenJrnl
{
    Format = FixedText;
    Direction = Import;
    TextEncoding = UTF8;
    UseRequestPage = false;
    TableSeparator = '';

    schema
    {
        textelement(Root)
        {
            tableelement(GenJournalLine; "Gen. Journal Line")
            {
                XmlName = 'GenJnlLine';
                fieldelement(JournalTemplateName; GenJournalLine."Journal Template Name") { }
                fieldelement(JournalBatchName; GenJournalLine."Journal Batch Name") { }
                fieldelement(AccNum; GenJournalLine."Account No.") { }
                fieldelement(Description; GenJournalLine.Description) { }
                fieldelement(DocumentNo; GenJournalLine."Document No.") { }
                fieldelement(Amount; GenJournalLine.Amount) { }
                fieldelement(PostingDate; GenJournalLine."Posting Date") { }
            }
        }
    }

    var
        myInt: Integer;
}

When I call XmlPort.Run(50000, false, true); in a Page extension action, I get error: Posting Date must have a value in Gen. Journal Line: Journal Template Name=, Journal Batch Name=, Line No.=0. It cannot be zero or empty.

Where am I going wrong? Posting Date does have a value. Any help would be greatly appreciated.


Solution

  • You must change Format = FixedText to Format = VariableText. FixedText indicates that all lines have exactly the same length, which your lines do not.

    Additionally you might need to tell it how to split the lines by setting the FieldSeparator and FieldDelimeter:

    FieldDelimeter = '"';
    FieldSeparator = ",";