2018-04-25

[AX2009] AX COM Business Connector

This is how to create a connection from C# to Dynamics AX 2009.
Install AxCom.dll
  1. Run  D:\Setup.exe HideUI=1 AcceptLicenseTerms=1 InstallComBusinessConnector=1 if it is failed, find AxCom.dll from installation and copy the file into C:\Program Files (x86)\Microsoft Dynamics AX\50\Client\Bin.
  2. Register AxCom.dll by regSvr32 in command prompt.
Create a Class in AX
  1. Create axCom class.
  2. Create method getName.
Name getName(Name _name)
{
    ;
    return strfmt("-- %1 --",_name);
}
Create a C# Console program (Just a sample) 
  1. Add reference to AxCom.dll, in tab COM, select Axapta Com Connector 1.2 Type Library.
  2. C# code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AxaptaCOMConnector;//AX Business Connector

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Axapta3             ax = new Axapta3();
            IAxaptaObject       axObj;
            string              axName;
            AxaptaParameterList list = new AxaptaParameterList();
            String              name;

            name = "Default Name";
            //axCOM.Logon("CEU", "", "", "");
            ax.LogonAs("axService", "DAX", "axProxy", "DAX", "Password");//log on as AxService user in DAX domain over on proxy user. Add parameters as needed.
            axObj = ax.CreateObject("axCom", null, null, null, null, null, null);//create object from axCom class in AX

            name = System.Console.ReadLine();//get name from console
          
            //create parameter
            list.Size=1;
            list.set_Element(1, name);
          
            axName = (string)axObj.CallEx("getName", list);//execute method in axCom class
          
            System.Console.WriteLine(axName);//show modified name in console
            System.Console.ReadKey();//wait until hit a key
          
            ax.Logoff();//log out
        }
    }
}


2018-04-16

[AX2009] Run Workflow Batch Job

Find SysWorkflowMessageQueueManager and WorkflowWorkItemDueDateJob in AOT. Run them by right click.

2018-02-26

[AX2012] Parallel Compile X++ And Deploy All Reports

Compile X++
Command Prompt:

C:\>cd "Program Files\Microsoft Dynamics AX\60\Server\MicrosoftDynamicsAX\bin"
C:\Program Files\Microsoft Dynamics AX\60\Server\MicrosoftDynamicsAX\bin>axbuild.exe xppcompileall /s=01 /altbin="C:\Program Files (x86)\Microsoft Dynamics AX\60\Client\Bin"


Deploy Reports
Microsoft Dynamics AX 2012 Management Shell:
Publish-AXReport –ReportName *


2017-03-31

[AX2012] Minimum On-hand on On-hand Form

Put display method in InventSum datasource:
\Forms\InventOnhandItem\Data Sources\InventSum\Methods\inventOnHandMin

display public InventQtyMinOnhand inventOnHandMin(InventSum _inventSum)
{
    InventDim       joinDim, dimValues;
    InventDimParm   dimParm;
    ReqItemTable    reqItemTable;

    dimValues.data(_inventSum.joinChild());
    dimParm.initFromInventDim(dimValues);

    select sum(MinInventOnhand) from reqItemTable where reqItemTable.ItemId == _inventSum.ItemId
        #InventDimExistsJoin(reqItemTable.CovInventDimId, joinDim, dimValues, dimParm);

    return reqItemTable.MinInventOnhand;
}

Then put this method in overview grid.

2017-03-09

[AX 365] Upload and Read Excel File

This is a sample to upload and read Excel file.

class RunnableClass1UploadExcel
{       
    public static void main(Args _args)
    {       
        FileUploadTemporaryStorageResult result;
        str                 fileUrl;
        System.IO.Stream    stream;

        OfficeOpenXml.ExcelWorksheet    workSheet;
        OfficeOpenXml.ExcelPackage      package;
        OfficeOpenXml.ExcelRange        cells;

        int         rowCount, colCount;
        anytype     anyValue;
        container   conRow;
        ;
        result = File::GetFileFromUser() as FileUploadTemporaryStorageResult;
       
        if (result && result.getUploadStatus())
        {
            fileUrl = result.getDownloadUrl();
            try
            {
                stream = File::UseFileFromURL(fileUrl);
                package = new OfficeOpenXml.ExcelPackage(stream);

                if(package)
                {
                    worksheet   = package.get_Workbook().get_Worksheets().Copy("Sheet1","Data");
                    cells       = worksheet.get_Cells();
                    rowCount    = worksheet.get_Dimension().get_End().get_Row();
                    colCount   = worksheet.get_Dimension().get_End().get_Column();

                    for (int i=2;i<=rowCount;i++)//row
                    {
                        conRow = conNull();
                        for (int j=1;j<=colCount;j++)//column
                        {
                            anyValue= cells.get_Item(i, j).get_Value();
                            conRow += anyValue;
                        }

                        if(conRow)
                        {
                            info(
                                strFmt("%1 - %2",
                                    any2Str(conPeek(conRow, 1)),
                                    any2Str(conPeek(conRow, 2))
                                    )
                                );
                        }
                    }
                }
            }
            catch(Exception::Error)
            {
                info(strFmt("%1 - %2",Exception::Error, fileUrl));
            }
            result.deleteResult();
        }
    }

}

2017-03-08

[AX 365] Create CSV File

This is a sample code to create csv file. It use CommaTextStreamIo class.

class RunnableClassExport2CSV
{       
    public static void main(Args _args)
    {   
        ATSGroupTable     groupTable;
        CommaTextStreamIo comma = CommaTextStreamIo::constructForWrite();
        ;
        while select groupTable
        {
            comma.writeExp([groupTable.GroupId, groupTable.Name]);
        }
        File::SendFileToUser(comma.getStream(), "atus.csv");
    }

}

2017-02-22

Convert/Clone VHDX to VHD

Virtualbox cannot run VHDX file currently. So we have to convert them to VHD or VDI.
Here is a command to convert/clone from VHDX to VHD
VBoxManage.exe clonehd "PATH_TO_YOUR_FILE.VHDX" --format VHD "PATH_TO_CONVERTED_FILE.vhd"
Sample command:
C:\Program Files\Oracle\VirtualBox>VBoxManage.exe clonehd c:\MyData\VM\VBox\TOCR3CU9\StandardCU9.vhdx --format VHD c:\MyData\VM\VBox\TOCR3CU9\StandardCU9.vhd