Showing posts with label TextIO. Show all posts
Showing posts with label TextIO. Show all posts

Monday, March 7, 2011

What's up with that 'Error executing code - AsciiIO object not initialized'?

Picture this:
You are working on a piece of X++ code, where creating a text file is involved.  Maybe you use the AsciiIO class, or maybe you use the TextIO class, the newer installment.  Maybe CommaIO/CommaTextIO is the class you use for creating comma-separated files with Ax.
Your code looks fine (of course it does :-) ).  But still you get this nasty error. 
You have set your permissions, you have initialized the file, set it ready for writing etc.  But still you keep getting this error, even though the code worked fine with other projects.

Error executing code - AsciiIO object not initialized

Tip: You might wanna take a look at the layer the code is executed, server or client.
The code might need to run on the client tier, as otherwise you will get the error message above.  So check you keywords in the declaration, the RunOn-property of the menu-item.
File handling on the server requires some precautions, take a look at the WinAPIServer class.

A short example on how to create a text file with Ax:

static void TextFileDemo(Args _args)
{   
    AsciiIO             tmpfile;
    Filename            tmpfilename;
    FileIOPermission    permission;
    #File
    ;
    tmpfilename = WinApi::getTempFilename(WinAPI::getTempPath(),'DEMO');

    permission = new FileIOPermission(tmpfilename,#IO_write);
    permission.assert();

    tmpfile = new AsciiIO(tmpfilename, #IO_write);

    if(tmpfile.status()==IO_Status::Ok)
    {
        tmpfile.outRecordDelimiter('\n');
        tmpfile.outFieldDelimiter(';');
    } else
    {
        return;
    }

    tmpfile.writeExp(['column1','column2','column3']);
    tmpfile = null;
    CodeAccessPermission::revertAssert();
}