How to integrate custom code in UiPath

How to integrate custom code in UiPath

A brief guide on using C# and VB.NET Code in UiPath

ยท

4 min read

Table of contents

UiPath is a powerful RPA tool that allows for the creation of advanced automation workflows with minimal programming efforts. Despite being a low-code platform, UiPath also provides the capability to integrate custom C# or VB code within workflows through the use of the Invoke Code and Invoke Method activities.

In this article, we will explore how to integrate C# or VB code in UiPath with an example.

Invoke Method

The Invoke Method activity in UiPath is used to call a specific method within an existing .NET assembly or class. The activity supports the use of generic methods and calls a public method of a specified object or type.

image.png

The Invoke Method activity has 3 main properties:

  1. Target Type

  2. Target Object

  3. Method Name

The Method Name property is mandatory as the sole purpose of this activity is to call a public method.

Target Type and Target Object properties on the other hand are mutually exclusive. The method signature usually determines the value for these properties.

๐Ÿ”ธ For static methods, provide the Target Type property along with the Method Name property as the method call doesn't require object creation.

๐Ÿ”ธ For instance methods, provide the Target Object property along with the Method Name property as the method call requires a target object, on which the method is executed.

โœ๏ธ Example:

Consider we need to sort a list of type string. One of the best ways to achieve this is by using the Sort() method.

  • Define a list of type string as below:

userCities = New List(Of String) From {"New York", "Los Angeles", "Seattle", "Houston"};

  • Provide the Sort() method in the Method Name property and the list name in the Target Object property.

    image.png

  • This will help us sort the list and provide an output as below.

image.png

It's important to note that the method you're trying to invoke must be a public static method, otherwise it can't be invoked by the activity. This concludes our example on Invoke Method.

Invoke Code

The Invoke Code activity synchronously invokes C# or VB code. It can also take in or give out arguments of any data type. This activity can come in handy if we need to execute a custom script within a workflow or when we have a need to build complex automation workflows that cannot be achieved using pre-built activities such as filtering, sorting, and calculation of data.

image.png

The Invoke Code activity has 3 main properties:

  1. Language

  2. Code

  3. Arguments

The Language property refers to the programming language used inside the activity. It is set to VB by default and it can be changed to C# as required.

The Code property comprises the code snippet which is to be executed upon the bot run.

The Argument property comprises arguments of different directions (in, out, in/out), that are to be manipulated by the code snippet. This could be as simple as adding two numbers or iterating through multiple rows in a data table.

โœ๏ธ Example:

Consider we need to calculate the salary bonuses for employees who are drawing a monthly salary greater than a given amount with different additional conditions for different locations.

This can be achieved using the Invoke Code activity as it helps us customize the workflow easily compared to that of a flowchart, plus it's super faster given that we're working with very large sets of data.

  • Create a new data table using the Build Data Table activity or read the input Excel file.

    image.png

  • Pass the data table as an in-out argument.

    image.png

  • Write C# or VB code for the given business logic inside the Invoke Code activity.

EmpSalaryDT.Columns.Add("Bonus", typeof(System.Double));
foreach (System.Data.DataRow row in EmpSalaryDT.Rows){
 if(Convert.ToInt32(row["Employee Salary"]) > 7000 && row["City"].ToString().ToLower().Contains("palo alto")){
     row["Bonus"] = Convert.ToDouble(row["Employee Salary"]) * 0.25;
     Console.WriteLine("Bonus for " + row["Employee Name"].ToString() + " is " + row["Bonus"].ToString());
 }
}
  • This will help us calculate the bonus based on the defined business logic and provide an output as below:

    image.png

This concludes our example on Invoke Code. The Invoke Code activity can be very useful if developers need to integrate custom logic and functionality into their automation projects.

Please note that modern activities contain a new activity named Inject .NET and this will be covered in a different article.


Thanks for reading the article. Please give it a like and share it with your friends. Got any questions? Let me know!

Cheers ๐Ÿ‘‹

ย