Header Ads Widget

Seamless Import of CSV Data into D365FO Using X++ | Export-Ready Structure for Excel/Text Files

 

💡 Introduction

Managing bulk data efficiently is crucial in Dynamics 365 Finance and Operations (D365FO). Whether you're onboarding new inventory, updating customer records, or syncing external configurations, importing data from CSV files is often the go-to method.

In this guide, we’ll explore how to read and import CSV data using X++ in D365FO, showcasing clean, reusable code with fresh buffer names and practices suited for today's environment.


📥 1. Importing CSV File Into a Custom Table (Customer Data Example)

x++
static void D365FO_ReadCsvAndInsertData(Args _args) { #File FilenameOpen importFilePath = @"D:\CustomerData.csv"; CommaTextIo csvReader; container csvRecord; boolean isFirstLine = true; // Custom buffer for target table D365CustomerImportTable customerBuffer; CustAccount customerId; CustGroupId groupId; csvReader = new CommaTextIo(importFilePath, #IO_Read); if (!csvReader || csvReader.status() != IO_Status::Ok) { throw error("@SYS19358"); } while (csvReader.status() == IO_Status::Ok) { csvRecord = csvReader.read(); if (csvRecord) { if (isFirstLine) { isFirstLine = false; // Skip header row } else { customerId = conPeek(csvRecord, 1); groupId = conPeek(csvRecord, 2); customerBuffer.clear(); customerBuffer.CustomerId = customerId; customerBuffer.CustomerGroup = groupId; customerBuffer.insert(); } } } info("Customer import completed successfully."); }

🔄 2. Updating InventTable Using CSV Data (Channel, Season & Category)

x++
static void D365FO_UpdateItemAttributesFromCsv(Args _args) { #File str importPath = @"C:\TEMP\"; CommaTextIo csvImporter; FilenameOpen selectedFile; container lineData; FileNameFilter fileFilter = ["CSV files", "*.csv"]; boolean skipHeader = true; // Fields for import ItemId itemCode; HSOChannelCode channel; HSOAPSeasonCodeId season; HSOAPItemCategoryId category; DataAreaId legalEntity = "USMF"; // Buffers InventTable itemBuffer; int successCount, failCount; boolean isValidLine; selectedFile = WinAPI::getOpenFileName( infolog.hWnd(), fileFilter, importPath, "Select CSV File to Update Item Attributes", "csv", "", 1); if (!selectedFile) return; csvImporter = new CommaTextIo(selectedFile, #IO_Read); csvImporter.inFieldDelimiter(";"); if (!csvImporter || csvImporter.status() != IO_Status::Ok) { throw error("@SYS19358"); } while (csvImporter.status() == IO_Status::Ok) { lineData = csvImporter.read(); if (lineData) { if (skipHeader) { skipHeader = false; } else { isValidLine = true; itemCode = strLRTrim(conPeek(lineData, 1)); season = strLRTrim(conPeek(lineData, 2)); category = strLRTrim(conPeek(lineData, 3)); channel = strLRTrim(conPeek(lineData, 4)); ttsBegin; select forUpdate itemBuffer where itemBuffer.ItemId == itemCode && itemBuffer.DataAreaId == legalEntity; if (!itemBuffer) { isValidLine = checkFailed(strFmt("Item '%1' not found in InventTable.", itemCode)); } if (isValidLine) { itemBuffer.HSOAPSeasonCodeId = season; itemBuffer.HSOAPItemCategoryId = category; itemBuffer.HSOChannelCode = channel; itemBuffer.doUpdate(); successCount++; } else { failCount++; } ttsCommit; } } } info(strFmt("Item update completed: %1 updated, %2 errors.", successCount, failCount)); }

✅ Summary

  • This article walks you through importing and updating data in D365FO using CommaTextIo.

  • It includes clean X++ examples with new buffer naming conventions to avoid reusability concerns and improve clarity.

  • We've modernized the process to reflect D365FO standards while preserving AX 2012 functionality for backward reference.

D365FO Data Import, X++ CSV Reader, Import Text File D365, CommaTextIo in D365, Excel and CSV X++, AX 2012 File Handling, X++ Inventory Update, Data Import through Code, Custom Table Insert X++, AX to D365FO Migration

Post a Comment

0 Comments