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));

Change the documentstatus to packingSlip , if you want to post packing slip.
Enjoy ....Invoicing through Code.

TextBuffer Class to replace the special characters in string.

TextBuffer class can be used to replace the text/characters in the string.
Usually its used to replace the special character with escape sequence so that string(data with special character) can be inserted into the database.

Following job illustrates how to achieve this.

static void Dev_ReplaceTxt(Args _args)

TextBuffer buffer = new TextBuffer();
Str message;

message = " hi hello's how r u's ";
message += " How r u doing's wujer's * ? ' what ur mot's anbej's";

buffer.replace("[*?']","\\'"); // replace special character with escape sequence


Code to Display Expiry date Message at Status Bar

Following job illustrates how you can display custom message in the status bar next to alerts. ( Mean , At bottom where you will find company,layer,currency info).

static void Dev_setStatusLineText(Args _args)

str msg;

msg = "Expiry date :"+date2str(xinfo::expireDate(),123,2,4,2,4,2);

// Force the text to be shown


X++ code to write data to Event viewer

Usually it will be difficult to monitor/debug Batch Jobs running on server . For e.g if something goes wrong than we can't find out why.
So we can make use of Eventviewer instead of infolog and monitor the status by checking the eventviewr .
Following Job illustrates how to write to event viewer.

static void Dev_Write2EventViewer(Args _args)

System.Diagnostics.EventLog eventlog;
Str logSource;
Str logName;

logSource = "Dynamics Ax";
logName = "Dynamics Ax Log";

// check if the log already exists
// create new log
System.Diagnostics.EventLog::CreateEventSource(logSource, logName);

eventlog = new System.Diagnostics.EventLog();

eventlog.WriteEntry("Info Message");
eventlog.WriteEntry("Warning Message" , System.Diagnostics.EventLogEntryType::Warning);
eventlog.WriteEntry("Error! Please check the stack trace below. \n\n" +
con2str(xSession::xppCallStack()), System.Diagnostics.EventLogEntryType::Error);

info("Check the event viewer!");


Enjoy ....!!!! Batch Jobs...

How to Debugg Form Controls in Ax

As you all know X++ debugger won't work , when we place breakpoints in form controls. So we all end-up in writing the breakpoint statement in form control method.

So have a look
Debug Form Controls in Ax

Enjoy Debugging !!!!

DAX Form Tool - Get Form Details at One Place

As you all know in ax whenever we want to know form detail, always we used to do..
1. Right click on form and then click on Setup and then we get the AOT->Form path.
2. To check the datasource ,like which table its using and joined to which datasource.

I have developed tool called as Dev_FormTool which will help you to get all form details in one place.

Have a look ....