2017-03-31

[AX2012] Minimum On-hand on On-hand Form

Put display method in InventSum datasource:
\Forms\InventOnhandItem\Data Sources\InventSum\Methods\inventOnHandMin

display public InventQtyMinOnhand inventOnHandMin(InventSum _inventSum)
{
    InventDim       joinDim, dimValues;
    InventDimParm   dimParm;
    ReqItemTable    reqItemTable;

    dimValues.data(_inventSum.joinChild());
    dimParm.initFromInventDim(dimValues);

    select sum(MinInventOnhand) from reqItemTable where reqItemTable.ItemId == _inventSum.ItemId
        #InventDimExistsJoin(reqItemTable.CovInventDimId, joinDim, dimValues, dimParm);

    return reqItemTable.MinInventOnhand;
}

Then put this method in overview grid.

2017-03-09

[AX 365] Upload and Read Excel File

This is a sample to upload and read Excel file.

class RunnableClass1UploadExcel
{       
    public static void main(Args _args)
    {       
        FileUploadTemporaryStorageResult result;
        str                 fileUrl;
        System.IO.Stream    stream;

        OfficeOpenXml.ExcelWorksheet    workSheet;
        OfficeOpenXml.ExcelPackage      package;
        OfficeOpenXml.ExcelRange        cells;

        int         rowCount, colCount;
        anytype     anyValue;
        container   conRow;
        ;
        result = File::GetFileFromUser() as FileUploadTemporaryStorageResult;
       
        if (result && result.getUploadStatus())
        {
            fileUrl = result.getDownloadUrl();
            try
            {
                stream = File::UseFileFromURL(fileUrl);
                package = new OfficeOpenXml.ExcelPackage(stream);

                if(package)
                {
                    worksheet   = package.get_Workbook().get_Worksheets().Copy("Sheet1","Data");
                    cells       = worksheet.get_Cells();
                    rowCount    = worksheet.get_Dimension().get_End().get_Row();
                    colCount   = worksheet.get_Dimension().get_End().get_Column();

                    for (int i=2;i<=rowCount;i++)//row
                    {
                        conRow = conNull();
                        for (int j=1;j<=colCount;j++)//column
                        {
                            anyValue= cells.get_Item(i, j).get_Value();
                            conRow += anyValue;
                        }

                        if(conRow)
                        {
                            info(
                                strFmt("%1 - %2",
                                    any2Str(conPeek(conRow, 1)),
                                    any2Str(conPeek(conRow, 2))
                                    )
                                );
                        }
                    }
                }
            }
            catch(Exception::Error)
            {
                info(strFmt("%1 - %2",Exception::Error, fileUrl));
            }
            result.deleteResult();
        }
    }

}

2017-03-08

[AX 365] Create CSV File

This is a sample code to create csv file. It use CommaTextStreamIo class.

class RunnableClassExport2CSV
{       
    public static void main(Args _args)
    {   
        ATSGroupTable     groupTable;
        CommaTextStreamIo comma = CommaTextStreamIo::constructForWrite();
        ;
        while select groupTable
        {
            comma.writeExp([groupTable.GroupId, groupTable.Name]);
        }
        File::SendFileToUser(comma.getStream(), "atus.csv");
    }

}