studio
2023.10
false
Studio User Guide
Last updated Nov 4, 2024

Quickstart guide

This quickstart guide helps you embark on the journey of working with coded automations. You will learn how to create coded automations following a tutorial that shows how to create a coded workflow that generates random numbers between 1-100, and performs addition or subtract operations based on whether the numbers are even or odd.

What are coded automations?

Coded automations are automations created using code, instead of drag-and-drop activities. When developing coded automations, you can use UiPath services (equal to activity packages), APIs (similar to activities), external .NET NuGet packages, and your custom C# classes in UiPath Studio. This makes it easy to integrate with external systems, databases, APIs, and other services. Coded automations can be of three types:
  1. Coded workflows - used for designing workflows in code.
  2. Coded test cases - used for designing test cases.
  3. Code source files - used for creating code that you can later call in other coded file types.

Visit the Introduction for coded automations to learn more.

Scenario

In this tutorial we use the RandomNumber API to generate random decimal numbers within the specified range. We then check if the generated numbers are even, using a custom method named IsEven, and perform addition or subtraction based on that condition.

Create the coded workflow

Create the coded workflow within your Studio project, and install the necessary dependencies.
  1. Install Testing.Activities 23.10, because the scenario involves using the RandomNumber API from the Testing service.
  2. Create a coded workflow by selecting New, and then Coded Workflow from the File group.
    Coded automations are structured with namespaces, helper classes, and entry point methods. The base class, CodedWorkflow, is used for both coded workflows and test cases, and it provides access to necessary interfaces and services. The entry point method, named Execute(), is crucial for running these automations and can be customized with input and output arguments.


Design the coded workflow

Write the code in the Studio IDE that consists of a dedicated code editor, file tabs, and breadcrumbs for easy navigation.
  1. In the coded workflow, but outside of the Execute() method, create a custom method named IsEven.
    This method returns a boolean value, that represents if the decimal variable inputted is an even number or not.
    Check out the code example below:
    private bool IsEven(decimal number)
            {
               // Check if a decimal number is even
            return (number % 2 == 0);
            } private bool IsEven(decimal number)
            {
               // Check if a decimal number is even
            return (number % 2 == 0);
            }
    Tip: If you don't want to create a custom method in a separate code source file, then you can create it within the coded workflow or coded test case, but outside the Execute() method.
  2. Inside the Execute method use the RandomNumber API to create two decimal variables with a random value, and print them out in the console using Console.WriteLine.
    Check out the code example below:
    // Generate random numbers within a specified range (e.g., 1 to 100)
            decimal num1 = testing.RandomNumber(1, 100);
            decimal num2 = testing.RandomNumber(1, 100);
                
            Console.WriteLine($"Generated numbers: {num1} and {num2}");// Generate random numbers within a specified range (e.g., 1 to 100)
            decimal num1 = testing.RandomNumber(1, 100);
            decimal num2 = testing.RandomNumber(1, 100);
                
            Console.WriteLine($"Generated numbers: {num1} and {num2}");
    Note: You write coded automations like you would write code, using APIs available through UiPath services, and other custom C# classes, or .NET class libraries available from the nuget.org feed. In coded automations, services are equivalent to activity packages used in low-code automations. These services, such as System.Activities, UiAutomation.Activities, and Testing.Activities, come with APIs that you can use for building coded automations.
  3. Create an If statement, with the condition that if both numbers are even, then the automation should add them together. The Else clause should subtract the numbers, if at least one of them is odd. Both results should be printed out in the console using Console.WriteLine.
    Check out the code example below:
    if (IsEven(num1) && IsEven(num2))
            {
                // Both numbers are even, so add them together
                decimal sum = num1 + num2;
                Console.WriteLine($"Both numbers are even. Sum: {sum}");
            }
            else
            {
                // At least one number is odd, so subtract them
                decimal difference = num1 - num2;
                Console.WriteLine($"At least one number is odd. Difference: {difference}");
            } if (IsEven(num1) && IsEven(num2))
            {
                // Both numbers are even, so add them together
                decimal sum = num1 + num2;
                Console.WriteLine($"Both numbers are even. Sum: {sum}");
            }
            else
            {
                // At least one number is odd, so subtract them
                decimal difference = num1 - num2;
                Console.WriteLine($"At least one number is odd. Difference: {difference}");
            }


Manage the coded workflow process

After you create and design a coded workflow, you can validate it, using the Workflow Analyzer, debug it, run it, and then publish it to Orchestrator.
  1. In the Design ribbon, click Analyze File and then Validate File to check the coded workflow file for C# compiler errors.


    Visit About Workflow Analyzer to read about the Workflow Analyzer.
  2. In the Debug ribbon, click Debug File to debug the coded workflow file and check for inconsistencies at runtime.


  3. Either in the Debug or Design ribbon, click Debug File then Run File, to run the coded workflow file that you created.
  4. Select Publish in the Design ribbon.
    The Publish Process dialog appears.
  5. In the Publish options tab, select where to publish the project. The available options depend on the type of project you are publishing.
    For Processes:
    • Orchestrator Tenant Processes Feed, Orchestrator Personal Workspace Feed
    • Assistant (Robot Defaults) - the default package location for the Robot and Assistant, C:\ProgramData\UiPath\Packages. Projects published here automatically appear in the Assistant. The option is not available if Studio is connected to Orchestrator. These options are available if Studio is connected to Orchestrator.
    • Custom - either a custom NuGet feed URL or local folder. Adding an API Key is optional.
  6. Click Publish.
    A NUPKG file is created and uploaded to Orchestrator, the custom NuGet feed, or saved in the local directory.

Sample project

To follow the steps of this tutorial and try it out yourself, you can download the following sample project: First Coded Workflow.

Next steps

Visit the resources below to enhance your knowledge on coded automations:

Was this page helpful?

Get The Help You Need
Learning RPA - Automation Courses
UiPath Community Forum
Uipath Logo White
Trust and Security
© 2005-2024 UiPath. All rights reserved.