Studio
2023.10
false
Studio User Guide
Last updated Jul 15, 2024

Invoking Code Source File

Use this workflow to invoke a coded source file within a low-code workflow. In this example, we create a class called TimeSpanHelper in the TimeSpanHelper.cs coded source file. This class generates a random time span based on the specified bounds. To integrate this class in a low-code workflow, we use an Invoke Workflow File activity, followed by other low-code activities, depending on the use case.
  1. Create a coded source file. For this example, name it TimeSpanHelper.cs.
  2. In the coded source file, create a public class named TimeSpanHelper.
    1. Inside the TimeSpanHelper class, declare a private static Random object named _random to generate random values.
      private static Random _random = new Random();private static Random _random = new Random();
    2. Declare a private static object named _lockObj to secure the thread, while working with multiple threads at the same time..
      private static object _lockObj = new object();private static object _lockObj = new object();
    3. Implement a public static method called GetRandomTimeSpanBetween that takes two integer parameters: lowerBoundMs and upperBoundMs.
      public static TimeSpan GetRandomTimeSpanBetween(int lowerBoundMs, int upperBoundMs)
          {public static TimeSpan GetRandomTimeSpanBetween(int lowerBoundMs, int upperBoundMs)
          {
    4. Use the lock statement with _lockObj to secure this thread, while working with multiple threads at the same time.
      lock (_lockObj)
            {lock (_lockObj)
            {
    5. Within the lock block, generate a random integer value using _random.Next and pass lowerBoundMs and upperBoundMs as the arguments.
      var ms = _random.Next(lowerBoundMs, upperBoundMs);var ms = _random.Next(lowerBoundMs, upperBoundMs);
    6. Convert the random value that was generated to a TimeSpan object using TimeSpan.FromMilliseconds.
      return TimeSpan.FromMilliseconds(ms);return TimeSpan.FromMilliseconds(ms);
    7. Return the generated TimeSpan value from the method.
  3. Create a low-code workflow. For this example, name it WorkflowUsingCodeSourceFile.
  4. Add a Log Message activity to output a random timespan between two and four seconds.
  5. In the Message field, type the name of the project, call the coded source file, and then call the GetRandomTimeSpanBetween method.
    In the example below, CodedWorkflowInteroperability is the name of the project.
    CodedWorkflowInteroperability.TimeSpanHelper.GetRandomTimeSpanBetween(2000, 4000)CodedWorkflowInteroperability.TimeSpanHelper.GetRandomTimeSpanBetween(2000, 4000)

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.