Dynamics Search Engine

Monday, June 20, 2016

How to export data in a csv or comma separated file by X++ code in Microsoft Dynamics AX 2012


In Microsoft Dynamics AX 2012 how to export data in a csv or comma separated file by X++ code.

This article explains how to generate a csv or comma separated file and export data from Microsoft Dynamics AX 2012 by X++ code.



Applied on: Dynamics AX 2012 R3 CU9.
Prerequisite: Basic Dynamics AX 2012 programming knowledge.
Target audience: AX programmers.
Assumption: You are familiar with Dynamics AX 2012.



I prepared a x++ job here which will guide you how to generate a csv file with a header and one sample line. If you have one header and multi line, you may keep line in a while.



static void exportDataInCSV(Args _args)

{

    Dialog          dialog  = new Dialog();

    DialogField     dialogFieldFileSave;

    Commaio         file;

    container       line;

    FilePath        TempPath;



    dialogFieldFileSave     = dialog.addField(extendedTypeStr(FilenameSave),"File path","Help text goes here");

    dialog.caption("Caption goes here");

    dialog.filenameLookupFilter(['csv','*.csv']);



    if(!dialog.run())

        return;



    if (dialogFieldFileSave.value() == '')

    {

        error("Please set up the location for export file.");

        return;

    }



    TempPath = dialogFieldFileSave.value() + '.csv';



    #define.filename(TempPath)

    #File





    file = new Commaio(#filename , #io_write);

    if( !file || file.status() != IO_Status::Ok)

    {

        throw error("File Cannot be opened");

    }



    //Put headers

    line = ["CardNum","Amount","IssueDate","ExpiryDate","IssueBy","CustAccount","Description"];

    file.writeExp(line);



    //Put a sample line

    line = ["C_476786","100.00","05/26/2015","03/25/2016","John Smith S","Cust_0000034","Description"];

    file.writeExp(line);



    info(strFmt("File exported successfully. Please find it at %1",TempPath));

}





Hope this was useful. You may leave your comment below.








Thursday, June 16, 2016

In Microsoft Dynamics AX 2012 how to update a division, department, location cost center as financial dimension by X++ code.

In Microsoft Dynamics AX 2012 how to update a financial dimension field by X++ code.

In Microsoft Dynamics AX 2012 how to update a division, department, location cost center as financial dimension by X++ code.


Applied on: Dynamics AX 2012 R3 CU8.
Prerequisite: Basic Dynamics AX 2012 programming knowledge.
Target audience
: AX programmers.
Assumption
: You are familiar with Dynamics AX 2012.


There was a requirement from one of the functional analyst to update the 3rd Financial Dimension only. When functional analyst loaded products to Dynamics AX this financial dimension was missing for every product. It was about 90 thousand products. Because of huge number of products, it was possible for him to do it manually. I wrote a X++ job to update it.



Here is the X++ job.

static void updateFinancialDimension(Args _args)
{
    Struct                  struct = new Struct();
    container               financialDimension;
    DimensionDefault        dimensionDefault;
    InventTable             inventTable;
    InventItemGroupItem     inventItemGroupItem;

    ttsBegin;
    select forUpdate inventtable
        where inventTable.dataAreaId == 'your data area id' && inventTable.ItemId == 'Test';
   
    struct.add('Division', '048');
    financialDimension += struct.fields();
    financialDimension += struct.fieldName(1);
    financialDimension += struct.valueIndex(1);
       
    dimensionDefault = AxdDimensionUtil::getDimensionAttributeValueSetId(financialDimension);
       
    if (inventTable.RecId)
    {
        inventTable.DefaultDimension = DimensionDefault;
        inventTable.update();
        //info(inventTable.ItemId);
    }
   
    ttsCommit;
}


After executing the above job I got below result.


Now question is if you need to do it for all the 4 dimensions how to do?
Here I have another X++ job to do it for all the financial dimension fields update.
static void updateAllFinancialDimension(Args _args)
{
    Struct                  struct = new Struct();
    container               ledgerDimension;
    DimensionDefault        dimensionDefault;
    InventTable             inventTable;
    InventItemGroupItem     inventItemGroupItem;

    ttsBegin;

    //while select forUpdate inventtable
    //    where inventTable.dataAreaId == 'your data area id' && inventTable.ItemId == 'Test'
       
    select forUpdate inventtable
        where inventTable.dataAreaId == 'your data area id' && inventTable.ItemId == 'Test';
   
    struct.add('BusinessUnit', '001');
    struct.add('CostCenter', '002');
    struct.add('Division', '033');
    struct.add('Location', '0475486');
    ledgerDimension += struct.fields();
    ledgerDimension += struct.fieldName(1);
    ledgerDimension += struct.valueIndex(1);
    ledgerDimension += struct.fieldName(2);
    ledgerDimension += struct.valueIndex(2);
    ledgerDimension += struct.fieldName(3);
    ledgerDimension += struct.valueIndex(3);
    ledgerDimension += struct.fieldName(4);
    ledgerDimension += struct.valueIndex(4);

    dimensionDefault = AxdDimensionUtil::getDimensionAttributeValueSetId(ledgerDimension);
       
    if (inventTable.RecId)
    {
        inventTable.DefaultDimension = dimensionDefault;
        inventTable.update();
        //info(inventTable.ItemId);
    }
   
    ttsCommit;
}



Hope this was useful. You may leave your comment below.




In Microsoft Dynamics AX 2012 how to get a progress bar to display the progress of a process or transaction by X++ code.

In Microsoft Dynamics AX 2012 how to get a progress bar to display the progress of a process or transaction by X++ code.

Applied on: Dynamics AX 2012 R3 CU8.
Prerequisite: Basic Dynamics AX 2012 programming knowledge.
Target audience
: AX programmers.
Assumption
: You are familiar with Dynamics AX 2012.

There are different ways to get a progress bar or update bar while a lengthy transaction or update is happening.
It’s always nice to give a progress bar to user for any lengthy manual operation so that gives user an indication like something is happening.
Below method is responsible to give a progress bar if you use it for any heavy or lengthy operation.

Option 1:

public static client SysOperationProgress showWaitDialog(str _text = "Extracting record for you, please wait...")
{
    #AviFiles
    SysOperationProgress waitDialog;

    waitDialog = new SysOperationProgress(1, true, strLen(_text));

    waitDialog.setCaption("Calculating value...");
    waitDialog.setTotal(100);
    waitDialog.setCount(1);
    waitDialog.setAnimation(#AviUpdate);
    waitDialog.setText(_text);

    return waitDialog;
}

The output is shown below.


To get the above shown output you need to create a method as shown above and call the method before your lengthy operation starts.
How to call?
I have a sample job here to call the method.




Option 2:

Add startLengthyOperation() before and after your lengthy operation code. A sample code is shown below with output. In this case you see a busy icon and system does not allow to do anything.







Wednesday, June 15, 2016

In Microsoft dynamics AX 2012 (RTM, R2 or R3) how to get the financial dimension structure along with ledger account or any other account by X++ code?

From this article you may get answer of below questions.
  • In Microsoft dynamics AX 2012 (RTM, R2 or R3) how to get the financial dimension structure by X++ code?
  • In Microsoft dynamics AX 2012 (RTM, R2 or R3) how to get the dimension structure along with main account by X++ code?
  • In Microsoft dynamics AX 2012 (RTM, R2 or R3) how to get the financial dimension structure along with ledger account, bank account, customer account or any other account by X++ code?
  • In Microsoft dynamics AX 2012 (RTM, R2 or R3) different ways to get the financial dimension structure by X++ code?
  • In Microsoft dynamics AX 2012 (RTM, R2 or R3) how to get financial dimension attribute and its value by X++ code?


Applied on: Dynamics AX 2012 R3 CU8.
Prerequisite: Basic Dynamics AX 2012 programming knowledge.
Target audience
: AX programmers.
Assumption
: You are familiar with Dynamics AX 2012.





Please see the above image, it’s a financial dimension on sales order, in general such kind of field holds a RecId if you see it at table level. Question is how to get all these dimensions and their values by X++ code?
You may have faced the requirement where you need to bring the dimension structure along with an account. E.g. a ledger account should be flown along with the default or financial dimension structure. Let’s say ledger account number 3235 and above shown financial dimension should be added and final outcome should be like 3235-001-100-047-00001. How to get this ledger account along with structure by x++ code?
There are two ways to get this done.

Option 1:
In below example I need to assign offset ledger dimension to ledgerJournalTrans table field along with financial dimension from SalesTable. When you need to create payment journal and voucher by X++ code, it could be a perfect requirement match for you.

LedgerJournalTrans.OffsetLedgerDimension = DimensionDefaultingService::serviceCreateLedgerDimension(5637189003,SalesTable.DefaultDimension);

Here serviceCreateLedgerDimension() is a method of DimensionDefaultingService class which gives you a RecId with structure combination. To this method I’m passing two parameters, 1st one is a ledger Account coming from Account Receivable parameter which is account number 10140 and 2nd one is coming from SalesTable.DefaultDimension field. This is according to my requirement. In my case I should get the outcome like 10140--001-100-047-00001 as return value from serviceCreateLedgerDimension() method which again nothing a RecId only and on left hand side LedgerJournalTrans.OffsetLedgerDimension field is expecting a RecId.






Option 2:
In below example I need to assign offset ledger dimension to ledgerJournalTrans table field along with financial dimension from SalesTable.
//acctPattern = ['10140','10140',4,'BusinessUnit','001','CostCenter','100','Division','047','Location','00001'];

LedgerJournalTrans.OffsetLedgerDimension = AxdDimensionUtil::getLedgerAccountId(acctPattern);

Here I’m giving a parm to getLedgerAccontId() which is nothing a container type variable called acctPattern. What this container type variable contains? To get the answer you may refer above commended line. The 1st two values of this container are main account display value and main account number or ledger account, 3rd value is number of dimension. I have 4 dimensions here. The 4th and 5th values are dimension Business unit and its value 001. The 6th and 7th values are dimension Cost center and its value 100. The 8th and 9th values are dimension Division and its value 047. The 10th and 11th values are dimension Location and its value 00001.

I know you must be thinking why to pass hard coded values to container type variable?
By using X++ code you can make it dynamic.
Below code you may use as an example to get container type return. You can create a method at tale or class level.

public container getDimensionPattern(DimensionDisplayValue _displayValue, LedgerJournalAC _accountNum, DimensionDynamicDefaultAccount _defaultDimension)
{
    container                       conAcctStructure;
    RetailMCRChannelTable           RetailMCRChannelTable;
    DimensionAttributeValueSet      dimAttrValueSet;
    DimensionAttributeValueSetItem  dimAttrValueSetItem;
    DimensionAttributeValue         dimAttrValue;
    DimensionAttribute              dimAttr;

    conAcctStructure += _displayValue;
    conAcctStructure += _accountNum;
    conAcctStructure += 4;              //4 Dimensions

    dimAttrValueSet = DimensionAttributeValueSet::find(_defaultDimension);
   
    while select dimAttrValueSetItem
        where   dimAttrValueSetItem.DimensionAttributeValueSet   == dimAttrValueSet.RecId
    {
        dimAttrValue        = DimensionAttributeValue::find(dimAttrValueSetItem.DimensionAttributeValue);
        dimAttr             = DimensionAttribute::find(dimAttrValue.DimensionAttribute);
           
        conAcctStructure += dimAttr.Name;
        conAcctStructure += dimAttrValue.getValue();
    }

    return conAcctStructure;
}


Below screenshot was taken while I was debugging the above method.



Hope this was useful. You may leave your comment or feedback below.

Tuesday, June 7, 2016

How to get postal address for customer or vendor by address type in Microsoft Dynamics AX 2012

This article explains how to get postal address for customer or vendor by address type in Microsoft Dynamics AX 2012 using X++ code.

Applied on: Dynamics AX 2012 R3 CU8.
Prerequisite: Basic Dynamics AX 2012 programming knowledge.
Target audience: AX programmers.
Assumption: You are familiar with Dynamics AX 2012.

Couple of times I received queries from my coworkers that how to get customer address or vendor address by type using standard code.

I noticed few of my coworkers used to write custom code to get postal address for customer or vendor and they struggled to find table(s) to be included in the query. It's time consuming.

In Microsoft Dynamics AX 2012 we have standard class which you can use to get address without writing any custom code.
E.g. Class DirParty has couple of useful methods for different purpose like getPostalAddress() to get the postal address, getPostalAddressByType() to get the postal address by address type etc.

The method getPostalAddressByType() gives you a complete address based on party and address type. Here you need to make a note that this method gives the address in a single string. If you need entire address information by field values, you can copy this method and write a new method where you can return the table LogisticsPostalAddress instead of Addressing EDT. Below code image you can refer to make required change. The highlighted code in red color should be removed and green color code you need to add at first and last line as shown in the image.