Exporting AX Data to .csv File Through Class (Dialog)






CLASS DECLARATION:

class CustomerDataExtension_RunBase extends RunBaseBatch
{
    CustTable   custTable;
    FilePath    TempPath;
    Dialog      dialog;
    DialogField dialogFieldFileSave;
    FilenameOpen filename;
    CommaTextIo file;
    container line;

  

   #File    #DEFINE.CurrentVersion(1)
    #LOCALMACRO.CurrentList
        TempPath
    #ENDMACRO

}

DIALOG:
protected Object Dialog()
{
    dialog = super();
    dialogFieldFileSave     = dialog.addField(extendedTypeStr(FilenameSave),"File path","Help text goes here");
    dialog.caption("Customer Data Extension");
    dialog.filenameLookupFilter(['csv','*.csv']);
    return dialog;
}

GET FROM DIALOG:

public boolean getFromDialog()
{
    TempPath = dialogFieldFileSave.value();
   
    return super();
}


RUN:
public void run()
{

    if (TempPath == ''")
    {
        error("Please set up the location for export file.");
        return;
    }
   
  
  
    file = new CommaTextIo(#filename, #io_write);

    if (!file || file.status() != IO_Status::Ok)
    {
        throw error("File cannot be opened.");
    }
   
    line=["AccountNumber"];
    file.outFieldDelimiter(",");
    file.writeExp(line);

    while select AccountNum from custTable
    {
            line = [custTable.AccountNum];
            file.outFieldDelimiter("\t");
            file.writeExp(line);
    }

    //WinAPI::shellExecute(filename);
}


MAIN:

static void main(Args _args)
{
    CustomerDataExtension_RunBase customerDataExtension = new CustomerDataExtension_RunBase();


    if (customerDataExtension.prompt())
    {
       customerDataExtension.run();
    }
}

container pack()
{
    return [#CurrentVersion,#CurrentList];
}

public boolean unpack(container _packedClass)
{
    int version = conPeek(_packedClass,1);
   
    switch (version)   
   {       
        case #CurrentVersion:
            [version,#CurrentList] = _packedClass;
            break;
        default:
            return false;
    }
    return true;
}


Comments

Popular posts from this blog

How to add select button for query form on a class

Export Model Store (AXUtil) - ax 2012