Friday, 3 April 2015

Export Data with Outbound ports in AX 2012

Outbound ports are not related with web services or network at all (talking about WCF, bindings and endpoints). You can use it in order to retreive data from Dynamics AX and send the results to either a file or a message queue (MSMQ).


The following post will show a summary of the steps I followed in order to export customers in several XML files into a directory on the AOS server.






1)  Create an outbound port specifying "File system adapter" as adapter.
You can limit it to a specific company if you want
In the operations choose : CustCustomerService.read

This service operation will be used to read a customer from AX by specifying an account number.
The output result of the operation will be the customer itself.



2)  I create a new folder on the AOS side (in the example C:\AIFOutboundTest)
    Don't forget to give the correct write access rights for the user executing your AOS Service. The AOS will need write access to create the file.

3)  Specify the newly created folder in the URI of the outbound port and save the outbound port and activate it.

4)  Create a new job to post some customer in the AIF Queue :



static void sendCustomerOutbound(Args _args)
{

    AxdSendContext axdSendContext = AxdSendContext::construct();
    AifEntityKey aifEntityKey = AifEntityKey::construct();
    Map keyData;
    AifConstraintList aifConstraintList = new AifConstraintList();
    AifConstraint aifConstraint = new AifConstraint();

    CustTable       custTable;
    ;

    custTable = CustTable::find("US-001");

    keyData = SysDictTable::getKeyData(custTable);

    aifEntityKey.parmTableId(custTable.TableId);
    aifEntityKey.parmRecId(custTable.RecId);
    aifEntityKey.parmKeyDataMap(keyData);

    axdSendContext.parmXMLDocPurpose(XMLDocPurpose::Original);
    axdSendContext.parmSecurity(false);

    aifConstraint.parmType(AifConstraintType::NoConstraint) ;
    aifConstraintList.addConstraint(aifConstraint) ;

    AifSendService::submitDefault(
        classnum(CustCustomerService),
        aifEntityKey,
        aifConstraintList,
        AifSendMode::Async,
        axdSendContext.pack());

}

5)  Once this code is executed, a new record will be created in the AIF Queue manager.

You can reach the queue manager with the following path :
System Administration > Periodic > Services and Application Integration Framework > Queue manager

Status will be ready and you will see the document service operation involved.




6)  Now the instructions are created with correct parameters. You can either setup a batch system that will automatically execute the following method, or you can simply execute it manually.

static void runAIF()
{
    AifOutboundProcessingService outbound = new AifOutboundProcessingService();    outbound.run();
}

7)  Once this method has been executed, you will be able to see the resulting newly created AIF message (XML result). For this, use the following menu : Document Log and View message.




8)  Once the message has been created, we can execute the following method :

static void sendAIF()
{
    AifGatewaySendService        send     = new AifGatewaySendService();
    send.run();
}

9)  This operation will export the message to a physical XML file on the folder we specified in the outbound port.





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