Creation of PO/SO through X++ code:

X++ Code to Create Purchase Order and Post the Invoice:

Following Job creates the Purchase order from code and post the invoice by making use of PurchFormLetter class.
If you don’t have demo data , please test with your input values.

static void Dev_CreatePO_and_Invoice(Args _args)
NumberSeq numberSeq;
Purchtable Purchtable;
PurchLine PurchLine;
PurchFormLetter purchFormLetter;

numberSeq = NumberSeq::newGetNumFromCode(purchParameters::numRefPurchaseOrderId().NumberSequence,true);

// Initialize Purchase order values
Purchtable.PurchId = numberSeq.num();
Purchtable.OrderAccount = ‘3000’;

if (!Purchtable.validateWrite())
throw Exception::Error;

// Initialize Purchase Line items
PurchLine.PurchId = Purchtable.PurchId;
PurchLine.ItemId = ‘B-R14’;
PurchLine.createLine(true, true, true, true, true, false);

purchFormLetter = purchFormLetter::construct(DocumentStatus::Invoice);
purchFormLetter.update(purchtable, // Purchase record Buffer
“Inv_”+purchTable.PurchId, // Invoice Number
systemdateget()); // Transaction date

if (PurchTable::find(purchTable.PurchId).DocumentStatus == DocumentStatus::Invoice)
info(strfmt(“Posted invoiced journal for purchase order %1”,purchTable.PurchId));

X++ Code to Post the Invoice of existing Sales Order:

One of the most commonly used classes in AX are the FormLetter classes. The FormLetter classes define how to post different status updates against sales orders and purchase orders.
The normal flow when a posting is being performed is that the parm tables are Populated with data, and then the update itself is performed.
Below example will shows how to Post the Invoice from existing sales order, similar logics is used to post the Invoice as we used for Purchase order above.

static void postSalesInvoice(Args _args)
// Define a classvariable according to the
// type of posting being performed
SalesFormLetter_Invoice invoice;
SalesTable salesTable;
// Select the salesTable to update
salesTable = SalesTable::find(“SO-101297″);
// Create a new object of the SalesFormLetter_Invoice
// by using the construct-method in SalesFormLetter
invoice = SalesFormLetter::construct(DocumentStatus::Invoice);
// Post the invoice
true); // Set to true to print the invoice


3 thoughts on “Creation of PO/SO through X++ code:

  1. how to create a PO with multiple lines..for my case i need to create a PO with 300 lines to check one performance issue..

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s