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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

D365FO Import bank reconcilation using CSV file - x++

(2) ShareShare
ReportReport
Posted on by 275
Hello guys,
 
i currently developed a custom solution that imports csv file of type xlsx (Excel) to the system, below is 1st screenshot of the excel sheet. its a static sheet that has company logo from column 1 till 7. with all the fields that needs to be imported to the system.
 
on the bank statement form , in the action pane, i added the button that opens a dialog to add the excel sheet.
 
 
for the below class, it contains the update bank reconcilation and create bank statement method. what is happening is that the excel is not being imported as its taking alot of time and at the end its failing, even that the excel has only 1 record, its taking forever. i debugged the code and found out that fileUploadResult is coming null, so its exiting to the error message "file error upload" and keeps going in infinit loop.
if anyone can provide me solution in order to resolve it. the main purpose of this functionality is to import bank reconcilation using static excel sheet.
 
 
 
using System.IO;

using OfficeOpenXml;

using OfficeOpenXml.ExcelPackage;

using OfficeOpenXml.ExcelRange;

class ImportBankReconcilation
{
    
        public void run()
    {
        this.updateBankReconcilation();
    }

    void updateBankReconcilation()
    {
        System.IO.Stream stream;
        FileUploadBuild fileUpload;
        DialogGroup dlgUploadGroup;
        FileUploadBuild fileUploadBuild;
        FormBuildControl formBuildControl;
        BankReconcilation bankReconcilationRecord, insertBankReconcilation ;

        str UserName , CompanyName , AccountName , ShortName , Filters , DateOrder , TotalCreditTransactions ;
        str TotalDebitAmount , AccountNumber , TotalCreditAmount , ProcessingTime , CreditDebit , ResultsPerPage;
        str DateRange , TransactionType , TotalDebitTransactions , TransactionDescription ;
        str Description,DescriptionExtra,Reference,Remarks,Debit,Credit,Balance;

        str DateTo , DateFrom , FieldDate;
        utcDateTime DateTime;

        Dialog dialog = new Dialog("Bank Reconciliation Data Import");

        dlgUploadGroup = dialog.addGroup("@SYS54759");
        formBuildControl = dialog.formBuildDesign().control(dlgUploadGroup.name());
        fileUploadBuild = formBuildControl.addControlEx(classstr(FileUpload), "Upload");
        fileUploadBuild.style(FileUploadStyle::MinimalWithFilename);
        fileUploadBuild.fileTypesAccepted(".xlsx");

        if (dialog.run() && dialog.closedOk())
        {
            FileUpload fileUploadControl = dialog.formRun().control(dialog.formRun().controlId("Upload"));
            FileUploadTemporaryStorageResult fileUploadResult = fileUploadControl.getFileUploadResult();

            if (fileUploadResult != null && fileUploadResult.getUploadStatus())
            {
                stream = fileUploadResult.openResult();

                using (ExcelPackage package = new ExcelPackage(stream))
                {
                    int rowCount, i, j , columnCount;
                    package.Load(stream);
                    ExcelWorksheet   worksheet   = package.get_Workbook().get_Worksheets().get_Item(1);  // Assumes data is in the first sheet
                    OfficeOpenXml.ExcelRange range = worksheet.Cells;
                    rowCount = worksheet.Dimension.End.Row;
                    columnCount = worksheet.Dimension.End.Column;

                    delete_from bankReconcilationRecord;

                   
                    for (i =9; i <= rowCount; i++)  // i=9
                    {
                        

                        if(i == 9)
                        {
                            UserName = (range.get_Item(i, 2).value);
                            insertBankReconcilation.UserName = UserName;
                        }
                        if(i == 10)
                        {
                            DateTime = (range.get_Item(i, 2).value);
                            insertBankReconcilation.DateTime = DateTime;
                        }
                        if(i == 11)
                        {
                            CompanyName = (range.get_Item(i, 2).value);
                            insertBankReconcilation.CompanyName = CompanyName;
                        }
                        if(i == 12)
                        {
                            AccountName = (range.get_Item(i, 2).value);
                            insertBankReconcilation.AccountName = AccountName;
                        }
                        if(i == 13)
                        {
                            ShortName = (range.get_Item(i, 2).value);
                            insertBankReconcilation.ShortName = ShortName;
                        }
                        if(i == 15)
                        {
                            DateOrder = (range.get_Item(i, 2).value);
                            insertBankReconcilation.DateOrder = DateOrder;
                        }
                        if(i == 16)
                        {
                            TotalCreditTransactions = (range.get_Item(i, 2).value);
                            insertBankReconcilation.TotalCreditTransactions = TotalCreditTransactions;
                        }
                        if(i == 18)
                        {
                            TotalDebitAmount = (range.get_Item(i, 2).value);
                            insertBankReconcilation.TotalDebitAmount = TotalDebitAmount;
                        }
                        if(i == 19)
                        {
                            AccountNumber = (range.get_Item(i, 2).value);
                            insertBankReconcilation.AccountNumber = AccountNumber;
                        }
                        if(i == 20)
                        {
                            TotalCreditAmount = (range.get_Item(i, 2).value);
                            insertBankReconcilation.TotalCreditAmount = TotalCreditAmount;
                        }
                        if(i == 21)
                        {
                            ProcessingTime = (range.get_Item(i, 2).value);
                            insertBankReconcilation.ProcessingTime = ProcessingTime;
                        }
                        if(i == 22)
                        {
                            CreditDebit = (range.get_Item(i, 2).value);
                            insertBankReconcilation.CreditDebit = CreditDebit;
                        }
                        if(i == 23)
                        {
                            ResultsPerPage = (range.get_Item(i, 2).value);
                            insertBankReconcilation.ResultsPerPage = ResultsPerPage;
                        }
                        if(i == 24)
                        {
                            DateTo = (range.get_Item(i, 2).value);
                            insertBankReconcilation.DateTo = str2Date(DateTo,DateSeparator::Slash);
                        }
                        if(i == 25)
                        {
                            DateFrom = (range.get_Item(i, 2).value);
                            insertBankReconcilation.DateFrom = str2Date(DateFrom,DateSeparator::Slash);
                        }
                        if(i == 26)
                        {
                            DateRange = (range.get_Item(i, 2).value);
                            insertBankReconcilation.DateRange = DateRange;
                        }
                        if(i == 27)
                        {
                            TransactionType = (range.get_Item(i, 2).value);
                            insertBankReconcilation.TransactionType = TransactionType;
                        }
                        if(i == 28)
                        {
                            TotalDebitTransactions = (range.get_Item(i, 2).value);
                            insertBankReconcilation.TotalDebitTransactions = TotalDebitTransactions;
                        }
                        if(i == 31)
                        {
                            for(j = 32 ; j <= 87 ; j++)
                            {
                            
                                FieldDate = (range.get_Item(i, j).value);
                                TransactionDescription = (range.get_Item(i, j).value);
                                Description = (range.get_Item(i, j).value);
                                DescriptionExtra = (range.get_Item(i, j).value);
                                // Reference = (range.get_Item(i, 4).value);
                                Reference = (range.get_Item(i, j).value);
                                Remarks = (range.get_Item(i, j).value);
                                Debit = (range.get_Item(i, j).value);
                                Credit = (range.get_Item(i, j).value);
                                Balance = (range.get_Item(i, j).value);


                                insertBankReconcilation.FieldDate = str2Date(FieldDate,DateSeparator::Slash);
                                insertBankReconcilation.TransactionDescription = TransactionDescription;
                                insertBankReconcilation.Description = Description;
                                insertBankReconcilation.DescriptionExtra = DescriptionExtra;
                                insertBankReconcilation.Reference = Reference;
                                insertBankReconcilation.Remarks = Remarks;
                                insertBankReconcilation.Debit = Debit;
                                insertBankReconcilation.Credit = Credit;
                                insertBankReconcilation.Balance = Balance;

                                ttsbegin;
                                insertBankReconcilation.insert();
                                ttscommit;
                            }                            
                        }   
                    }   
                }
            }
            else
            {
                error("File upload error.");
            }
           
       }
        this.createBankStatement();
    }

    public bankAccountTable createBankStatement()
    {
       
        BankStmtISOReportEntry      BankStmtISOReportEntry;
        BankAccountTable            bankAccountTable;
        BankReconcilation           BankReconcilation;
        BankStmtISOCashBalance      BankStmtISOCashBalance;
        BankStmtISOAccountStatement BankStmtISOAccountStatement;

        real EndingBalance;

       
            ttsbegin;
            bankAccountTable.initValue();
            BankStmtISOReportEntry.initValue();
            BankStmtISOCashBalance.initValue();
            BankStmtISOAccountStatement.initValue();

        while select BankReconcilation
        {

            select BankAccountTable where BankAccountTable.Name == BankReconcilation.CompanyName;
            bankAccountTable.AccountNum = BankReconcilation.AccountNumber;
            


            
            BankStmtISOReportEntry.editCreditAmount(true,str2Num(BankReconcilation.Debit)) ;

            BankStmtISOReportEntry.editDebitAmount(true,str2Num(BankReconcilation.Credit)) ;

            BankStmtISOReportEntry.BookingDateTime = BankReconcilation.FieldDate;

            BankStmtISOReportEntry.AdditionalEntryInformation = BankReconcilation.Description;

            BankStmtISOReportEntry.ReferenceNumber = BankReconcilation.Reference;

            EndingBalance = str2Num(BankReconcilation.Balance) + str2Num(BankReconcilation.Debit) - str2Num(BankReconcilation.Credit);
            BankStmtISOCashBalance.editEndingBalance(true , EndingBalance );

            BankStmtISOCashBalance.editOpeningBalance(true , str2Num(BankReconcilation.Balance));

            select RecId from bankStmtISOAccountStatement
            where bankStmtISOAccountStatement.BankAccountTable == bankAccountTable.AccountID;
           
            if (!bankStmtISOAccountStatement.RecId)
            {
                bankStmtISOAccountStatement.BankAccountTable = BankReconcilation.AccountNumber;
            }
            
            //bankAccountTable.insert();
            BankStmtISOReportEntry.insert();
            BankStmtISOCashBalance.insert();
            BankStmtISOAccountStatement.insert();
        }
            ttscommit;
           
        return bankAccountTable;
    }

    public static void main(Args args)
    {
        ImportBankReconcilation ImportBankReconcilation  = new ImportBankReconcilation();
      
        ImportBankReconcilation.run();
    }
}
 
 
 
 
Categories:
I have the same question (0)
  • Martin Dráb Profile Picture
    239,632 Most Valuable Professional on at
    Could you please clarify what you're importing? You statement "imports csv file of type xlsx (Excel)" doesn't make a good sense, because CSV and XLSX are two completely different formats. CSV is a text file with with comma-separated values, while XSLX is a ZIP archive containing a plenty of XML files.

    A bug is that you call createBankStatement() even if the upload failed. You should replace error("File upload error."); with throw error("File upload error.");.
  • ISDev Profile Picture
    275 on at
     
    Thanks for replying, im importing bank statements but through Excel sheet. but as i said, its not working, it going through infinit loop and at the end it gives the error message "file upload error". i have also added throw before the error message as u mentioned.
    im facing challenges with fixing this issue.
    appreciate if u could help me.
    thanks
  • Suggested answer
    Mohamed Amine Mahmoudi Profile Picture
    26,803 Super User 2026 Season 1 on at
    Hi @ISDev,
     
    It's normal that you get infinit loop error message since you haven't incremented varaibals i and j.
     
    You need to add i++ et j++ in your loops.
     
    Best regards,
    Mohamed Amine MAHMOUDI
  • ISDev Profile Picture
    275 on at
    can you update the part in the code that is wrong and share it with me?
  • Suggested answer
    Mohamed Amine Mahmoudi Profile Picture
    26,803 Super User 2026 Season 1 on at
    Hi @ISDev,
     
    for (i =9; i <= rowCount; i++)  // i=9
                        {
                            
                            if(i == 9)
                            {
                                UserName = (range.get_Item(i, 2).value);
                                insertBankReconcilation.UserName = UserName;
                            }
                            if(i == 10)
                            {
                                DateTime = (range.get_Item(i, 2).value);
                                insertBankReconcilation.DateTime = DateTime;
                            }
                            if(i == 11)
                            {
                                CompanyName = (range.get_Item(i, 2).value);
                                insertBankReconcilation.CompanyName = CompanyName;
                            }
                            if(i == 12)
                            {
                                AccountName = (range.get_Item(i, 2).value);
                                insertBankReconcilation.AccountName = AccountName;
                            }
                            if(i == 13)
                            {
                                ShortName = (range.get_Item(i, 2).value);
                                insertBankReconcilation.ShortName = ShortName;
                            }
                            if(i == 15)
                            {
                                DateOrder = (range.get_Item(i, 2).value);
                                insertBankReconcilation.DateOrder = DateOrder;
                            }
                            if(i == 16)
                            {
                                TotalCreditTransactions = (range.get_Item(i, 2).value);
                                insertBankReconcilation.TotalCreditTransactions = TotalCreditTransactions;
                            }
                            if(i == 18)
                            {
                                TotalDebitAmount = (range.get_Item(i, 2).value);
                                insertBankReconcilation.TotalDebitAmount = TotalDebitAmount;
                            }
                            if(i == 19)
                            {
                                AccountNumber = (range.get_Item(i, 2).value);
                                insertBankReconcilation.AccountNumber = AccountNumber;
                            }
                            if(i == 20)
                            {
                                TotalCreditAmount = (range.get_Item(i, 2).value);
                                insertBankReconcilation.TotalCreditAmount = TotalCreditAmount;
                            }
                            if(i == 21)
                            {
                                ProcessingTime = (range.get_Item(i, 2).value);
                                insertBankReconcilation.ProcessingTime = ProcessingTime;
                            }
                            if(i == 22)
                            {
                                CreditDebit = (range.get_Item(i, 2).value);
                                insertBankReconcilation.CreditDebit = CreditDebit;
                            }
                            if(i == 23)
                            {
                                ResultsPerPage = (range.get_Item(i, 2).value);
                                insertBankReconcilation.ResultsPerPage = ResultsPerPage;
                            }
                            if(i == 24)
                            {
                                DateTo = (range.get_Item(i, 2).value);
                                insertBankReconcilation.DateTo = str2Date(DateTo,DateSeparator::Slash);
                            }
                            if(i == 25)
                            {
                                DateFrom = (range.get_Item(i, 2).value);
                                insertBankReconcilation.DateFrom = str2Date(DateFrom,DateSeparator::Slash);
                            }
                            if(i == 26)
                            {
                                DateRange = (range.get_Item(i, 2).value);
                                insertBankReconcilation.DateRange = DateRange;
                            }
                            if(i == 27)
                            {
                                TransactionType = (range.get_Item(i, 2).value);
                                insertBankReconcilation.TransactionType = TransactionType;
                            }
                            if(i == 28)
                            {
                                TotalDebitTransactions = (range.get_Item(i, 2).value);
                                insertBankReconcilation.TotalDebitTransactions = TotalDebitTransactions;
                            }
                            if(i == 31)
                            {
                                for(j = 32 ; j <= 87 ; j++)
                                {
                                
                                    FieldDate = (range.get_Item(i, j).value);
                                    TransactionDescription = (range.get_Item(i, j).value);
                                    Description = (range.get_Item(i, j).value);
                                    DescriptionExtra = (range.get_Item(i, j).value);
                                    // Reference = (range.get_Item(i, 4).value);
                                    Reference = (range.get_Item(i, j).value);
                                    Remarks = (range.get_Item(i, j).value);
                                    Debit = (range.get_Item(i, j).value);
                                    Credit = (range.get_Item(i, j).value);
                                    Balance = (range.get_Item(i, j).value);

                                    insertBankReconcilation.FieldDate = str2Date(FieldDate,DateSeparator::Slash);
                                    insertBankReconcilation.TransactionDescription = TransactionDescription;
                                    insertBankReconcilation.Description = Description;
                                    insertBankReconcilation.DescriptionExtra = DescriptionExtra;
                                    insertBankReconcilation.Reference = Reference;
                                    insertBankReconcilation.Remarks = Remarks;
                                    insertBankReconcilation.Debit = Debit;
                                    insertBankReconcilation.Credit = Credit;
                                    insertBankReconcilation.Balance = Balance;
                                    ttsbegin;
                                    insertBankReconcilation.insert();
                                    ttscommit;
    j++;
                                }                            
                            }  
     i++;
                        }   
     
    Best regards,
    Mohamed Amine MAHMOUDI
  • Martin Dráb Profile Picture
    239,632 Most Valuable Professional on at
    If fileUploadResult is null, the for loops will never be reached (because of the if condition).
  • ISDev Profile Picture
    275 on at
    Hello,
     
    the issue is lets say the excel sheet has 2 records only, its going thru an infinit loop, even with only 2 records,
     
     
    also this part of the code is always coming blank
     
    for(j = 32 ; j <= 87 ; j++)
    {
        FieldDate = (range.get_Item(i, j).value);
        TransactionDescription = (range.get_Item(i, j).value);
        Description = (range.get_Item(i, j).value);
        DescriptionExtra = (range.get_Item(i, j).value);
        // Reference = (range.get_Item(i, 4).value);
        Reference = (range.get_Item(i, j).value);
        Remarks = (range.get_Item(i, j).value);
        Debit = (range.get_Item(i, j).value);
        Credit = (range.get_Item(i, j).value);
        Balance = (range.get_Item(i, j).value);

        insertBankReconcilation.FieldDate = str2Date(FieldDate,DateSeparator::Slash);
        insertBankReconcilation.TransactionDescription = TransactionDescription;
        insertBankReconcilation.Description = Description;
        insertBankReconcilation.DescriptionExtra = DescriptionExtra;
        insertBankReconcilation.Reference = Reference;
        insertBankReconcilation.Remarks = Remarks;
        insertBankReconcilation.Debit = Debit;
        insertBankReconcilation.Credit = Credit;
        insertBankReconcilation.Balance = Balance;
     
     
  • Martin Dráb Profile Picture
    239,632 Most Valuable Professional on at
    As you see, to keep repeating that "its going thru an infinit loop" doesn't allow you to make any progress. You need to find the loop; then you can evaluate the conditions and the actual variable values to understand why the loop doesn't end.
     
    By the way, is your statement "fileUploadResult is coming null" still true? It sounds like it isn't and you forgot to give us the correct information. If so, please do it now.
  • ISDev Profile Picture
    275 on at
     
    for  the fileUploadResult is coming null" , this issue is resolved. my issue now if i have an excel sheet with 2 line records, its taking forever to insert them 
  • Martin Dráb Profile Picture
    239,632 Most Valuable Professional on at
    Okay, please keep us updated about the situation. We can't help you if you start talking about something else without notifying us.
     
    Now use the debugger to find the problematic loop. If you need more help from us, don't forget to show us your current code for the loop. (I'm assuming that you already tried to fix the bugs found by Mohamed, but maybe you haven't done it correctly).

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Giorgio Bonacorsi Profile Picture

Giorgio Bonacorsi 804

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 639 Super User 2026 Season 1

#3
Subra Profile Picture

Subra 528

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans