AI Generate X++ docs instantly

X++ Cheat Sheet

Quick reference guide with copy-paste ready code snippets

Try DocuWriter Free

Getting Started

3 snippets

Basic 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 comment

Variables & Data Types

4 snippets

Data 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 snippets

Conditional 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.

Try Free

Table Operations

5 snippets

CRUD 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 snippets

Object-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 snippets

Building 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 snippets

Common 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 snippets

String 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

More Cheat Sheets

FAQ

Frequently asked questions

What is a X++ cheat sheet?

A X++ cheat sheet is a quick reference guide containing the most commonly used syntax, functions, and patterns in X++. It helps developers quickly look up syntax without searching through documentation.

How do I learn X++ quickly?

Start with the basics: variables, control flow, and functions. Use this cheat sheet as a reference while practicing. For faster learning, try DocuWriter.ai to automatically explain code and generate documentation as you learn.

What are the most important X++ concepts?

Key X++ concepts include variables and data types, control flow (if/else, loops), functions, error handling, and working with data structures like arrays and objects/dictionaries.

How can I document my X++ code?

Use inline comments for complex logic, docstrings for functions and classes, and README files for projects. DocuWriter.ai can automatically generate professional documentation from your X++ code using AI.

Code Conversion Tools

Convert X++ to Other Languages

Easily translate your X++ code to other programming languages with our AI-powered converters

Related resources

Stop memorizing. Start shipping.

Generate X++ Docs with AI

DocuWriter.ai automatically generates comments, docstrings, and README files for your code.

Auto-generate comments
Create README files
Explain complex code
API documentation
Start Free - No Credit Card

Join 33,700+ developers saving hours every week