When and how to use Invoke Method activity || UiPath
Are you confused when to use Invoke Method activity and how to use it!?
We often use “methods” in UiPath to achieve required results. Generally, we do it through Assign activity. But there are some methods for which have to use Invoke Method activity! Why should we do that? Why can't we keep using our beloved Assign activity?
Worry no longer! Just read this short blog and it will be crystal clear to you about where and how to use an Invoke Method activity instead of Assign activity. The trick is to check if a method returns any value!
The Assign activity scenario
The UiPath intellisense will be our savior in understanding this topic. Let's look at below samples:
All the above methods have 3 components in common:
-> Method Name [Equals, Contains, Copy, StrReverse]
-> Input Arguments [Equals method takes 2 arguments while others here take 1. Some methods don’t require any input arguments.]
-> Return Value [Boolean, String: Focus on the last part of the snapshot starting with ‘As ….’]
So, do we need Invoke method activity here? NO. Since all above methods are returning values, we can assign the returned value to a variable on left side of Assign activity! Below is the example usage of the StrReverse method where we store the reversed string (return value) to a variable TestString1:
The Invoke method scenario
Now let’s have a look at some other methods:
Did you notice any difference? Yes, you guessed it right! None of these have any return type. So, we will have to use Invoke Method activity! This activity requires either the TargetType or the TargetObject. Also, you can pass arguments to the method through the Parameters property, if required.
Example-1 : Using Clear() through Invoke Method activity
- TargetObject : A list [I have used a variable SampleList]
- MethodName : Clear
- Parameters : [None required in this case]
Example-2 : Using SetOrdinal() through Invoke Method activity
- TargetObject : A DataColumn [I am referring to TestColumn3 from a DataTable named SampleDataTable]
- MethodName : SetOrdinal
- Parameters : 1 integer input argument [I am passing 0 to shift that column to 1st position!]
Voila! Now you know when and how to use Invoke Method activity!!
Revision: When in doubt about the need for Invoke method, just make use of UiPath intellisense and check if the method returns any value or not!
Feel free to Follow me for more such easy-to-understand content. I am also available on LinkedIn :-)
Thank you for your time!! See you soon.