Lab - Create runnable classes in Visual Studio to test code

Completed

Read this first - before you start the lab!

Important

For this lab, you CANNOT sign in with your own credentials. Use the following steps to sign in to your lab environment with the correct credentials.

  1. Ensure that you are signed in to Microsoft Learn.

  2. Select Launch VM mode or Sign in to launch VM mode in this unit.

  3. In the Resources tab on the lab side bar, select the T icon next to Password in the MININT box, to have the administrator password for the Virtual Machine entered for you.

    Screenshot of the administrator password.

  4. Select Enter.

  5. Microsoft Edge will open. Wait for it to navigate to the Sign in page for finance and operations.

  6. On the Microsoft Sign in page in finance and operations, place your mouse cursor into the Username field.

  7. On the Resources tab of the lab side bar, below the Azure portal heading, select the T icon next to Username, then press Enter.

    Screenshot of the Username field and the Sign in dialog box that appears.

  8. Your mouse cursor will now be in the Password page.

  9. On the Resources tab of the lab side bar, below the Azure portal heading, select the T icon next to select Password, then press Enter.

    Screenshot of the Password field the Enter password dialog box that appears.

  10. Don't stay signed in or store the password on the virtual machine.

  11. Select Accept in the Permissions requested page.

    Screenshot of the Permissions requested page.

  12. To see the lab instructions, select the Instructions tab on the lab side bar.

You can now begin your work on this lab.

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

  1. Minimize the Microsoft Edge window.
  2. Right-click the Visual Studio shortcut and select Run as administrator.
  3. Select Continue without code.
  4. Open the File menu and select New > Project.
  5. Ensure that Finance Operations is selected in the left pane, and then select Next.
  6. In the Configure your new project dialog box, enter RunnableClass in the Project name field.
  7. Select Create.
  8. To ensure the correct applications suite is referenced, select Dynamics 365 in the Extensions menu.
  9. Select Model Management > Update model parameters
  10. Select the FleetManagement model from the Model name dropdown list.
  11. Select Next, then ensure ApplicationSuite (not ApplicationSuiteFormAdaptor) is selected in the Select referenced packages drop-down list.
  12. Select Next until the form closes.
  13. Select Dynamics 365 in the Extensions menu again.
  14. Select Options.
  15. Under the Dynamics 365 node on the left pane, select Projects.
  16. Ensure the check boxes for Organize projects by element type and Synchronize database on build for newly created project are selected.
  17. Select OK.

Create a runnable class

  1. In the Solution Explorer window, right-click the RunnableClass project.

  2. Select Add > New Item.

  3. On the left pane, select Code under Dynamics 365 Items.

  4. On the middle pane, select Runnable class (Job).

  5. Enter SampleClass in the Name field.

  6. Select Add.

  7. The SampleClass runnable class will now be open in the element designer window.

  8. 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 since 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.");
        }
    }
    
  9. Save the runnable class.

  10. Right-click the RunnableClass project and select Build.

  11. Right-click the Sample Class runnable class and select Set as Startup Object.

  12. From the toolbar, select the option Debug > Start Without Debugging to run the class.

  13. The class runner will appear in a browser window after a couple of minutes, when the code executes, and an Infolog appear with Your record has been inserted..

Close the lab environment

  1. Select Done in the Instructions pane in the lab side bar.
  2. In the Lab is complete window, select Continue, and then select Leave to return to the next unit in the module.