Exercise - Create runnable classes in Visual Studio to test code
Scenario
As a finance and operations apps developer, you can use runnable classes to test your code. In this lab, you will create a new project in Visual Studio, write a runnable class, then run the code within the application to ensure it is working as expected.
Create a new project
- Minimize the Microsoft Edge window.
- Right-click the Visual Studio shortcut and select Run as administrator.
- Select Continue without code.
- Open the File menu and select New > Project.
- Ensure that Finance Operations is selected in the left pane, and then select Next.
- In the Configure your new project dialog box, enter RunnableClass in the Project name field.
- Select Create.
- To ensure the correct applications suite is referenced, select Dynamics 365 in the Extensions menu, followed by Model Management > Update model parameters
- Select the FleetManagement model from the Model name dropdown list.
- Select Next, then ensure ApplicationSuite (not ApplicationSuiteFormAdaptor) is selected in the Select referenced packages drop-down list.
- Select Next until the form closes.
- Select Dynamics 365 in the Extensions menu again, and then select Options.
- Under the Dynamics 365 node on the left pane, select Projects.
- Ensure the check boxes for Organize projects by element type and Synchronize database on build for newly created project are selected.
- Select OK.
Create a runnable class
In the Solution Explorer window, right-click the RunnableClass project on the right of the page.
Select Add > Class.
On the left pane, select Code under Dynamics 365 Items.
On the middle pane, select Runnable class (Job).
Enter SampleClass in the Name field.
Select Add.
The SampleClass runnable class will now be open in the element designer window. It may take a few seconds to open.
Remove the code present in Element designer window and then enter the following code line by line. Notice that the main method is already added for you because you selected a runnable class.
class SampleClass { /// <summary> /// Runs the class with the specified arguments. /// </summary> /// >param name = "_args">The specified arguments.</param> Public static void main(Args _args) { FMCustomer fmcustomers; fmcustomers.FirstName = "Jane"; fmcustomers.LastName = "Doe"; fmcustomers.Email = "jane.doe@contoso.com"; fmcustomers.insert(); Info("Your record has been inserted."); } }Save the runnable class.
Right-click the RunnableClass project and select Build.
When the build is completed, right-click the Sample Class runnable class and select Set as Startup Object.
From the toolbar, select the option Debug > Start Without Debugging to run the class.
The class runner appears in a browser window after a couple of minutes, when the code executes, and an Infolog appears with the text Your record has been inserted..