Sunday, 9 February 2014

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;
}

Wednesday, 29 January 2014

How To Get Date A Year Before & A Year After in AX 2012

To get date after 1 year from a date in Microsoft Dynamics AX, we can use function "nextYr(date _date)" and we can use function "prevYr(date _date)" to get 1 year before.

Static Void PrevNextYear_test()
{
    date      testDate = str2date("30/1/2014", 123);
    ;
    info(strfmt("This Date : %1, 1 Year After : %2", testDate, nextYr(testDate)));
    info(strfmt("This Date : %1, 1 Year Before : %2", testDate, prevYr(testDate)));
}

If we take start date from user and End Date comes a year after then.
Write Following code in AOT > Forms > FormName > Design > Design > Start Date > modified()
Set Auto Declaration property to Yes.

public boolean modified()
{
    boolean ret;
    ret = super();
    TableName.EndDate = str2date(StartDateControlName.valueStr(), 1234);
    TableName.EndDate = nextYr(TableName.EndDate)-1;
    return ret;
}

Filter Records Based On Unbound Controls in Ax 2012

1. In the AOT, expand Forms, and then expand the form where you want to add fields to the filter group.
2. Expand Designs, expand Design,  click New Control, and then click the type of control you want to use. The control is         added to the group.
3. Expand the control node, right-click Methods, click Override methods, and then click LookUp. 
public void lookup()
{
    QueryBuildDataSource qbds;
    Query query = new Query();
    SysTableLookup sysTableLookup;
    super();
    sysTableLookup = SysTableLookup::newParameters(tableNum(Table1),this);
    qbds = query.addDataSource(tableNum(Table1));
    sysTableLookup.addLookupfield(fieldNum(Table1, RelatedId));
    sysTableLookup.parmQuery(query);
    sysTableLookup.performFormLookup();
}
In the same Control, right-click the Methods, click Override method, and then click modified
public boolean modified()
{
    boolean ret;
    ret = super();
    Table1_ds.executeQuery();
    return ret;
}

4. Expand the Methods node of the form, right-click classDeclaration,
public class FormRun extends ObjectRun
{
    QueryFilter queryFilter;
}
5. Expand the Data Sources node, and then find the table that has the field that has the      values that you will use to filter the list.
6. Expand the table node, right-click Methods, click Override method, and then click init. 
public void init()
{
    super();
    queryFilter = Table1_ds.query().addQueryFilter(Table1_ds.queryBuildDataSource(),"relatedID");
}
In the same table, right-click the Methods, click Override method, and then click executeQuery
public void executeQuery()
{
    queryFilter.value(element.design().controlName("StringEdit").valueStr());
    super();
}