Getting Started
3 snippetsBasic X++ syntax for Dynamics 365
Hello World
static void HelloWorld(Args _args)
{
info("Hello, World!");
}Job Structure
static void MyJob(Args _args)
{
// Variable declarations
str myString;
int myInt;
// Logic
myString = "X++ Programming";
myInt = 100;
info(strFmt("%1 - %2", myString, myInt));
}Comments
// Single line comment
/* Multi-line
comment */
info("Code"); // Inline commentVariables & Data Types
4 snippetsData type declarations
Primitive Types
int myInt = 100;
real myReal = 3.14;
str myString = "Hello";
boolean myBool = true;
date myDate = today();
utcdatetime myDateTime = DateTimeUtil::utcNow();Extended Data Types
CustAccount custAccount = "US-001";
ItemId itemId = "A0001";
Amount amount = 1500.00;
Description description = "Sample item";Constants
const str COMPANY_NAME = "Contoso";
const int MAX_RECORDS = 1000;
const real TAX_RATE = 0.0825;Enums
NoYes isActive = NoYes::Yes;
CustVendACType accountType = CustVendACType::Cust;
if (isActive == NoYes::Yes)
{
info("Active");
}Control Flow
4 snippetsConditional statements and loops
If-Else
if (amount > 1000)
{
info("High value");
}
else if (amount > 500)
{
info("Medium value");
}
else
{
info("Low value");
}Switch Statement
switch (status)
{
case CustStatus::Open:
info("Open");
break;
case CustStatus::Closed:
info("Closed");
break;
default:
info("Unknown");
break;
}While Loop
int counter = 0;
while (counter < 10)
{
info(int2Str(counter));
counter++;
}For Loop
int i;
for (i = 1; i <= 100; i++)
{
info(int2Str(i));
}Tired of looking up syntax?
DocuWriter.ai generates documentation and explains code using AI.
Table Operations
5 snippetsCRUD operations on tables
Select Single Record
CustTable custTable;
select firstOnly custTable
where custTable.AccountNum == "US-001";
if (custTable)
{
info(custTable.Name);
}Select Multiple Records
CustTable custTable;
while select custTable
where custTable.CustGroup == "10"
{
info(strFmt("%1 - %2", custTable.AccountNum, custTable.Name));
}Insert Record
CustTable custTable;
ttsbegin;
custTable.AccountNum = "US-999";
custTable.Name = "New Customer";
custTable.CustGroup = "10";
custTable.insert();
ttscommit;Update Record
CustTable custTable;
ttsbegin;
select forUpdate custTable
where custTable.AccountNum == "US-001";
custTable.CreditMax = 50000;
custTable.update();
ttscommit;Delete Record
CustTable custTable;
ttsbegin;
select forUpdate custTable
where custTable.AccountNum == "US-999";
if (custTable)
{
custTable.delete();
}
ttscommit;Classes & Methods
3 snippetsObject-oriented programming
Class Definition
class MyCalculator
{
real taxRate;
public void new(real _taxRate = 0.0825)
{
taxRate = _taxRate;
}
public real calculateTotal(real amount)
{
return amount * (1 + taxRate);
}
}Instantiate & Use
MyCalculator calc;
real total;
calc = new MyCalculator(0.10);
total = calc.calculateTotal(100);
info(strFmt("Total: %1", num2Str(total, 2, 2, 1, 1)));Static Method
class MyUtility
{
public static str formatDate(date _date)
{
return date2Str(_date, 123, DateDay::Digits2,
DateSeparator::Slash,
DateMonth::Digits2,
DateYear::Digits4);
}
}
// Usage
str formattedDate = MyUtility::formatDate(today());Query Objects
2 snippetsBuilding dynamic queries
Simple Query
Query query;
QueryBuildDataSource qbds;
QueryRun queryRun;
CustTable custTable;
query = new Query();
qbds = query.addDataSource(tableNum(CustTable));
qbds.addRange(fieldNum(CustTable, CustGroup)).value("10");
queryRun = new QueryRun(query);
while (queryRun.next())
{
custTable = queryRun.get(tableNum(CustTable));
info(custTable.Name);
}Join Query
Query query;
QueryBuildDataSource qbdsCust;
QueryBuildDataSource qbdsTrans;
query = new Query();
qbdsCust = query.addDataSource(tableNum(CustTable));
qbdsTrans = qbdsCust.addDataSource(tableNum(CustTrans));
qbdsTrans.relations(true);
qbdsTrans.joinMode(JoinMode::InnerJoin);Form Methods
3 snippetsCommon form method overrides
Form Init
public void init()
{
super();
// Custom initialization
element.lock();
}Data Source Init
public void init()
{
super();
// Filter records
this.query().dataSourceTable(tableNum(CustTable))
.addRange(fieldNum(CustTable, CustGroup)).value("10");
}Field Modified
public void modified()
{
super();
// Recalculate total when quantity changes
CustTable.AmountTotal = CustTable.Quantity * CustTable.Price;
}String Functions
3 snippetsString manipulation utilities
String Operations
str text1 = "Hello";
str text2 = "World";
str combined = text1 + " " + text2;
str upper = strUpr(combined);
str lower = strLwr(combined);
int length = strLen(combined);String Formatting
str formatted;
int qty = 10;
real price = 19.99;
formatted = strFmt("Qty: %1, Price: %2", qty, num2Str(price, 2, 2, 1, 1));
info(formatted);Substring
str fullText = "Hello World";
str sub = subStr(fullText, 1, 5); // "Hello"
int pos = strScan(fullText, "World", 1, strLen(fullText)); // 7