6.4 Invoke Method & Invoke Code Activities
Key Takeaways
Invoke Method calls a static method through TargetType (TargetObject left empty) or an instance method through TargetObject (TargetType left empty).
Invoke Method parameters have directions In, Out, and In/Out, and the method's return value goes to the Result property.
Invoke Code runs a VB.NET or C# method body chosen by its Language property and can use only the values passed in its Arguments collection.
Invoke Code cannot declare classes and runs as one debugging step; logic reused across projects belongs in a library or coded source file.
6.4 Invoke Method & Invoke Code Activities
Core Concept: When no activity does what you need, UiPath gives you two ways to run .NET code directly. Invoke Method calls one existing method on a type or an object. Invoke Code runs a small block of VB.NET or C# that you write inside the activity. The exam description lists both under Advanced Data Manipulation, so know how to configure each, how arguments flow in and out, and when a library or coded workflow is the better home for the logic.
1. The Invoke Method Activity: Static vs. Instance Invocations
When an automation requires executing a .NET method that is not exposed as a standard visual activity, developers use the Invoke Method activity. It executes any accessible method on a .NET class or object using reflection without requiring external custom code compilation.
Configuring Static Methods
To call a static method (a method belonging to the class itself, not an instantiated object):
TargetType: Set to the fully qualified type (e.g.,System.IO.File,System.Math,System.Convert).TargetObject: Leave blank (set toNothing).MethodName: Enter the exact method name as a string (e.g.,"Copy","Round").Parameters: Configure the method's input parameters in the Parameters collection modal.Result: Map to a workflow variable if the static method returns a value.
Enterprise Example: System.IO.File.Copy
TargetType:System.IO.FileMethodName:"Copy"Parameters:- Parameter 1: Direction
In, TypeString, Valuein_SourcePath - Parameter 2: Direction
In, TypeString, Valuein_DestinationPath - Parameter 3: Direction
In, TypeBoolean, ValueTrue(overwrite)
- Parameter 1: Direction
Configuring Instance Methods
To call an instance method (a method operating on a specific instantiated object):
TargetType: Leave blank (or defaultnull).TargetObject: Provide the instantiated variable name (e.g.,myList,myDataTable,myStringBuilder).MethodName: Enter the method name (e.g.,"Sort","Reverse","Clear","Append").Parameters: Supply arguments required by the instance method signature.Result: Map to a variable if the method yields a return value.
Enterprise Example: Sorting a Generic List in Place
TargetObject:workItemsList(whereworkItemsListisList(Of String))MethodName:"Sort"Parameters: Empty (calls default parameterless sort)
Parameter Direction Types
In: Passes an argument value into the method by value.Out: Captures a value produced by the method via a .NEToutparameter.InOut: Passes a reference into the method and captures the mutated value (refin C# /ByRefin VB.NET).
2. The Invoke Code Activity: Native Scripting & Architectural Tradeoffs
The Invoke Code activity allows developers to author raw Visual Basic .NET or C# code blocks directly inside a UiPath workflow. It executes synchronously within the workflow runtime process.
Configuring Arguments in Invoke Code
Unlike workflow scopes where activities share global workflow variables, Invoke Code operates in an isolated code scope. Variables must be explicitly mapped through the Arguments collection:
- Direction:
In(read-only input),Out(output created inside code),InOut(mutated reference). - Type: Must strictly match the .NET data type expected by the script.
- Name: The identifier used inside the code snippet.
' VB.NET Code Snippet inside Invoke Code activity
' Arguments:
' in_RawDataTable (In, DataTable)
' in_Multiplier (In, Double)
' out_SummaryDict (Out, Dictionary(Of String, Double))
out_SummaryDict = New Dictionary(Of String, Double)()
For Each row As DataRow In in_RawDataTable.Rows
Dim category As String = row("Category").ToString()
Dim amount As Double = Convert.ToDouble(row("Amount")) * in_Multiplier
If out_SummaryDict.ContainsKey(category) Then
out_SummaryDict(category) += amount
Else
out_SummaryDict.Add(category, amount)
End If
Next
Exception Handling inside Invoke Code
- Unhandled exceptions inside an
Invoke Codeblock immediately bubble up to the parent workflow as aSystem.Exception, halting workflow execution unless caught by an enclosing Try Catch activity. - Developers can embed internal
Try...Catchblocks within theInvoke Codescript to handle recoverable edge cases, log errors, or populate diagnosticOutarguments.
Architectural Decision Framework: When to Use What
| Implementation Approach | Execution Performance | Studio Readability & Auditability | Maintainability Across Projects | Best Enterprise Use Case |
|---|---|---|---|---|
| Visual Workflow Activities | Moderate (WF activity tracking overhead) | High (Visual flowchart/sequence diagrams) | High (Standardized out-of-the-box components) | Standard UI interactions, API calls, simple conditionals, business control flow. |
Invoke Method | High (Direct CLR method invocation) | Moderate (Hidden method parameters in dialog) | High (Native .NET assembly binding) | Calling single built-in .NET methods (e.g., List.Sort(), DataTable.Merge()). |
Invoke Code | High (Compiled native CLR script) | Low (Logic hidden inside black-box script window) | Low (Script copy-pasted across workflows) | Complex math, multi-dimensional array processing, fast batch operations where visual loops lag. |
| Custom Activity / Library (NuGet) | High (Compiled pre-optimized DLL assembly) | High (Visual activity with custom properties/icons) | Extreme (Single source of truth via Orchestrator Feed) | Enterprise-wide shared utility logic, custom system connectors, proprietary algorithms. |
Important
Enterprise Governance Rule:
Avoid using Invoke Code for logic that is reused across multiple automation processes. If an Invoke Code block grows long or is copied into several projects, refactor the code into a UiPath Library project, test it independently, and publish it as an enterprise NuGet package to the Orchestrator library feed.
Invoke Code Details Worth Knowing
- Language. Each Invoke Code activity has a Language property (VB.NET or C#). It does not have to match the project's expression language.
- Namespaces. The code can use the namespaces imported in the project's Imports panel, plus fully qualified names.
- No class definitions. The body is the inside of a method, so you cannot declare classes there. Put reusable types in a code source file (
.cs) in the project or in a referenced library. - Arguments are the only link to the workflow. Workflow variables are not visible inside the code unless you pass them in the Arguments collection, with a direction of In, Out, or In/Out.
- Debugging. You cannot step through individual lines of an Invoke Code body with Studio breakpoints; the activity runs as a single step. Log values with
Console.WriteLine, which writes to the Output panel, or move complex logic into a coded workflow where the IDE supports breakpoints.
Choosing the Right Tool
| Need | Best choice |
|---|---|
Call one existing .NET method, such as List.Sort or System.IO.File.Copy | Invoke Method |
| A short, local calculation with a loop, used in one place | Invoke Code |
| The same logic used by several processes | A library project or coded source file, published and versioned |
| Complex, testable logic with types, IntelliSense, and debugging | A coded workflow or code source file |
An RPA developer wants to use the Invoke Method activity to clear all rows from an existing DataTable variable named dt_Transactions using the System.Data.DataTable.Clear() method. How should the developer configure the properties of the Invoke Method activity?
Set TargetType to "System.Data.DataTable", leave TargetObject blank, and set MethodName to "Clear"
Set TargetType to "System.Data.DataTable", set TargetObject to dt_Transactions, and set Parameters to dt_Transactions
Set TargetType to "dt_Transactions", leave TargetObject blank, and set Result to dt_Transactions
Leave TargetType blank (null), set TargetObject to dt_Transactions, and set MethodName to "Clear"
An automation team is refactoring a legacy workflow that contains a 250-line C# script inside an Invoke Code activity performing complex tax calculations. The script is currently duplicated across four different unattended automation projects. According to enterprise RPA best practices, how should this architecture be improved?
Replace the Invoke Code activity with 250 individual visual Assign activities inside a Flowchart
Package the C# tax calculation logic into a reusable UiPath Library project, compile it into a versioned NuGet package, and publish it to the Orchestrator Libraries feed
Convert the C# script to a VB.NET script inside Invoke Code to increase execution speed
Store the raw C# code as a plain text string in an Orchestrator Asset and download it dynamically at runtime
Inside an Invoke Code activity, a developer needs the value of a workflow variable named taxRate. What must be configured?
Nothing; Invoke Code can read every variable in the enclosing workflow automatically.
Add taxRate to the Invoke Code Arguments collection with direction In and a matching type.
Declare taxRate as a global constant in the Data Manager only.
Set the Invoke Code Language property to C#.
Sections you finish are checked off in the contents.