Showing posts with label Dynamics AX IO. Show all posts
Showing posts with label Dynamics AX IO. Show all posts

Tuesday, August 23, 2011

Excel Value to String

Query: How can i create a generic method for returning string values from excel.

Answer:

// convert into str from excel cell value
str COMVariant2Str(COMVariant _cv,
                    int _decimals = 0,
                        int _characters = 0,
                        int _separator1 = 0,
                        int _separator2 = 0)
{
    switch (_cv.variantType())
    {
        case (COMVariantType::VT_BSTR):
            return _cv.bStr();

        case (COMVariantType::VT_R4):
            return num2str(_cv.float(),_characters,_decimals,_separator1,_separator2);

        case (COMVariantType::VT_R8):
            return num2str(_cv.double(),_characters,_decimals,_separator1,_separator2);

        case (COMVariantType::VT_DECIMAL):
            return num2str(_cv.decimal(),_characters,_decimals,_separator1,_separator2);

        case (COMVariantType::VT_DATE):
            return date2str(_cv.date(),123,2,1,2,1,4);

        case (COMVariantType::VT_EMPTY):
            return "";

        default:
            throw error(strfmt("@SYS26908", _cv.variantType()));
    }
    return "";
}

Monday, June 13, 2011

Reading CSV File in AX

Query:
How can I read a csv file in AX?

Answer:
use the following code,

public void run()
{
    #File
    CommaTextIo commaTextIo;
    FileIOPermission permission;
    container containFromRead;
    int x;
    int cols;
    PurchIdBase purchId;
    LineNum lineNum;
    ;
    super();
    if(!this.validate())
    {
        return;
    }


    try
    {
       
        permission = new FileIOPermission(filePath, #io_read);
        permission.assert();


        commaTextIo = new CommaTextIO(filePath, #io_read);
        containFromRead = commaTextIo.read();


        while(containFromRead)
        {
            cols = conLen(containFromRead);
            for(x=1; x <= cols; x++)
            {
                info(any2str(conpeek(containFromRead, x)));
            }
            containFromRead = commaTextIo.read();
        }
        commaTextIo = null;
    }
    catch(Exception::Error)
    {


    }
}

CommaTextIO not initialized error

Query:

I am getting commatextio not initialized error and it is not going away. I am sure my code is right.

Answer:

Make sure you are on the correct tier (client or server) and make sure you have permission on that file for the specific tier.

Hint: see the main and construct methods make sure they don't have only server keyword.