How to Create a Runnable Class (Job) in D365FO
Learn how to create and execute a runnable class (job) in D365 Finance & Operations. This guide covers the main() method, running classes from Visual Studio and SysClassRunner, practical code examples, and common mistakes to avoid.
How to Create a Runnable Class (Job) in D365FO#
A runnable class is one of the first things every D365FO developer learns, and for good reason. It's the quickest way to execute X++ code without creating forms, menu items, or other application objects.
Whether you're testing new code, fixing data, or experimenting with the X++ framework, a runnable class lets you write code and execute it immediately.
In this article, we'll walk through how to create one, run it, and discuss when you should (and shouldn't) use it.
What Is a Runnable Class?#
A runnable class is simply a standard X++ class that contains a public static void main(Args _args) method.
When you create a Runnable Class (Job) in Visual Studio, Microsoft generates this method for you automatically.
You could also create a normal class and add the main method yourself—the result is exactly the same.
class ExampleRunnableClass
{
public static void main(Args _args)
{
}
}The only special thing about a runnable class is that the system knows to start execution from the main method.
When Should You Use a Runnable Class?#
Runnable classes are perfect for short-lived development tasks.
Common use cases include:
- Testing new X++ code
- Learning the framework
- Validating a query
- Correcting data
- Updating records during development
- Running one-time maintenance scripts
- Verifying business logic before moving it into production code
If you find yourself repeatedly running the same runnable class, it's usually a sign that the logic belongs somewhere else, such as a batch job, service class, or menu item.
Creating a Runnable Class#
In Visual Studio:
- Right-click your project.
- Select Add > New Item.
- Choose Dynamics 365 Items.
- Select Runnable Class (Job).
- Enter a name.
- Click Add.
Visual Studio creates a class similar to this:
class RunnableClass1
{
public static void main(Args _args)
{
}
}Your First Runnable Class#
Let's verify everything works by printing a message to the Infolog.
class RunnableClass1
{
public static void main(Args _args)
{
info("Hello XppForge!");
}
}When the class runs, you'll see the message appear in the Infolog.
Running the Class#
To execute the runnable class:
- Right-click the class in Solution Explorer.
- Select Set as Startup Object.
- Build the project.
- Press Start (or F5).
Visual Studio launches your D365FO environment and executes the main method.
If the build succeeds, your code runs immediately.
Running a Runnable Class from the Browser#
You can also execute a runnable class directly from the browser by using the SysClassRunner menu item.
The URL follows this format:
https://<environment>/?cmp=<company>&mi=SysClassRunner&cls=<ClassName>Example:
https://contoso.operations.dynamics.com/?cmp=USMF&mi=SysClassRunner&cls=RunnableClass1This is useful when you need to run a one-time script without creating a menu item.
Note: Only users with sufficient permissions (typically System Administrators) can execute runnable classes this way.
Runnable Class vs Regular Class#
A common misconception is that runnable classes are a special object type.
They aren't.
| Runnable Class | Regular Class |
|---|---|
Contains a main method | Usually doesn't |
| Can be executed directly | Called by other objects |
| Great for testing and one-time scripts | Used throughout the application |
| Doesn't normally have a menu item | Often used by forms, services, or batch jobs |
Think of a runnable class as a convenient entry point into your code.
Best Practices#
A runnable class should be small and focused.
Good practices include:
- Keep the code limited to a single task.
- Wrap database updates in transactions when appropriate.
- Log meaningful messages to the Infolog.
- Validate records before updating them.
- Delete old runnable classes once they're no longer needed.
- Move reusable logic into service or helper classes instead of placing everything inside
main.
Avoid leaving dozens of old runnable classes in your project. They're useful while developing, but they shouldn't become permanent storage for business logic.
Common Mistakes#
Putting all of your logic inside main#
Instead, call separate methods or service classes.
Forgetting transactions#
If you're updating multiple records, use ttsBegin and ttsCommit where appropriate.
Using runnable classes for recurring processes#
If the process needs to run regularly, build a SysOperation framework process or batch job instead.
Leaving test code in source control#
Once you've finished testing, remove runnable classes that are no longer needed.
Conclusion#
Runnable classes are one of the fastest ways to write and execute X++ code in D365FO. They're ideal for testing ideas, validating queries, correcting data, and performing one-time administrative tasks.
Just remember that they're intended to be temporary tools. As your solution evolves, move reusable business logic into proper service classes, batch processes, or other application components, and keep runnable classes focused on quick development and maintenance tasks.
Missing something?
Suggest a topic you'd like to see covered next. The most requested topics help shape future XppForge documentation.
Sign in to suggest a topic