Tag Archives: X++

How to post PO by Code in Dynamics AX

I have task to integrate Dynamics AX with another application by posting PO from out side Dynamics AX, so I used AIF and build new web service to be used from out of AX and call the method below to post PO (packing slip, or invoice) .

public str CreatePostProductReceipt(PurchId _PurchId, Num _PackingSlip, ItemId  Itemid, Qty qty,
InventSiteId  InventSiteId =”, InventLocationId  InventLocationId= ” , inventBatchid batchid = ”, InventSerialId  serialId = ”, inventsizeId inventsizeId =”, InventColorId InventColorId =”)
PurchFormLetter             purchFormLetter;
PurchParmUpdate             purchParmUpdate;
PurchParmTable              purchParmTable;
PurchParmLine               purchParmLine;
PurchTable                  purchTable;
PurchLine                   purchLine;
PurchId                     purchId;
Num                         packingSlipId;
InventDim                   inventDim;
str                 ret=”;
System.Exception    err;

packingSlipId   = _PackingSlip;
purchTable      = PurchTable::find(_PurchId);

// Create PurchParamUpdate table
purchFormletter = PurchFormLetter::construct(DocumentStatus::PackingSlip); // to post invoice change to DocumentStatus::invoice


purchParmUpdate = PurchFormLetter.purchParmUpdate();

// Set PurchParmTable table
purchParmTable.TransDate                = SystemDateGet();
purchParmTable.Ordering                 = DocumentStatus::PackingSlip;
purchParmTable.ParmJobStatus            = ParmJobStatus::Waiting;
purchParmTable.Num                      = packingSlipId;
purchParmTable.PurchId                  = purchTable.PurchId;
purchParmTable.PurchName                = purchTable.PurchName;
purchParmTable.DeliveryName             = purchTable.DeliveryName;
purchParmTable.OrderAccount             = purchTable.OrderAccount;
purchParmTable.CurrencyCode             = purchTable.CurrencyCode;
purchParmTable.InvoiceAccount           = purchTable.InvoiceAccount;
purchParmTable.ParmId                   = purchParmUpdate.ParmId;

// Set PurchParmLine table
while select purchLine
where purchLine.PurchId == purchTable.purchId && purchline.ItemId == Itemid

inventDim = purchline.inventDim(true);

// Set batch and serial number
if(InventSiteId != ”)
inventDim.InventSiteId = InventSiteId;

if(InventLocationId != ”)
inventDim.InventLocationId = InventLocationId;

if(batchid != ”)
inventDim.inventBatchId = batchId;

if(serialid != ”)
inventDim.inventSerialId = serialID;

if(inventsizeId != ”)
inventDim.inventsizeId = inventsizeId;

if(InventColorId != ”)
inventDim.InventColorId = InventColorId;

purchParmLine.InventDimId = inventDim::findOrCreate(inventdim).inventDimId;

purchParmLine.ReceiveNow    = 1 ; //PurchLine.PurchQty;
purchParmLine.ParmId        = purchParmTable.ParmId;
purchParmLine.TableRefId    = purchParmTable.TableRefId;
purchParmLine.setQty(DocumentStatus::PackingSlip, false, true);

purchFormLetter = PurchFormLetter::construct(DocumentStatus::PackingSlip);

// This is the ID we hard code as the product receipt ID, if we do the posting via UI
// user would have the option to manually enter this value
return “OK”;
catch (Exception::CLRError)
err = CLRInterop::getLastException();
ret = err.ToString();
return ret;
Return “Error”;

How to reset TTSBegin/TTSCommit in AX

We are using TTSBegin and TTSCommit when update and create a record in AX, but some times an error is occurred  and through an exception.

If we did not abort TTS by using TTSAbort  statement between try and catch keyword as below




// your update code







so we may facing this error below,

To fix this error please run the following job

static void ResetTTS(Args _args)
    while (appl.ttsLevel() > 0)
        info(strfmt("Level %1 aborted",appl.ttsLevel()));

Create and Post inventory journal by code : Dynamics AX

How to create and post inventory journal using X++ by create inventjournalTable and adding inventJournalTrans, you can use the below code to import data from another system during integration between AX and others application with adding some to fetch data from, which it may be from Excel file or connect to any data base, and you can find in the blog another posts related to read from Excel file and how to connect to DB using X++.

        InventJournalTable              inventJournalTable;
        InventJournalTrans              inventJournalTrans;

        InventJournalNameId             inventJournalName;
        InventDim                            inventDim;
        JournalCheckPost               journalCheckPost;

//Below code creates journal header       


        inventJournalName =  InventJournalName::standardJournalName(InventJournalType::Movement);
        inventJournalTable.initFromInventJournalName(InventJournalName::find(inventJournalName ));



//Below code creates journal lines



        inventJournalTrans.TransDate = systemDateGet();

        inventJournalTrans.ItemId = "MDJ0001";


        inventJournalTrans.Qty = 2500;

        inventDim.InventSiteId  = ’12’;

        inventDim.InventLocationId = ‘1201’;

        inventDim.wMSLocationId = ‘BULK-001’;

        inventJournalTrans.InventDimId = inventDim::findOrCreate(inventDim).inventDimId;



//The below code posts the journal
        journalCheckPost = InventJournalCheckPost::newPostJournal(inventJournalTable);

Sleep Function – Dynamics AX


Pauses the execution of the current thread for the specified number of milliseconds.

static void sleepExample(Args _arg) { int seconds = 10; // number of second to sleep int i; ; i = sleep(seconds*1000); print "job slept for " + int2str(i/1000) + " seconds"; pause;


Integration with X++ Objects from Visual Studio [AX 2012]



Proxies enable you to add an AOT element to your project in Visual Studio so that element can be accessed by managed code. When you add an AOT element to a project by using Application Explorer, a proxy for that element is created internally by the system.

The AOT elements that you can add to a managed code project are as follows:

  • Classes
  • Tables
  • Enums

After you add the AOT element to your managed code project, all the methods and properties are available through IntelliSense. For more information, see post “Walkthrough: Adding an X++ Object to a Visual Studio Project”

A typical scenario for adding an X++ object to managed code is when you identify managed code functionality that you want to call from X++. This functionality may already be located in managed code or the development scenario may require that it run in managed code.

For example, you might want to have your Microsoft Dynamics AX installation updated with customer data from an external system. In this case, you create an X++ job that is scheduled to run periodically and calls a managed code class method. This method could then check for the external data and then call the appropriate CustTable methods to update the customer data (see the following diagram). Because the managed code that you write runs in the Microsoft Dynamics AX process, that code will typically be in a class library project.

The following diagram provides a high-level view of the integration of X++ and managed code. The initial call from X++ to managed code can originate from either an X++ .NET interop call or from an event handler.

Proxies Managed Code Flow

Event Handlers


In the AOT, you can associate a class method with an event handler. An event handler is code that runs before the associated method runs or after the associated method has finished running. The event handler itself is also a class method, and it can be written in either X++ or managed code.

For example, you may have an X++ class method called MyClass.myMethod, and you have managed code that you want to run after the MyClass.myMethod method has finished running. In this case, you would create an event handler class and method in managed code, such as MyEventHandlerClass.PostmyMethod. This event handler contains the code that will execute after the MyClass.myMethod method is called.

When you add an event handler in Visual Studio, the system automatically creates a method that begins with either “Pre” (for event handlers that run before the method runs) or “Post” (for event handlers that run after the method completes). An event handler subscription is also automatically added to the MyClass.myMethod method. In addition, the properties of that subscription are set to point to the managed code class method. After the MyClass.myMethod method has finished running, then the MyEventHandlerClass.PostmyMethod event handler code executes. Event handlers can only be associated with a class in the AOTClasses node.

The following diagram illustrates how a managed code post-event handler is called. You can create the managed code class method and configure the event handler in the AOT to call that class method all from Visual Studio. For more information, see post “ Walkthrough: Creating an Event Handler in Visual Studio.”

Managed Code Event Handler Flow

Managed code post-event handler flow


Get every new post on this blog delivered to your Inbox.

Join other followers:

error: Content is protected !!