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 :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Reading multiple headers of CSV file

(0) ShareShare
ReportReport
Posted on by

I have a CSV file with multiple headers.

Please suggest X++ code for reading the incoming file section as expected structure section as depicted in pic below. Which basically is header 2 + header 3 makes the complete header.

pastedimage1598891694074v1.png

Thanks

Mav

I have the same question (0)
  • Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    Skip the first line. Read lines 2 and 3 and combine the values. Then read the rest of the file.

    How the X++ code will look like depends on how you'll import the file. Have you already decided that? For example, could could utilize CommaTextIo class.

  • Mav Profile Picture
    on at

    Yep as it is CSV have decided to use CommaTextIo class.

    Also the file structure can change ,i.e.  columns can increase  for header 2. 

    Will have AX table where i will have all header 2 values, on an event when new header 2 is read in CSV it throws error to user stating that new header 2 found which is not present in Ax table, pls update Ax table & retry file upload.

    Can you please share the code.

  • Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    Does it mean that you've decided to use CommaTextIo but don't know how? Or you know how to use it and you don't need such code? Maybe you have a problem just with a part of the requirement (but you didn't explain which one).

    Also, you mentioned an error, which suggests that you already have some code. Why don't you show it?

    What do you mean that "columns can increase  for header 2"? That the number of columns in the file can increase? Or that some of the columns that are empty in your example may have values? If the latter, it's not really import if you develop a generic solution.

    What do you mean by saying that a table will have "all header 2 value"? And what is "new header 2 found which is not present in Ax table"? What code is throwing the error?

    Please realize that we can't help you much if you don't give us enough information.

  • Mav Profile Picture
    on at

    Thanks for your response.

    1>I have decided to use CommaTexIO because i will be using CSV file format.

    2>Columns can increase meaning is depicted in pic below along with AX Table

    3>Would appreciate if you can share some code to reach the desire objective which is to combine header 2 & 3 & then read the file , validate against ax table "Axheader2 table"  & ask user to add header 2 value in AXheader2 table on an event new value is being read from CSV file which is not present in Axtable for example header2d in pic below.

    pastedimage1598907531067v1.png

  • Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    Unfortunately you didn't answer if you know how to use CommaTextIo nor showed your code, so we still don't know if you need help with that or not.

    Adding two strings together is trivial: "aaa" "bbb". And this is how you can do it for all fields returned from the IO class:

    container line2 = io.read();
    container line3 = io.read();
    container merged;
    
    int i;
    str combinedValue;
    
    for (i = 1; i <= conLen(line2); i  )
    {
    	combinedValue = conpeek(line2, i)   conpeek(line3, i);
    	merged  = combinedValue;
    }

    What you'll do with the error depends - as always - on business requirements. If you want to insert data to "AX Header2 Table", simply do it.

  • Mav Profile Picture
    on at

    Thanks for sharing, sorry if i am still unable to clarify my requirements.

    I dont know how to use CommaTexIO & do not have any code or else i would have shown it the moment you asked.

    My requirement at high level are listed below , let me know if you still need more information & sorry for adding point # 3 which i forgot to add earlier. Please share the code or let me know if you still have any questions.

    1. Read the csv file format of which i have shared multiple times. skip line1 , merge line 1 & line2 like you shared.

    2. While reading CSV, validate header2 against Axheader2table, on an event when a new header2 is found in CSV & not present in AXheader2table throw an error/warning asking user to first have the missing header2 value in Axheader2table.

    3.The program to allow user to enter a network path which the program would continuously monitor &  read CSV files from .

    It would  generate log file for each CSV file it has read so that user is apprised of which CSV has been read & what happened during the read process.

  • Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    1) Here is a simple example:

    CommaTextIO io = new CommaTextIO(filepath, 'r');
    container lineData = io.read();
    
    while (lineData)
    {
    	// ... process data ...
    	lineData = io.read();
    }

    2) What problem do you have with this requirement? Don't you know how to read data from a table, or how to throw and error, or what?
    3) What problem do you have with this requirement? You don't know how to create a dialog, how to create a batch, how to read files, or what?

    Some of your questions may be unrelated to the topic of this thread (Reading multiple headers of CSV file). In such a case, create a new thread (with an appropriate title and tags) and explain your problem in detail there. You can include a link to this thread for extra context.

  • Mav Profile Picture
    on at

    1>How is that code skipping line 1 , merging line2 & line3 & giving desired output as shared in my pic earlier ?

    2>I wanted to know how to read all CSV header 2 data & validate it against all records in AXheader2 table.

    3>I  was checking if someone has already build somethings similar & share the solution, no reason to get worried if you do not have something helpful, i will figure it out & share the code so that in future if someone needs it he/she can get all of it without going through repetitive questions yielding no helpful response.

  • Martin Dráb Profile Picture
    237,965 Most Valuable Professional on at

    1) I explain that earlier. Maybe you ask a developer on your project to help you.

    All right, let me combine it for you...

    CommaTextIO io = new CommaTextIO(filepath, 'r');
    container lineData = io.read();
    
    io.read() // Skipping line 1
    
    container line2 = io.read();
    container line3 = io.read();
    container merged;
    
    for (i = 1; i <= conLen(line2); i  )
    {
    	combinedValue = conpeek(line2, i)   conpeek(line3, i);
    	merged  = combinedValue;
    }
    
    // ... process the header ...
    
    lineData = io.read();
    while (lineData)
    {
    	// ... process data ...
    	lineData = io.read();
    }

    2) Yes, I know, but what problem do you have with it? Does it mean, for example, that you don't know how to use select statements to check data in a table and you'd like get an introduction to this topic?

    3) All right, if you don't want to explain what you need from us, we can skip this point.

  • Mav Profile Picture
    on at

    1> Your Code does not work, check my pic of desired outcome & your code , your code does not meet the desired outcome.

    2>A simple example of how to compare container value & table values would do.

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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 451 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 428 Super User 2025 Season 2

#3
BillurSamdancioglu Profile Picture

BillurSamdancioglu 239 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans