Dynamics Search Engine

Thursday, July 7, 2016

How to export import images or pictures from a table field by X++ code in Microsoft Dynamics AX 2012.

How to export / import a picture or image from a table field by X++ code in Microsoft Dynamics AX 2012.


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.


Below X++ job will explain you how to export an image or picture from Dynamics AX 2012 table. It's a simple piece of code but sometimes we spent hours to get the code how to export picture or image. I have posted here just to make it handy.


static void exportImageFromTable(Args _args)
{
    bindata             bin = new bindata();
    str                 content;
    container           image;
    RetailImages        retailImages;
    FilePath            filepath;
   
   
    select retailImages where retailImages.PictureId == 8045;
   
    filepath = strFmt('C:\\Users\\Administrator\\AppData\\Local\\Temp\\images\\PictureID_%1.jpg', retailImages.PictureId);
    image = retailImages.picture;
    bin.setData(image);
   
    // Create the base64 encoded string
    // content = bin.base64Encode();
    // info(content);
   
    // Save it to the file system as a jpg, png or tif format
    AifUtil::saveBase64ToFile(@filepath, content);
   
}


In addition I am share the code how to import or insert a picture or image in Dynamics AX 2012 using X++ code.

Here is the job:

static void InsertImageToTableField(Args _args)
{
   
    Bindata         binData = new BinData();
    FilePath        path;
    RetailImages    retailImages;
    str             imageID;
   
    path = "C:\\Users\\Administrator\\AppData\\Local\\Temp\\Images\\PictureID_8046.jpg"; // file location
   
    binData.loadFile(path);
   
    select retailImages where retailImages.PictureId == 1111;
   
    if (!retailImages)
    {
        retailImages.picture    = binData.getData();
        retailImages.PictureId  = 1111;
        retailImages.doInsert();
    }
   
}


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





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.