Create/Export Data in CSV file (With Batch) in AX 2012

There are two types of Class shows how to create/export data in CSV file in AX 2012 (With Batch).


1. Creat Class and create a following method under Class:

A. Declare variable in ClassDeclaration.

class ACX_GoldRatt extends RunBaseBatch
{
    System.IO.StreamWriter                  sw;
    InteropPermission                            perm;
    Filename                                          fileName;
    FilePath                                            filePath;

    str 200                                               timeStamp;
    InventLocationId                              warehouse;
    Name                                                warehouseName;

    InventLocation                                  inventLocation;
}

--------------------------------------------------------------------------------------------------------------------------
B. Create  a  method called stockLocation or anything as you want.

public Filename stockLocation()
{
    ;

    perm            = new InteropPermission(InteropKind::ClrInterop);
    timeStamp       = System.String::Format("{0:ddMMMyyyy}", ACX_Base::getServerDateTime());

    filePath    = @"filePath";

    perm.assert();

    fileName  = (filePath) + 'fileName' + timeStamp + '.csv';

    sw = new System.IO.StreamWriter(fileName);


    string =   'Stock Location Name'        + ','
            +  'Stock Location Description'
            ;

    sw.WriteLine(string);
    string    ='' ;

    while select inventLocation
    {
        warehouse               = inventLocation.InventLocationId;
        warehouseName           = inventLocation.Name;

        string =    strRem(warehouse, "," + "\"" + "\t" + "\r" + "\n" + "\r\n" + "\\" +"\b" + "\f")     +   ',' +
                    strRem(warehouseName, "," + "\"" + "\t" + "\r" + "\n" + "\r\n" + "\\" +"\b" + "\f")
        ;

        sw.WriteLine(string);
        string    = '';
    }

    sw.Flush();
    sw.Close();
    sw.Dispose();
 
    CodeAccessPermission::revertAssert();

    return fileName;
}

--------------------------------------------------------------------------------------------------------------------------
C. Create a run method

public void run()
{
    this.stockLocation();
}

--------------------------------------------------------------------------------------------------------------------------
D. Create a main method

public static void main(Args args)
{
    ACX_GoldRatt   goldRatt = new ACX_GoldRatt();

    if (goldRatt.Prompt())
    {
        goldRatt.run();
    }
}
________________________________________________________________________________

2. Creat Class and creat a following method under Class:

A. Declare variable in ClassDeclaration.

class ACX_GoldRatt extends RunBaseBatch
{
    System.IO.StreamWriter                  sw;
    InteropPermission                            perm;
    Filename                                          fileName;
    FilePath                                            filePath;

    str 200                                               timeStamp;
    InventLocationId                              warehouse;
    Name                                                warehouseName;

    InventLocation                                  inventLocation;
}

--------------------------------------------------------------------------------------------------------------------------
B. Create  a  method called stockLocation or anything as you want.

public Filename stockLocation()
{
    void setStockLocation()
    {
        warehouse               = inventLocation.InventLocationId;
        warehouseName           = inventLocation.Name;
    }
 
    void printStockLocation()
    {
        string =    strRem(warehouse, "," + "\"" + "\t" + "\r" + "\n" + "\r\n" + "\\" +"\b" + "\f")     +   ',' +
                    strRem(warehouseName, "," + "\"" + "\t" + "\r" + "\n" + "\r\n" + "\\" +"\b" + "\f")
        ;

        sw.WriteLine(string);
        string    = '';

    }

    void stockLocation()
    {
        while select inventLocation
        {
            setStockLocation();
            printStockLocation();
        }
    }

    ;

    perm                = new InteropPermission(InteropKind::ClrInterop);

    timeStamp       = System.String::Format("{0:ddMMMyyyy}", ACX_Base::getServerDateTime());

    filePath    = @"filePath";

    perm.assert();

    fileName  = (filePath) + 'fileName' + timeStamp + '.csv';

    sw = new System.IO.StreamWriter(fileName);


    string =   'Stock Location Name'        + ','
            +  'Stock Location Description'
            ;


    sw.WriteLine(string);
    string    ='' ;

    stockLocation();

    sw.Flush();
    sw.Close();
    sw.Dispose();
 
    CodeAccessPermission::revertAssert();

    return fileName;

}

--------------------------------------------------------------------------------------------------------------------------
C. Create a run method

public void run()
{
    this.stockLocation();
}

--------------------------------------------------------------------------------------------------------------------------
D. Create a main method

public static void main(Args args)
{
    ACX_GoldRatt   goldRatt = new ACX_GoldRatt();

    if (goldRatt.Prompt())
    {
        goldRatt.run();
    }
}

Comments