Tuesday 31 March 2015

Use display method values in other method in axapta

If some values in the form are displayed through display methods then if you need to use those values in another method to do some calculations then do this way

value = element.displaymethod1() + element.dislaymethod2;

Monday 30 March 2015

Using dictEnum to use enum in axapta

void clicked()
{
    str strValue;
    DictEnum    dictEnum;
    EnumId  enumId;
    AnyType    anyTypeValue;
    AcquisitionMethod   acqMethodType;
 
    super();

    enumId               =   enumNum(AcquisitionMethod);    
    dictEnum            =   new DictEnum(enumId);
    anyTypeValue    =   dictEnum.name2Value(strValue);
 
    element.createNew(anyTypeValue);
}

Can workout this way also ---->

int Value;
int indexValue;

dictEnum  =   new DictEnum(enumNum(AcquisitionMethod));
value         =   dict.symbol2Value("Hour"); 
 //Gets the value in int


nameText = dict.value2Name(value); //Gets the localized Name (for example "Customer")
transactionType = str2enum(transactionType, nameText);

info(enum2str(transactionType));

dictEnum = new DictEnum(enumId);
anyTypeValue    =   dictEnum.index2Name(dictEnum.name2Value(strValue)); //gets the index value





Display method to return enum value from select query in Axapta

display AssetDepreciation depreciationValue()
{
    AmountMSTSecondary dep;
    ;

    while select assetTrans
    where assetTrans.AssetId    == assetId
       && assetTrans.BookId     == bookId
       && assetTrans.TransType  == AssetTransType::Depreciation
    join ledgerTrans
    where ledgerTrans.Voucher   == assetTrans.Voucher
       && ledgerTrans.TransDate == assetTrans.TransDate
       && ledgerTrans.AmountMSTSecond < 0
    {
        dep += ledgerTrans.AmountMSTSecond;
    }

    return dep;
}

Friday 27 March 2015

Enabling and disabling fields in a grid based on enum values in AX 2012

AcqMethod is Combobox control whose auto declaration is made yes.
AcqMethod has two enum values AcquisitionAmt and AcquisitionAdjustmentAmount .

AcquisitionAmt and AcquisitionAdjustmentAmount mentioned in the code are the names of the fields in grid whose auto declaration property is yes.

Override the active method of the grid datasource and write the following code :

if(AcqMethod.selection() == 0)
    {
         AcquisitionAmt.enabled(true);
         AcquisitionAdjustmentAmount.enabled(false);
    }
    else if(AcqMethod.selection() == 1)
    {
         AcquisitionAmt.enabled(false);
         AcquisitionAdjustmentAmount.enabled(true);
    }

The same way if you need to disable complete record based on the enum values then :

if(AssetTransSecCur.Updated == NoYes::Yes)
    {
        AssetTransSecCur_ds.object(fieldNum(AssetTransSecCur, TransDate)).allowEdit(false);
        AssetTransSecCur_ds.object(fieldNum(AssetTransSecCur, AcquisitionAmt )).allowEdit(false);
        Update.enabled(false);
}


NOTE :

Update is a button with auto declaration yes.
AssetTransSecCur is the datasource name.

Friday 20 March 2015

Importing data from excel file using SysExcelApplication class in AX 2012

Write the following code in job :

static void importExcel(Args _args)
{
    SysExcelApplication application;
    SysExcelWorkbooks workbooks;
    SysExcelWorkbook workbook;
    SysExcelWorksheets worksheets;
    SysExcelWorksheet worksheet;
    SysExcelCells cells;
    COMVariantType type;
    int row;
    ItemId itemid;
    Name name;
    FileName filename;

    application = SysExcelApplication::construct();
    workbooks = application.workbooks();
    //specify the file path that you want to read
    filename = "C:\\Users\\Desktop\\itemDetails.xlsx";
    try
    {
    workbooks.open(filename);
    }
    catch (Exception::Error)
    {
    throw error("File cannot be opened.");
    }

    workbook = workbooks.item(1);
    worksheets = workbook.worksheets();
    worksheet = worksheets.itemFromNum(1);
    cells = worksheet.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);
    application.quit();

}

Wednesday 18 March 2015

Export table data to excel file using SysExcelApplication object in AX 2012

Write the following code in a job :

static void exportToExcelFile(Args _args)
{
    InventTable inventTable;
    SysExcelApplication application;
    SysExcelWorkbooks workbooks;
    SysExcelWorkbook workbook;
    SysExcelWorksheets worksheets;
    SysExcelWorksheet worksheet;
    SysExcelCells cells;
    SysExcelCell cell;
    Filename    fileName;
    int row;
 
    fileName = "C:\\desktop\\itemDetails.xlsx";

    application = SysExcelApplication::construct();
    workbooks = application.workbooks();
    workbook = workbooks.add();

    worksheets = workbook.worksheets();
    worksheet = worksheets.itemFromNum(1);
    cells = worksheet.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::find(inventTable.ItemId).itemName());
    }

    workbook.saveAs(fileName);
    workbook.comObject().save();
    workbook.saved(true);
}

Tuesday 3 March 2015

Display financial dimension in a list page in AX 2012

Inorder to display financial dimension field on a list page just add a string edit form control in the grid in PurchTableListPage and mention the datasource as PurchTable and datamethod as showFinancialDimension.

showFinancialDimension is a display method written in the PurchTable methods with the following code :

display str showFinancialDimension()
{
    PurchTable  purchTable;
    DimensionAttributeValueSetStorage   dimStorage;
    counter i;
    str dimension;

    dimStorage = DimensionAttributeValueSetStorage::find(purchTable.DefaultDimension);

    for (i = 1 ; i <= dimStorage.elements() ; i++)
    {
        if(DimensionAttribute::find(dimStorage.getAttributeByIndex(i)).Name == "BusinessUnit")
        {
            dimension = dimStorage.getDisplayValueByIndex(i);
        }
    }
    return dimension;
}

Display image for an item in PurchTable in AX 2012

In the PurchTable form create a new tab page image in the line details and whenever any line is created then corresponding image for that item should be displayed in the newly created tab page image.

1) First of all check whether any image is attached for an item or not ?

To attach an image for an item :

Goto production information management -> common -> released products ->

select any item and click on the product image button in the product tab -> document handling of item number form is opened.
click on new -> select type embedded it opens attach file window -> select the image -> click ok.
refresh the page -> image is added to the item.

2) PurchTable -> design -> add tabpage image under line details tab and add window control under it and name it as image.

3) In the form methods create a new method showImage as shown below.

public void showImage()
{
    Image   itemImage;

    productImageRecId = InventTable::find(PurchLine.ItemId).RecId;

    if(productImageRecId)
    {
        select ecoResProductImage where ecoResProductImage.RefRecId == productImageRecId
        || ecoResProductImage.RefRecord == productImageRecId;

        containerImage =  ecoResProductImage.ThumbnailSize;

        itemImage = new Image();
        itemImage.setData(containerImage);
        Image.image(itemImage);
        image.widthValue(itemImage.width());
        image.heightValue(itemImage.height());
    }
}

NOTE :  image is the window form control whose auto declaration is set to yes.

4) PurchTable -> Datasource -> PurchLine -> Methods -> active

In this method call the form method as element.showImage();

Thats it , the image will be displayed on the purchTable form.