Dynamics Search Engine

Showing posts with label CSV. Show all posts
Showing posts with label CSV. Show all posts

Monday, June 20, 2016

How to export data in a csv or comma separated file by X++ code in Microsoft Dynamics AX 2012


In Microsoft Dynamics AX 2012 how to export data in a csv or comma separated file by X++ code.

This article explains how to generate a csv or comma separated file and export data from Microsoft Dynamics AX 2012 by X++ code.



Applied on: Dynamics AX 2012 R3 CU9.
Prerequisite: Basic Dynamics AX 2012 programming knowledge.
Target audience: AX programmers.
Assumption: You are familiar with Dynamics AX 2012.



I prepared a x++ job here which will guide you how to generate a csv file with a header and one sample line. If you have one header and multi line, you may keep line in a while.



static void exportDataInCSV(Args _args)

{

    Dialog          dialog  = new Dialog();

    DialogField     dialogFieldFileSave;

    Commaio         file;

    container       line;

    FilePath        TempPath;



    dialogFieldFileSave     = dialog.addField(extendedTypeStr(FilenameSave),"File path","Help text goes here");

    dialog.caption("Caption goes here");

    dialog.filenameLookupFilter(['csv','*.csv']);



    if(!dialog.run())

        return;



    if (dialogFieldFileSave.value() == '')

    {

        error("Please set up the location for export file.");

        return;

    }



    TempPath = dialogFieldFileSave.value() + '.csv';



    #define.filename(TempPath)

    #File





    file = new Commaio(#filename , #io_write);

    if( !file || file.status() != IO_Status::Ok)

    {

        throw error("File Cannot be opened");

    }



    //Put headers

    line = ["CardNum","Amount","IssueDate","ExpiryDate","IssueBy","CustAccount","Description"];

    file.writeExp(line);



    //Put a sample line

    line = ["C_476786","100.00","05/26/2015","03/25/2016","John Smith S","Cust_0000034","Description"];

    file.writeExp(line);



    info(strFmt("File exported successfully. Please find it at %1",TempPath));

}





Hope this was useful. You may leave your comment below.








Tuesday, January 6, 2015

How to read and insert record from a CSV file using X++ code into Microsoft Dynamics AX 2012 table

How to read and insert record from a CSV file using X++ code into Microsoft Dynamics AX 2012 table.

This article helps you to understand how to read data from a CSV file and insert into Microsoft Dynamics AX 2012 table.

Applied on: Dynamics AX 2012 R3.
Prerequisite
: Basic Dynamics AX 2012 programming knowledge.
Target audience
: AX programmers.
Assumption: You are familiar with Dynamics AX 2012 and CSV file


Below job inserts records from a CSV file to CustGroup table.

static void CustGroupCSVInsert(Args _args)
{
    Dialog          dialog  = new Dialog();
    DialogField     dialogField;
    AsciiIo         importFile;
    str             filePath,fileNameOnly;
    filetype        type;
    container       record;
    str             delimiter = ",";
    int             totalRecords;
    CustGroup       custGrp;

    dialogField=dialog.addField(extendedTypeStr(FilenameOpen),"Select File","Select file to import");
    dialog.caption("File Picker");
    dialog.filenameLookupFilter(['csv','*.csv']);

    if(!dialog.run())
        return;

    [filePath, fileNameOnly, type] = fileNameSplit(dialogField.value());
    importFile = new AsciiIo(dialogField.value(), 'R');

    if((!importFile) || (importFile.status() != IO_Status::Ok))
    {
        warning("Error in opening import file");
        throw(Exception::Error);
    }

    importFile.inFieldDelimiter(Delimiter);

    try
    {
        ttsbegin;
        custGrp.clear();
        record = importFile.read(); // First row - Column name - Header

        while(importFile.status() ==  IO_Status::Ok)
        {
            record = importFile.read();
            if(!record)
                break;

            totalRecords = totalRecords + 1;
            custGrp.clear();

            custGrp.CustGroup = conPeek(record, 1);
            custGrp.Name = conPeek(record,2);

            custGrp.insert();
        }
        ttscommit;
    }

    catch(Exception::Error)
    {
        Throw(Exception::Error);
    }

    info(strFmt("Total Read Records = %1",totalRecords));
}


Sample .CSV file image.