Sometimes a user may receive the message of "An unbalanced X++ ttsbegin/ttscommit pair has been been detected"
In this tutorial we are going to create a simple job in Dynamics 365 Finance and Operations. This is one of the simplest pieces of code you can write. It's great for testing other larger pieces of functionality. To kick this off we need to create a project in Visual Studio and add a class to this project.
Lets start by first opening Microsoft Visual Studio. I'm using version 2015 which came with my development environment.
Once we are in Visual Studio lets create a new project. Lets navigate to the File and New Option. In the sub menu we are going to create a new Project. There are lots of different options to choose from, we're looking for Unified Operations.
We have lots of different templates to choose from. We are going to choose Dynamics 365 in the templates on the right hand side. In the project types which are displayed we have a number of different project types to choose from. For this tutorial we are going to choose the Unified Operations project type.
In the Name option, let's give our project a name. I'm going to enter D365Tutorials as my project name. Make sure the Create directory for solution option is selected.
5 Ok first steps completed. We have created an empty project. Notice two other details near the project name D365Tutorial (USR) [FeelyConsultants]. USR is the layer the project is created in. FeelyConsultants is the model which the project is part off.
6 An empty project is not very exciting. Lets add some code to this project. Right click and project name and in the project name Click Add.
7 Click New Item. If you had other code classes you wanted to import we would use the existing item. In this case the code is new so let's choose the New Item option
8 In the window which pops up, click on the Code option. This will narrow down the items we can choose from to Code objects
9 Now we have code only options. Choose Runnable Class. Classes are blocks of code that contain data and methods. When developing for Finance and Operations applications you will use the X++ language to create new classes.
10 Give your class a name and Click Add. I am going to call this class RunnableClassCust. I'm going to write some customer specific code in this class
10b Click
11 In the empty class. I am going to paste some code i have already prepared. There is a lot going on this code so you should have some basic code knowledge. The first line changes the company the code will run in. As I am using the Contoso application
12 I am going to use the USRT application. NextI am going to loop through all the customers in the CustTable. For each entry in the customer table I am going to print the customer's account number and name in a string. Let give it a go !
13 Before we can run our project we need to tell the compiler which code to run. We need a start up object. Right Click the class RunnableClassCust
14 In the menu which is displayed. Click Set as Start Up Object. You are now ready to go.
15 Click Start Without Debugging. I tend to use this option for speed. If you want to debug , you can choose this option too.
16 When the application is started a browser session is kicked off. In this window you can see two important pieces of information. First the Class name has completed. This is the name of our class. Also you can see 30 messages have been displayed.
17 These are our 30 customers in the USRT company. Notice that we are displaying the customer number and name.
18 That's it. You're done you have created your first project and you have created a runnable class. Good work.
X++ Code
class RunnableClassCust
{
/// <summary>
/// Runs the class with the specified arguments.
/// </summary>
/// <param name = "_args">The specified arguments.</param>
public static void main(Args _args)
{
CustTable custTable;
//Assume that you are running in company 'DAT'.
changeCompany('USRT') //Default company is now 'USRT'
{
custTable = null;
while select custTable
{
info(strFmt("%1 , % 2",custTable.AccountNum,custTable.name()));
}
}
}
}