Interview Questions

Tuesday, September 27, 2016

Finding the current company curreny code in AX 2012

Here is the code to find the company currency code in AX 2012

Method 1:
currencyCode = Ledger::accountingCurrency(CompanyInfo::current());

Method 2:
currencyCode = Ledger::accountingCurrency(CompanyInfo::Find().recid);

Method 3:
currencyCode = CompanyInfo::standardCurrency();

SSRS Reporting : Selecting the report design at the runtime based on the static parameter (dialog field) values in AX

Create a controller class that extends SrsReportRunController()
 
And override preRunModifyContract() in the controller() class and follow the below steps.
 
protected void preRunModifyContract()
{
    boolean     paramValue1;
    boolean     paramValue2;
 
    SrsReportDataContract dataContract = this.parmReportContract();
    SrsReportRdlDataContract contract = dataContract.parmRdlContract();
 
    paramValue1 = contract.getValue(Param1_name);
    paramValue2 = contract.getValue(Param2_name);
   
    if(paramValue1)
    {
        dataContract.parmReportName(reportname.design_1);
    }
    else if(paramValue2)
    {
        dataContract.parmReportName(reportname.design_2);
    }
   // else default design is used to display the report which is used in main(); or you can specify your design name here
}
 
static void main(Args _args)
{
    xxx controller = new xxx (); // Let us assume the current class as xxx
 
    controller.parmReportName(reportName.defaultDesign);  // Make sure you specify the default design while running the report and this is required to run the report and to get the dialog.
    controller.parmArgs(_args);
    controller.startOperation();
}
 

And now, Create the output menu item and specify the object type to class and specify the object to this controller class which we create now. And the report is ready to select the design at runtime.

AX2012- Error executing code: object does not have method construct

When working with AX 2012 outbound document service, I faced this issue.
When I clicked the send electronically button on my custom created form, then the I could see the message is queued in queue manager form. On running of the batch I could see status as error with description "Error executing code: <empty class> object does not have method construct".

Solution:

You need to be few things to fix this error. the issue is due to compilation error where the particular method is not found while run time. So perform the following steps.

First, compile all the objects pertaining to a AIF document service.
Secondly, compile the base class - AxInternalBase
At last, select your project node and "Generate incremental CIL". 

Reading a CSV file In Dynamics AX

static void CSVtoAX(Args _args)
{
    #File
    CommaTextIo        commaTextIo;
    FileIOPermission   permission;
    container          containFromRead;
    int                x;
    int                cols;
    ;
    commaTextIo = new CommaTextIO('u:\\items.csv',#io_read);
    commaTextIo.inFieldDelimiter(';');
    containFromRead = commaTextIo.read();
    While(containFromRead)
    {
        cols = conLen(containFromRead);
        for(x=1;x<=cols;x++)
        {
            print conpeek(containFromRead,x);
        }
        containFromRead = commaTextIo.read();
    }
    pause;
    commaTextIo = null;
}

Writing Data to CSV file from Dyanamics AX

Here is a code to write date from Microsoft Dyanics Ax to comma separated file(.csv).


static void AX2CSV(Args _args)
{
    CommaTextIO file;
    container line;
    InventTable invenTable;
    #define.filename('u:\\items.CSV')
    #File
    ;
    file = new CommaTextIO(#filename, #io_write);
    if (!file || file.status() != IO_Status::Ok)
    {
        throw error("File cannot be opened.");
    }
    file.outFieldDelimiter(';'); // for semicolon seperator and default field delimitter is ,(comma)
    while select invenTable
    {
        line = [invenTable.ItemId,invenTable.ItemName,invenTable.ItemGroupId];
        file.writeExp(line);
    }
}

Thursday, July 28, 2011 Reading Data from Excel sheet(.xls) in AX

//Read Data from Excelsheet to AX.

static void ReadFromExcelFile(Args _args)

SysExcelApplication excel;
SysExcelWorkbooks books;
SysExcelWorkbook book;
SysExcelWorksheets sheets;
SysExcelWorksheet sheet;
SysExcelCells cells;
COMVariantType type;
int row;
ItemId itemid;
Name name;
FileName filename;
;
excel = SysExcelApplication::construct();
books = excel.workbooks();
//specify the file path that you want to read
filename = "U:\\item.xls";
try
{
books.open(filename);
}
catch (Exception::Error)
{
throw error("File cannot be opened.");
}
book = books.item(1);
sheets = book.worksheets();
sheet = sheets.itemFromNum(1);
cells = sheet.cells();
do
{
row++;
itemId = cells.item(row, 1).value().bStr();
name = cells.item(row, 2).value().bStr();
info(strfmt('%1 - %2', itemId, name));
type = cells.item(row+1, 1).value().variantType();
}
while (type != COMVariantType::VT_EMPTY);
excel.quit();
}

Writing Data to Excel file in AX

//X++ Code to write data from AX to Excel sheet

static void WriteAX2ExcelFile(Args _args)
{
InventTable inventTable;
SysExcelApplication excel;
SysExcelWorkbooks books;
SysExcelWorkbook book;
SysExcelWorksheets sheets;
SysExcelWorksheet sheet;
SysExcelCells cells;
SysExcelCell cell;
int row;
;
excel = SysExcelApplication::construct();
books = excel.workbooks();
book = books.add();
sheets = book.worksheets();
sheet = sheets.itemFromNum(1);
cells = sheet.cells();
cells.range('A:A').numberFormat('@');
cell = cells.item(1,1);
cell.value("Item");

cell = cells.item(1,2);
cell.value("Name");
row = 1;
while select inventTable
{
row++;
cell = cells.item(row, 1);
cell.value(inventTable.ItemId);
cell = cells.item(row, 2);
cell.value(inventTable.ItemName);
}
excel.visible(true);
}