Search code examples
axaptabusiness-connector

How to get the number of values in an array in .Net?


AX allows arrays to be defined, but while fetching information through the .NET Business Connector, it shows as a single field. E.g: Dimension is set by:

axRec.setField("Dimension[1]","A");
axRec.setField("Dimension[2]","B");
axRec.setField("Dimension[3]","C");
// and so on...

How do I know how many fields "Dimension" have?


Solution

  • AX supports a compile time function dimOf to return the count, but that is not available from .Net!

    To rescue comes the DictField class:

    X++ code:

    DictField df = new DictField(tablenum(CustTable), fieldnum(CustTable, AccountNum));   
    if (df)  
    { 
        print strfmt("The arraySize is %1.", df.arraySize());  
    }
    

    You can make a X++ utility function, then call that:

    static int arraySize(str tableName, str fieldName)
    {
        DictField df = new DictField(tableName2Id(tableName), fieldName2Id(tableName2Id(tableName), fieldName)));   
        return df ? df.arraySize() : -1;  
    }