Thursday 1 October 2020

Create a runnable class in Dynamics 365 for operations

 

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.

Step 1 image

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. 

Step 2 image

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. 

Step 3 image

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. 

Step 4 image

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.

Step 5 image

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.

Step 6 image

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 

Step 7 image

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 

Step 8 image

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.
 

Step 9 image

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

Step 10 image

10b Click

Step 10b image

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

Step 11 image

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 ! 

Step 12 image

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

Step 13 image

14 In the menu which is displayed. Click Set as Start Up Object. You are now ready to go.

Step 14 image

15 Click Start Without Debugging. I tend to use this option for speed. If you want to debug , you can choose this option too.

Step 15 image

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.

Step 16 image

17 These are our 30 customers in the USRT company. Notice that we are displaying the customer number and name.

Step 17 image

18 That's it. You're done you have created your first project and you have created a runnable class. Good work.

Step 18 image

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()));
}
}
}

}

1 comment: