MICROSOFT DYNAMICS AX 2012
Programming Language
Introduction to X++
• The programming language that is used to
create an application in Microsoft Dynamics AX
• Not case-sensitive
Jobs
• Stand-alone block of code in Microsoft Dynamics
AX that can be run from the X++ editor
• Frequently used to test a piece of business logic
• Easily executed from within the MorphX
development environment
Variables
• Standard type: string, integer, boolean, etc.
• EDT & base enum.
• Table
• Initialization:// Standard type.
str strExample;
int intExample;
boolean boolEample;
// EDT & base enum.
SalesId salesIdExample;
SalesType salesType;
// Table.
SalesTable salesTable;
Expressions
• Arithmetic • Relational
operators operators
–x = y + z – X >= y
• Logical – x == y
operators – x != y
– !x • String
– x && y – “xyz” + “xyz”
– x || y – ‘xyz’
Conditional Statements
• If..then..else
if(name == 'Chris John') if(name != '')
{ {
// Do something. // Do something.
} }
else ||
{ if(name)
// Do something. {
} // Do something.
}
age = (name == 'Chris John') ? 20 : 30;
Conditional Statements (cont.)
• Switch..case
str s = 'test';
switch(s)
{
case 'test':
// Do something.
break;
default:
// Do something.
}
Looping Statements
• For • While • Do..while
for(i=0; i<=4; i++) while(i<=4) do
{ {
{
// Do something. // Do something.
// Do something. }
}
while(i<=4);
i++;
}
Looping Statements (cont.)
• Break • Continue
int i; int i;
while(i<=5) while(i<=5)
{ {
if(i>3) i++;
break;
if(i mod 2)
info(strFmt('%1', i)); continue;
i++; info(strFmt('%1', i));
} }
Classes
• Class declaration
• Inheritance
class Child extends Parent()
{
}
• Parent methods
super();
Classes (cont.)
• Static methods
static public void myStaticMethod()
{
}
myClass::myStaticMethod();
• Instantiating a Class
myClass myClass;
;
myClass = new myClass();
myClass.myMethod();
Functions
• public void run()
{
// Do something.
}
• private boolean isValid()
{
// Do something.
return true;
}
Communication Tools
• Boxes
– Box::info("Main Text", "Title", "This is the help text");
– Box::yesNo(“Choose yes or no", DialogButton::Yes , "Title", "This is the help text");
– DialogButton dialogButton;
dialogButton = Box::yesNo("Choose Yes or No", DialogButton::Yes, "Yes No Box Example");
if(dialogButton == DialogButton::Yes)
{
// Do something.
}
else if(dialogButton == DialogButton::No)
{
// Do something.
}
Communication Tools (cont.)
• Infolog
– info("Info message");
– warning("Warning message");
– error("Error message");
– throw error("Error message");
– setPrefix("Title");
Accessing The Database
• Embedded query string
• Table buffer
SalesTable salesTableBuffer;
• Select statement
select salesTableBuffer
where salesTableBuffer.SalesId == 'SO-1300';
-----------------------------------------------------------------
select firstonly salesTableBuffer
order by SalesId desc;
-----------------------------------------------------------------
while select salesTableBuffer
where salesTableBuffer.SalesStatus == SalesStatus::BackOrder
{
// Do something.
}
Accessing The Database (cont.)
• Insert statement
CustTable custTable;
;
custTable.AccountNum = "1234";
custTable.Currency = "USD";
custTable.insert();
Accessing The Database (cont.)
• Update statement
SalesTable salesTable;
;
ttsbegin;
while select forUpdate salesTable
where salesTable.CustAccount == "2001"
{
salesTable.SalesName = "New Enterprises";
salesTable.update();
}
ttscommit;
-----------------------------------------------------------------------------------------------
SalesTable salesTable;
;
update_recordset salesTable
setting salesName = "New Enterprises"
where salesTable.custAccount == "2001";
Accessing The Database (cont.)
• Delete statement
CustTable custTable;
;
ttsbegin;
select forUpdate custTable
where custTable.accountnum == "2032";
custTable.delete();
ttscommit;
-----------------------------------------------------------------------------------------------
CustTable custTable;
;
delete_from custTable
where custTable.Currency == "ABC";
Query In X++
• Executing a query
– Two important classes when executing a query
• Query()
• QueryRun()
– The Query() class does not "fetch" records, this is
accomplished by using the QueryRun() class
Query In X++ (cont.)
• Executing a query
– Example
Query query = new Query (QueryStr(SalesUpdate));
// Use the query to build a queryRun object
QueryRun queryRun = new QueryRun (query);
SalesTable salesTable;
// Traverse some records...
while (queryRun.next())
{
salesTable = queryRun.get(tableNum(SalesTable));
// Do something
}