Utilizing External Code
Utilizing External Code
Slide 1
1|Page
Advanced RPA Design and Development v4.0 June 2022
Slide 2
Lesson 7
2|Page
Advanced RPA Design and Development v4.0 June 2022
Slide 3
Agenda
3|Page
Advanced RPA Design and Development v4.0 June 2022
Slide 4
Learning Objectives
4|Page
Advanced RPA Design and Development v4.0 June 2022
Slide 5
5|Page
Advanced RPA Design and Development v4.0 June 2022
Slide 6
Programming
Languages in Studio
• Introduction to Programming Languages in Studio
6|Page
Advanced RPA Design and Development v4.0 June 2022
Slide 7
Introduction to Programming
Languages in Studio
Studio can be used with several programming languages such as:
PowerShell C#
VBA VB.NET
External code written in these programming languages can be executed from the
Studio platform. How to specify exactly what code to be executed and what
arguments to be passed to and through which activities are discussed in further
slides.
7|Page
Advanced RPA Design and Development v4.0 June 2022
Slide 8
8|Page
Advanced RPA Design and Development v4.0 June 2022
Slide 9
Invoke Method activity is used to call a method that is outside the standard built-in activities of Studio,
such as in a DLL file.
Invoke Method Activity
Invoke Method activity is used to call a method that is outside the standard built-in
activities of Studio, such as in a DLL (Dynamic Link Library) file.
• It is used to call a specific method in a class
• The activity calls a public method of a specified type or an object
• It acts on either Target Type or Target Object
9|Page
Advanced RPA Design and Development v4.0 June 2022
Slide 10
Properties of
Invoke Method Activity
The properties of Invoke Method activity are:
GenericTypeArguments Parameters
Sets the generic type arguments Enters the list of parameters to be
passed to the method
MethodName Result:
Inserts the method name to be Enters a variable to store the
invoked output
Private TargetObject
Specifies whether this activity will Passes the object whose method
be private or public is invoked
RunAsynchronously:
TargetType
Specifies whether this activity will
Specifies the data type of the
execute synchronously or
target
asynchronously
10 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 11
Types of Methods
in Invoke Method Activity
Invoke Method activity can be configured with the following methods:
• Static Method:
• Used for a class defined as stati
• There is no need to define the object. Thus, the Static method can be
called directly. (TargetObject field in the Invoke Method activity is left
empty)
• Target type is used. (Example: System.Console)
11 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 12
1 Complete workflow
Parameters of the
Invoke Method activity 2
An example of Static Method in the Invoke Method activity is shown on the slide in
which local time is converted to a specific time zone using an Invoke Method activity.
Following are the steps to build the workflow:
Step 1: Insert an Assign activity in the Designer panel. In its To field, create a
DateTime variable named in_LocalDateTime. In its Value field, enter Now.
Step 2: Insert a second Assign activity below the previous Assign activity. In its To
field, create a TimeZoneInfo variable (select the Browse for Types option from the
Variable Type dropdown and search for TimeZoneInfo class) named
in_destTimeZone. In its Value field, enter
TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")
Step 3: Insert an Invoke Method activity below the second Assign activity and pass
the values as below:
a. In the TargetType field, select System.TimeZoneInfo
b. In the MethodName field, enter ConvertTime
Step 4: In the Parameters property of the Invoke Method activity, enter
in_LocalDateTime and in_destTimeZone of direction In and type DateTime and
TimeZoneInfo respectively (screenshot 2).
Step 5: Run the workflow to get the Output (screenshot 3).
12 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 13
Step 1: Insert a Build Data Table activity in the Designer panel. Build a DataTable
with two columns. First column as the list of places and the second column as the list
of item IDs. In its Output property, press Ctrl + K to create a DataTable variable
named in_dt_cityListOne.
Step 2: Insert a second Build DataTable activity below the previous Build Data
Table activity. Build a DataTable with two columns. First column as the list of places
and second column as the list of item IDs. In its Output property, press Ctrl + K to
create a DataTable variable named in_dt_cityListTwo.
Step 3: Insert an Invoke Method activity below the second Build Data Table activity
and pass the values as below:
a. In the TargetType field, enter Null.
b. In the TargetObject field, enter in_dt_cityListTwo.
c. In the MethodName field, enter Merge.
Step 4: In the Parameters property of the Invoke Method activity, enter
in_dt_cityListOne of direction In and type DataTable (screenshot 2).
Step 5: Insert an Output Data Table activity below the Invoke Method activity. In its
DataTable property, enter in_dt_cityListTwo. In its Text property, press Ctrl + K and
enter Result.
Step 5: Insert a Message Box activity below the Output Data Table activity. In the
Text property, press Ctrl + K to create a new variable named Result.
Step 6: Run the workflow to get the output (screenshot 3).
13 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 14
The steps to call a method from a DLL file using Invoke Method Activity are:
14 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 15
15 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 16
Introduction to
Invoke Code Activity
Invoke Code activity can be used to synchronously invoke VB.NET or CSharp (C#) code and write
custom code for some steps in the process.
Invoke Code activity is used to synchronously invoke VB.NET and CSharp (C#)
code. It helps the user to write custom code as required within steps in a process,
simplify data manipulation procedures, and reduce the number of Assign and
Invoke Method activities, by replacing several activities with a single Invoke Code
activity.
Properties:
• Arguments: The parameters that can be passed to the code to be invoked
• Code: The code that is to be invoked. This field supports only strings
and String variables
• Language: The drop-down menu specifies the language in which the invoked
code is written. The available options are VB.NET and CSharp
16 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 17
Invoke Code
Activity with VB.NET and C#
Invoke Code Activity with VB.NET: VB.NET code is used inside the Invoke Code
activity.
• Example: The Input Dialog activity takes a number from the user as an input. A
VB.NET code for adding the number with three predefined numbers is invoked
using the activity
Invoke Code Activity with C#: C# code is used inside the Invoke Code activity
• Example: A C# code for adding two numbers is invoked using the activity
17 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 18
18 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 19
Introduction to
Invoke PowerShell Activity
Windows PowerShell is a task automation and configuration management framework by Microsoft.
The screenshot shows the Invoke PowerShell activity within a workflow. The
workflow displays a list of current system processes in the output panel upon
execution.
19 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 20
Properties of
Invoke PowerShell Activity
The properties of Invoke PowerShell activity are:
CommandText Parameters
Passes the PowerShell commands Enters a dictionary of PowerShell
to be executed command parameters
PowerShellVariables Output
Passes variables to be used in the Stores the TypeArgument object
current session of the command returned after command execution
20 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 21
Practice Exercise
Build a workflow using Invoke PowerShell activity that creates two text files in the
project folder and moves both the files to a new folder. The workflow should:
Process Overview
• START
• Store a PowerShell script in a text file in the project folder that creates two text
files and stores some text in them
• Use a Read Text File activity to read the text file
• Use an Invoke PowerShell activity to run the script
• Use a Start Process activity to open and show the content of the created text files
• Use a Delay activity to allow the user some time to read the content of the files
• Use a Kill Process activity to kill the Notepad process
• Use a Message Box activity to inform the user that a folder will be created in the
project folder where both the text files will be moved
• Store a PowerShell script in a text file in the project folder that creates a folder in
the current directory
• Use an Invoke PowerShell activity to run the second script
• Use a Message Box activity to inform the user that the folder is created, and the
files will be moved
21 | P a g e
Advanced RPA Design and Development v4.0 June 2022
• Store a PowerShell script in a text file in the project folder that moves files from
the current directory to the new folder
• Use an Invoke PowerShell activity to run the third script
• Use a Message Box activity to inform the user that the files have moved
successfully
• STOP
22 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 22
23 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 23
Invoke VBA activity is used to invoke macro from an external file containing VBA code and run against an
Excel file.
Invoke VBA activity is used to invoke macro from an external file containing VBA
code and run against an Excel file.
24 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 24
CodeFilePath
Enters the path to the macro file EntryMethodName
containing VBA Sub/Function Enters the Sub/Function name
definitions that is to be invoked
EntryMethodParameters OutputValue
Enters the list of up to 30 Stores the value returned by the
parameters that can be passed to execution of the invoked code
the entry method
• CodeFilePath: Enters the path to the macro file containing VBA Sub/Function
definitions
• EntryMethodParameters: Enters the list of up to 30 parameters that can be
passed to the entry method. This field supports
only IEnumerable<object> variables
• EntryMethodName: Enters the Sub/Function name that is to be invoked. Must be
present in the macro file supplied in the CodeFilePath property
• OutputValue: Stores the value returned by the execution of the invoked code if
any
25 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 25
This section introduces the Python Activities Package and properties of Python
activities.
26 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 26
Introduction to
Python Activities Package
Python activities package must be installed in Studio to run the Python code. The activities available in
the package are:
Python activities package must be installed in Studio to run the Python code. In
Studio, the Python activities package is in UiPath.Python.Activities. The activities
available in the package are:
27 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 27
The use of Python activities in a workflow that adds two numbers using the Python
code:
28 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 28
Python Scope Activity Load Python Script Activity Invoke Python Method Activity
• Path: Inserts the path of the • Code: Inserts Python code to be • Input parameters: Insert
Python.exe application executed parameters to the script
• Target: Choose Python 32- or 64- • File: Choose the Python file to be • Instance: Insert the output of Load
bit versions executed Python Script activity
• Version: Choose the Python • Result: Store the output in a • Name: Insert method names from
versions variable the script
• WorkingFolder: Specifies working • Result: Store the output in a
folder of the scripts variable
Note: Either the Code or the File property is needed at a time, but not both.
29 | P a g e
Advanced RPA Design and Development v4.0 June 2022
30 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 29
Note: Either the Code or the File property is needed at a time, but not both
31 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 30
Classroom Exercise
• Calculate the volume of a cuboid and display the output in the output panel using
VB.NET
• Sum two numbers and store the output in a Notepad file using PowerShell
• Multiply two numbers and display the output in a message box using Python
programming language
Steps:
• Calculate the volume of a cuboid and display the output in the output panel using
VB.NET:
• In Studio, insert a Sequence activity in the designer panel
• Insert an Invoke Code activity
• Click on the Edit Arguments button
• In the Name column, enter a new argument named in_intHeight
• In the Direction column, select In
• In the Type column, select Int32. And in the Value column, enter 30 as the
default value. This value will be used in the code while calculating the
volume
• Click OK
• Click on the Edit Code button
• In the Code Editor, enter the expression:
• Dim Length, Breadth, Volume As Integer
• Length = 50
• Breadth = 20
• Volume = Length * Breadth * in_intHeight
• Console.WriteLine(“The volume of the cuboid is: {0}”, Volume)
32 | P a g e
Advanced RPA Design and Development v4.0 June 2022
• Click OK
• Save and run the workflow
• Outcome:
• Open the Output panel to see the calculated value as 30,000
• It means that the code executed successfully
• Sum two numbers and store the output in a Notepad file using PowerShell:
• Open a Notepad file and add a few lines of code that will run in PowerShell
to add two numbers
• $one = 15
• $two = 50
• $sum = $one + $two
• $output = “The total sum value is: ” + $sum
• New-Item -Path . -Name “sum.txt” -ItemType “file” -Value $output
• Save the file as SumTwoNumbers.txt
• In UiPath Studio, insert a Sequence activity
• Insert a Read Text File activity
• Click the folder icon. Select the Notepad file containing the code. Click
Open.
• Insert a new string variable Script in the Result property
• Insert an Invoke PowerShell activity below the Read Text File activity
• Insert the variable Script in the text box
• In the Properties panel of the Invoke PowerShell activity, check the box of
IsScript property
• Click on the TypeArgument dropdown and select “Browse for Types”
• Enter System.IO.FileInfo in the Type Name search bar. Select FileInfo and
click OK
• Save and run the workflow
• Outcome:
• sum.txt can be seen in the project folder
• The output is shown in the file
• It means that the code executed successfully
• Multiply two numbers and display the output in a message box using Python:
• Ensure that Python is installed on the system before running Python code
via Studio. At present, Studio only accepts Python versions from 2.7 and
3.6
• Open a Notepad file and add a few lines of code that will run in Python to
multiply two numbers
• import os
• def multiply ():
• a = 10
• b = 20
• Result = a * b
• return Result
• Save the file as multiplyTwoNumbers.py
• In Studio, insert a Sequence activity
• Install UiPath.Python.Activities package
• Insert a Python Scope activity
• Copy the file path of Python.exe from the Program directory in the C Drive
33 | P a g e
Advanced RPA Design and Development v4.0 June 2022
• In the Properties panel of the Python Scope activity, enter the copied file
path in the Path property
• In the Target property, choose 64 bits or 32 bits from the dropdown as per
your Python version
• In the Version property, choose your Python version
• Insert a Load Python Script activity in the Do section of the Python
Scope activity
• Click on the ellipsis icon and select the Python file multiplyTwoNumbers.py
• In the Properties panel, press Ctrl + K in the Result property and enter a
variable named multiplyPythonScript
• Insert an Invoke Python Method activity
• In the Properties panel, enter multiplyPythonScript in the Instance property
• Enter “multiply” in the Name property in double-quotes
• In the Result property, press Ctrl + K and enter a variable named
invokeMultiply
• Insert a Get Python Object activity
• In the Properties panel, enter invokeMultiply in the Python Object property
• In the Result property, press Ctrl + K and enter a variable named
MultipliedValue
• Insert a Message Box activity
• In the text box, enter MultipliedValue
• Save and run the workflow
• Outcome:
• A message box shows the multiplied value as 200
• It means that the code executed successfully
34 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 31
Practice Exercise
Build a workflow using Python activities to check if the input number is a prime
number. The workflow should:
Process Overview
• START
• Use an Input Dialog activity to ask the user to input a number
• Store the Python script to check prime numbers in the project folder
• Use a Load Python Script activity within a Python Scope activity to load the
script file
• Use an Invoke Python Method activity to call methods from the script
• Use a Get Python Script activity to convert the output from PythonObject
variable to .NET datatype
• Use a Message Box activity to display whether the number is a prime number
• STOP
35 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 32
36 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 33
Java activities package must be installed in Studio to run Java code. Adding the package will show the
available Java activities in the Activities panel.
Java Scope
Create Java Object
Initializes a specified Java library
Creates a JavaObject variable
and provide a scope for Java
from a class
Activities
Java activities package must be installed in Studio to run Java code. Adding the
package, UiPath.Java.Activities will show the available Java activities in the
Activities panel. Java Activities package provides six activities to use in Studio. They
are:
• Java Scope: Initializes a specified Java library and provides scope for Java
Activities. All Java activities must run inside a Java Scope
To know more, visit: https://docs.uipath.com/activities/docs/java-scope
37 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 34
Steps: 2
1. Java Scope activity initializes a Java
Library
2. Load Jar activity loads items.jar file 3
containing the Java Code to generate
prime numbers
3. Invoke Java Method activity runs a
static method, getPrimeNumbers, and
store the output in 4
PrimeNumbersJavaObject
4. Convert Java Object activity converts
the output from JavaObject to a .NET
array of integers named
intPrimeNumbers
The array, intPrimeNumbers, is displayed as
a string.
The use of Java activities within a workflow that generates an array of prime
numbers and displays the result as a string:
38 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 35
Java Scope Activity Load Jar Activity Convert Java Object Activity
• Init timeout (milliseconds): Enter • JarPath: Enter the path to the .jar • JavaObject: Enter the JavaObject
the Amount of time to wait for Java file containing the Java Library variable to be converted to .NET
runtime connection Package data type
• JavaLibraryPath: Enter the path to • TypeArgument: Select .NET data
the Java executable java.exe file type
• Result: Store the converted
JavaObject stored in a .NET variable
• JarPath: Enter the path to the .jar file containing the Java Library Package
To know more, visit: https://docs.uipath.com/activities/docs/load-jar
39 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 36
Properties of
Java Activities (Contd.)
Get Field Activity Create Java Object Activity Invoke Java Method Activity
• FieldName: Return name of the • Parameters: Pass arguments to • MethodName: Pass name of the
field as JavaObject variable Java method method to be invoked
• Result: Store Java field in a • ParametersList: List of parameters • Parameters: Pass arguments to a
JavaObject variable to pass to a Java method Java method
• TargetObject: Retrieve JavaObject • Result: Store output in JavaObject • TargetObject: Enter name of Java
containing the field variable Package on which to execute Java
• TargetType: Use the Java class • TargetType: Use the Java class method
name to create JavaObject name to create JavaObject • TargetType: Enter Java package
name class
40 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 37
Used to execute the JavaScript code in Studio. It executes JavaScript code in the context of the web page
corresponding to a UiElement.
Inject JS Script activity is used to execute the JavaScript code in Studio. It executes
JavaScript code in the context of the web page corresponding to a UiElement.
41 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 38
Properties of
Inject JS Script Activity
The properties of Inject JS Script activity are:
Element
InputParameter
Returns the UiElement variable by
Inputs the data for JavaScript code
another activity
Selector
ScriptCode
Uses text property to find a UI
Runs the JavaScript code as a
element
string or full path to a .js file
Timeout (milliseconds)
ClippingRegion Specifies the amount of time to wait
Defines the clipping rectangle in for the activity to run
pixels
WaitForReady
Waits for the target to become
ready. The following options are
available: None, Interactive,
Complete
42 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 39
Best Practices
• Best Practices for Utilizing External Code
This section gives an overview of the best practices for utilizing external code.
43 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 40
Best Practices
Use Invoke Code activity to combine multiple Assign and Invoke Method activities
Use Invoke VBA activity if the goal is to handle Excel files only
Always check if there are equivalent out-of-the-box activities to run the logic in external code
Invoke code with multiple lines of code is a complex solution for long-term maintenance of the projects
44 | P a g e
Advanced RPA Design and Development v4.0 June 2022
Slide 41
Summary
45 | P a g e