Monday, 10 February 2014

Change the language of Form based on the language selected by user.

Override the CanClose Method of the Form:-

public boolean canClose()
{
    boolean ret;
    ret = super();
    infolog.language("en-us");
    return ret;
}

Then Override the Clicked Method of the Form Control and  then write the below code-

void clicked()
{
    infolog.language("en-us");
     super();
}

Sunday, 9 February 2014

Export Records To Excel Through Code In Ax 2012

static void ExportImportData(Args _args)
{
   SysExcelApplication      xlsApplication;
   SysExcelWorkBooks    xlsWorkBookCollection;
   SysExcelWorkBook     xlsWorkBook;
   SysExcelWorkSheets   xlsWorkSheetCollection;
   SysExcelWorkSheet    xlsWorkSheet;
   SysExcelRange           xlsRange;
   Tablename                  exportImportRecords;
   int                               row = 1;
   str                               fileName;
   ;
   //Filename
   fileName = "C:\\Libraries\Documents\exportImportRecords.xlsx";
   //Initialize Excel instance
   xlsApplication           = SysExcelApplication::construct();
   //Create Excel WorkBook and WorkSheet
   xlsWorkBookCollection    = xlsApplication.workbooks();
   xlsWorkBook              = xlsWorkBookCollection.add();
   xlsWorkSheetCollection   = xlsWorkBook.worksheets();
   xlsWorkSheet             = xlsWorkSheetCollection.itemFromNum(1);
   //Excel columns captions
   xlsWorkSheet.cells().item(row,1).value("Account Num");
   xlsWorkSheet.cells().item(row,2).value("Name");
   xlsWorkSheet.cells().item(row,3).value("Phone");
   row++;
   //Fill Excel with exportImportRecords AccountNum and Name fields (only 10 records)
   while select exportImportRecords
   {
      if(row == 10)
        break;
      xlsWorkSheet.cells().item(row,1).value(exportImportRecords.ID);
      xlsWorkSheet.cells().item(row,2).value(exportImportRecords.Name);
       xlsWorkSheet.cells().item(row,3).value(exportImportRecords.Phone);
      row++;
   }
   //Check whether the document already exists
   if(WinApi::fileExists(fileName))
      WinApi::deleteFile(fileName);
   //Open Excel document
   xlsApplication.visible(true);
   info("Export Completed");
}

Import records from Excel through code in ax 2012

static void importRecord(Args _args)
{
    SysExcelApplication     xlsApplication;
    SysExcelWorkBooks   xlsWorkBookCollection;
    SysExcelWorkBook    xlsWorkBook;
    SysExcelWorksheets   xlsWorkSheetCollection;
    SysExcelWorksheet    xlsWorkSheet;
    SysExcelRange           xlsRange;
    SysExcelCells             Cells;
    SysExcelCell               RCell;
    CommaIO                  inFile;
    int                               nRow,i;
    DialogField                dialogPath;
    Dialog                       dialog;
    Filename                   filename;
    Name                       Name;
    ID                             ID;
    Phone                       Phone;
    income                     income;
    Tablename               exportImportRecords;
    ;
    dialog = new Dialog("Import");
    dialogPath = dialog.addField(extendedTypeStr(Filenameopen), "File Name");
    dialog.run();
    if (dialog.run())
    {
        filename = (dialogPath.value());
    }
    inFile = new CommaIO (filename, 'R');
    if (!inFile || infile.status() != IO_Status::Ok )
    {
        throw error (strfmt("@SYS19312",filename));
    }
    try
    {
        xlsApplication          = SysExcelApplication::construct();
        xlsWorkBookCollection   = xlsApplication.workbooks();
        xlsWorkBookCollection.open(filename);
        xlsWorkSheetCollection  = xlsApplication.worksheets();
        xlsWorkSheet            = xlsWorkSheetCollection.itemFromNum(1);
        Cells                   = xlsWorkSheet.Cells();
        nRow = 2;
        RCell = Cells.Item(nRow, 1);
        while (RCell.value().bstr() != "")
        {   //RCell = Cells.Item(nRow, 1);
            ID       = any2int(RCell.value().bStr());
            RCell           = Cells.item(nRow,2);
            Name     = RCell.value().bStr();
            RCell           = Cells.item(nRow,3);
            Phone    = RCell.value().bStr();
            RCell           = Cells.item(nRow,4);
            income   = any2real(RCell.value().bStr());
                  // importExportData.initValue();
                    exportImportRecords.ID    = ID;
                    exportImportRecords.Name    = Name;
                    exportImportRecords.Phone      = Phone;
                    exportImportRecords.income  = income;
                    exportImportRecords.insert();

            nRow++;
            RCell = Cells.Item(nRow, 1);
        }
        xlsApplication.quit ();
        xlsApplication.finalize ();
        info("Import completed");
    }
    catch( Exception::Error)
    {
        //Close Excel.
        xlsApplication.quit ();
        xlsApplication.finalize ();
        ttsabort;
        info("Unable to process the excel import ");
    }
}

Friday, 7 February 2014

How To add multiple levels to the Infolog tree in Ax 2012

static void TestJob(Args _args)
{
    void sub1()
    {
        setprefix("Prefix");
        {
        info("Info1");
        info("Info2");
        }
        setprefix("Prefix1");
        {
        info("Info1");
        info("Info2");
        }
    }
    void sub2()
    {
        setprefix("Prefix2");
        info("Info3");
    }
    ;

    setPrefix("Main");
    sub1();
    sub2();
}

Thursday, 6 February 2014

Update Record on Line from Header with conditions (Never , Prompt, Always)

public boolean modified()
{
boolean ret;
InventTransferTable inventTransferTable1;
InventTransferLine inventTransferLine1;
TradeTable2LineUpdate TradeTable2LineUpdate;
InventParameters inventParameters;
str dlvmode;
DialogButton diagBut;
str strMessage = “Update in Line”;
str strTitle = “Prompt”;
;
ret = super();
select firstonly inventParameters;
if(inventParameters.TradeTable2LineUpdate == TradeTable2LineUpdate::Always)
{
dlvmode = this.valueStr();
select forUpdate inventTransferLine1 where inventTransferLine1.TransferId == Identification_TransferId.valueStr();
inventTransferLine1.DlvModeId = dlvmode;
inventTransferLine1.update();
inventTransferLine_ds.research();
}
if(inventParameters.TradeTable2LineUpdate == TradeTable2LineUpdate::Prompt)
{
diagBut = Box::yesNoCancel(
strMessage,
DialogButton::No,
strTitle);
if (diagBut == DialogButton::Yes)
{
dlvmode = this.valueStr();
select forUpdate inventTransferLine1 where inventTransferLine1.TransferId == Identification_TransferId.valueStr();
inventTransferLine1.DlvModeId = dlvmode;
inventTransferLine1.update();
inventTransferLine_ds.research();
}
else
{
info(” Record Not updated”);
}
}
return ret;
}

Invoke MenuItem to open Form through Code in ax 2012

static void Menuitem(Args _args)
{
MenuFunction m;

Args args = new Args();
;
m= new MenuFunction(MenuItemDisplayStr(CustTable),MenuItemType::Display).run(Args);
}

Tuesday, 4 February 2014

Attach document on list page Ax 2012

Step 1. Go To table that you are using as list page datasource and create display method.
//BP Deviation Documented
display smmDocIconNum showDocHandIcon()
{
#macrolib.resource
;
if ((select firstonly docuRef where docuRef.RefCompanyId == this.DataAreaId && docuRef.RefTableId == this.TableId && docuRef.RefRecId == this.RecId).RecId)
{
return #RES_NODE_DOC;
}
return #RES_AM_NEW;
}
Step 2: Now go to list page form > Designs >Design  > Grid > New Control > Window >
Step 3: Set window control properties Like
width : 15
height : 15
VerticalSpacing : 0
Label : Documents
DataSource : Which is used by Form
DataMethod : showDocHandIcon
Step 4 : Go to WindowControl > Method > Override Method > MouseUp >
write the code
int mouseUp(int _x, int _y, int _button, boolean _ctrl, boolean _shift)
{
CustTable custTable; // use your table
int ret;
ret = super(_x, _y, _button, _ctrl, _shift);
PurchRFQCaseTableForm::openDocHandling(custTable, element);
return ret;
}