Monday, December 15, 2014

Create Counting Journal in AX 2012 R2 via C# (Document Services)

Hi There,

Hi, I'd like to show some C# code that will create a Counting journal in AX 2012 R2.


I will be using the InventCountingJournalService service that already ships with AX 2012 R2.

This is a very low cost-high reward approach for the customers.

Let's start:


1- Create a Service Group








2- Add the InventCountingJournalService to the Service Group

3- Deploy the Service Group. This will output the following.








4- Get the WSDL URI from the inbound ports form.





5- Go to Visual Studio, create a new windows form project, add a button and double click the button to create a button event. 


6- Right - Click the Service References and choose Add Service Reference.





7 - Past the WSDL URI and click GO





8- Give your service a name i.e. InventCountingJournnal 

9 - Write the following code and test. 


 private void InventCountingJournal()

 {
            InventCountingJournal.CallContext callContext = new InventCountingJournal.CallContext();

            InventCountingJournal.CountingJournalServiceClient servClient = new  InventCountingJournal.CountingJournalServiceClient();


            InventCountingJournal.AxdCountingJournal countJournal = new InventCountingJournal.AxdCountingJournal();


            InventCountingJournal.AxdEntity_InventJournalTable journalHeader = new InventCountingJournal.AxdEntity_InventJournalTable();


            //Header

            callContext.Company = "CEU";
            journalHeader.JournalNameId = "CountJour";
            journalHeader.Description = "Counting Journal";
            //Header

            //lines

            InventCountingJournal.AxdEntity_InventJournalTrans journalLines = new InventCountingJournal.AxdEntity_InventJournalTrans();

            journalLines.ItemId = "12345";

            journalLines.Qty = 50;
            journalLines.TransDate = DateTime.Now;

            InventCountingJournal.AxdEntity_InventDim inventDim = new InventCountingJournal.AxdEntity_InventDim();


            inventDim.InventBatchId = "3";

            inventDim.InventLocationId = "1";
            inventDim.InventSiteId = "3";

            journalLines.InventDim = new InventCountingJournal.AxdEntity_InventDim[1] { inventDim };


            //Lines


            journalHeader.InventJournalTrans = new InventCountingJournal.AxdEntity_InventJournalTrans[1] { journalLines };


            countJournal.InventJournalTable = new InventCountingJournal.AxdEntity_InventJournalTable[1] { journalHeader };


            servClient.create(callContextcountJournal);

 }


You can test this by clicking the button, and calling this method. A new counting journal would be created in AX. Then, you can either have a batch posting all the journals or simply have a user doing it manually. 


That's all for now!

Saturday, November 1, 2014

DataUpdate Through X++ Scripts (Advanced)

Sometimes it's just better to roll up your sleeves and write a little bit of X++ code is when it comes to updating masses of data. I’m not talking about updating 100 or so records because you can use Excel for that, but if you need to update 1,000’s or 10’s of thousands of records then Excel can be a little slow, and a script is so much faster.
How To Do It…
In this example, I want to update the Calculation Group on all of my released products – because I forgot to load it through DIXF.
  1. To create the script, just open up the AOT by pressing CTRL+D.
  2. And then press CTRL+SHIFT+P to open up the Projects explorer.
  3. Right-mouse-click on the Projects folder and select the Project option from the New submenu to create a new Project.
  4. Then right-mouse-click on the project and select the Open option to open up the project itself.
  5. When the Project is displayed, right-mouse-click on the header and select the New menu, and then the Job option.
  6. This will open up an X++ scripting pane for you to write your code within.
  7. Now is the dirty part. Create a variable that points to the table you want to update – in this case it’s the InventTable by adding this line of code: InventTable item;
  1. Then create a loop that will step through every record by typing this:
    while 
    select forUpdate item
    {
    }
  1. Then add your code to see if there is a record in the table to update:
    if (item)
    {
    }
  1. Finally, add your code to update the record:
    ttsBegin;
    item.BOMCalcGroupId = “DEFAULT”;
    item.update();
    ttscommit;
  1. The full code will look like this.

    static void Job11(Args _args)
    {
      InventTable item;
while select forUpdate item
{
if (item)
{
ttsBegin;
item.BOMCalcGroupId = “DEFAULT”;
item.update();
ttscommit;
}
}
}
  1. To run the job, just click on the green play button in the menu bar.
Now all of the records will be updated for you.

Tuesday, September 2, 2014

Parallel Compilation

Hello everybody!

I want to start the post #1 of this blog explaining a little bit about a tool that Microsoft implemented on MS Dynamics AX 2012 CU7 -- the parallel compilation. The official page of the parallel compilation is here.

Parallel compilation of an AOS was properly implemented on CU7 version of AX 2012 R2. There are some guys over the web that said that the parallel compilation works for older versions, like CU6. Particularly, I don't believe. I worked in a project that a full AOS compilation on CU6 took 12 ~ 13 hours. CU7 came to help us developers and build engineers to not waste time in a simple task!

Below we have a small script that performs the parallel compilation and also compile the IL code. Note: the axbuild.exe calculate the numbers of workers automatically according to the number of cores on server processor, as well as the process loaded on them.

Use this script only in development environments! Carefully!

SET ServiceName=AOS60$01
SET AOSHost=your_aos_name
SET AOSPort=2712
SET ServerPath="C:\Program Files\Microsoft Dynamics AX\60\Server\your_server_path\bin"
SET ClientPath="C:\Program Files (x86)\Microsoft Dynamics AX\60\Client\bin"
SET AppExe="C:\Program Files (x86)\Microsoft Dynamics AX\60\Client\bin\ax32.exe"

net stop %ServiceName%
net start %ServiceName%

cd\

cd %ServerPath%

axbuild.exe xppcompileall /s=01 /v /a=%ClientPath%

cd\

%AppExe% -aos2=%AOSHost%:%AOSPort% -startupcmd=compileil -lazytableloading -lazyclassloading

net stop %ServiceName%

net start %ServiceName%

If you don't want to run the IL compilation, just remove the line %AppExe% -aos2=%AOSHost%:%AOSPort% -startupcmd=compileil -lazytableloading -lazyclassloading.

Save this script as a batch file (extension .bat). With this, you can schedule tasks using Windows Server.

Enjoy DAXers!

Note: during the day, I pretend to improve this post. There are some more interesting details hidden on the command.

Friday, August 8, 2014

Post Zero

Let's work. Programming is only a matter of code. It is the art of translate what users want into a language that will become a system, a functionality. This blog has the goal to provide some tips and tricks about how programming better in X++, for all audiences -- since AX functional consultants that want to understand how the engine on the background makes the "magic" up to senior architects that had forgotten some small code.

Programming in X++, the language for AX is not easy. Sometimes the code is well-written, compiles fine, but the system does not work as the expect. Many daily problems can be solved changing some system/modules parameters. So, every time, before start a new customization, ask your user, functional consultant or manager if the system does not have a second path to solve the business inquiry. Check if the solution was not given at Microsoft Sure Step or Lifecycle Services (let's talk about them on the future).

If the only way is a change, let's do it. Once. Let's avoid "go-horse process" or "jeitinho brasileiro" (Brazilian readers will know well what I'm talking about).