Invoke Code and Invoke Method - An Overview
UiPath is a low-code tool which helps us build sophisticated automation workflows with minimal programming effort, which is primarily achieved through the usage of built-in activities.
Despite being a low-code platform, UiPath also provides us ways to write and execute C# or VB code or even make use of the public methods available in C# or VB class libraries through the available built-in activities such as Invoke Method and Invoke Code so in this article, lets understand the usage of these activities with an example.
What's in this article?
A brief overview on the below topics:
- Invoke Method
- Invoke Code
The Invoke Method activity is used to call a public method from a class, which is not available in the built-in activities or use any of the workflow base classes.
The activity calls a method of a specified type or object and is available by default in the Systems.Activities package. Here's how the activity looks:
There are 2 required properties for this activity to work as expected:
- TargetType or TargetObject
As stated earlier, the purpose of this activity is to call a public method that is available in a C# or VB class library, therefore the MethodName property is mandatory whereas the TargetType or TargetObject is mutually exclusive. It is the method signature determines the values for TargetType or TargetObject properties.
Static methods don't require object creation and it can be called-in directly. Therefore, they require the TargetType property to be provided along with the MethodName property.
Instance methods on the other hand, requires the object from which the method or members are derived needs to be defined. Therefore, they require the TargetObject property to be provided along with the MethodName property.
The activity does not support or take property values for both TargetObject and TargetType at once.
➡️ Example for Invoke Method
The objective of this example is to sort a list using the Sort method.
Step 1: Create a list with dummy values.
Step 2: Pull-in the Invoke Method activity from the activities panel.
Step 3: Fill the required property values for the activity. In this case, we fill in the MethodName and TargetObject properties.
Step 4: Run the file.
As simple as this may look, we've actually made use of the Sort method to sort a list using the Invoke Method activity.
The Invoke Code activity synchronously invokes C# or VB code. The activity can optionally take-in and give-out arguments of any type, as required.
The activity is available by default in the Systems.Activities package. Here's how the activity looks:
There are 3 required properties for this activity to work as expected:
The Language property is set to VB .NET by default and it can be changed to C#, if required.
The Code property comprises the code snippet which is to be executed upon the bot run.
The Argument property comprises of the in, out, or in-out arguments that are to be manipulated by the code snippet. This could be as simple as adding two numbers to making complex data table manipulations.
The activity can come-in handy when we need to write custom code or to simply minimize the workflow by reducing the no of activities. One limitation with this activity is that, it doesn't allow us to write functions, procedures as one would with an IDE.
➡️ Example for Invoke Code
The objective of this example is to calculate the salary bonus for the employees drawing a monthly pay of more than $7K but lesser than $9K.
Step 1: Read the input Excel file using the Read Range activity.
Step 2: Pull-in the Invoke Code activity and the change the language property, if required.
Step 3: Pass the datatable as an in-out argument to the Invoke Code activity.
Step 4: Write the code block that performs the required actions.
Step 5: Run the file.
Notice how the code snippet has added a new column named Bonus of type double and has performed the calculations, as required.
Using Invoke Code can be incredibly helpful when working with large sets of data as it can help us improve the performance of the overall process.
This concludes our article on the usage of Invoke Method and Invoke Code activities using UiPath.
Hey, thanks for reading the article. I hope it helped you learned something new. If it did, please consider giving it a like, and share it with your like-minded awesome friends who like RPA. Cheers 👋🏼