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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

How can I write the results from a QueryRun object to a CSV file?

(0) ShareShare
ReportReport
Posted on by 811

Hi,

after executing a queryrun object I want to loop through the different datasources and per row of a datasource write a record to a CSV file (one cell per field - looping through the fields?).

Is this possible, can anyone supply me with some code examples?

In the end the result should look a bit like this:

Account Number;Account Name;

0001;Sven

0002;Pieter

...

Thanks for any help.

Sven

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    239,932 Most Valuable Professional on at

    Hi Sven, you can write to text file using the TextIo class:

    #File
    TextIo textIo = new TextIO("xyz123.txt", #IO_Write);
    ;
    textIo.outFieldDelimiter(';');
    while (queryRun.next())
    {
        //... get data
        textIo.write(yourTable.AccountNumber, yourTable.AccountName);
    }

    If you want to implement a generic solution, you can iterate through datasources/fields like this:

    setPrefix("List query fields");
    for (i = 1; i <= query.dataSourceCount(); i++)
    {
        qbds = query.dataSourceNo(i);
        setPrefix(tableId2name(qbds.table()));
            
        for (j = 1; j <= qbds.fields().fieldCount(); j++)
        {
            fieldId = qbds.fields().field(j);
            info(new SysDictField(qbds.table(), fieldId).name());
        }
    }

    When you have TableId and FieldId, you can write data by the following code:

    common = queryRun.get(tableId);
    textIo.write(common.(fieldId));

  • Martin Dráb Profile Picture
    239,932 Most Valuable Professional on at

    I forgot to mention: My code above doesn't contain any escaping of special characters, which would be needed in a real solution.

    Also, TextIo constructor's third parameter allows to set a code page, which is often needed.

  • Sven Joos Profile Picture
    811 on at

    Thx Martin,

    that helped a lot.

    I have a related question.

    Everytime I use the method "write" a new line is written.

    Is it not possible to continue writing on the same line?

    THanks,

    Sven

  • Martin Dráb Profile Picture
    239,932 Most Valuable Professional on at

    You can build a container and write it down by writeExp() when it is complete.

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Sanhthosh.Kumar.K Profile Picture

Sanhthosh.Kumar.K 2

#2
Raed Salah Bzour Profile Picture

Raed Salah Bzour 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans