Function in Invoke Code with C#

I am trying to write C# code using invoke code activity with C# but when I am creating function it is not working and stating that “No compiled code to run”. Below is the example

try
{
GetUserName(userName);
}
catch(Exception ex)
{
}

Public void GetUserName(string username)
{
Console.writeline(username);
}

Please let me know what is the right way to call function in invoke code activity.

1 Like

I understand that each Invoke Code Block is a Function or Method, as it has both Input and Output variables, that is, you need to create your code routine inside this Invoke Code Block. As far as I know, UiPath doesn’t offer within this activity the possibility of writing classes, methods or functions.

1 Like

Thank you @Jorge_Cavalcante for replying on my query.
There is one function which I need to call recursively to get the last ID details.
Could you please help me how I can achieve this ?

Hey,
if you have code in C# (class, methods or function) you can use it inside invoke code block.
Firstly you have to import all namespaces which you use:


Then you need set all arguments which you transferring between UiPath <-> C# code.
example:

The last step is use you code without using any class/function/method.
image
Please get my example where I unhiding rows in Excel file using C# code in ‘invoke code method’:
cSharpExample.zip (22.5 KB)

@ankitdwivedi0290

Hello Ankit,
@Jorge_Cavalcante is right, the C# code, inside a Invoke Code Activitiy, is like a Main method of a class. To meet your requirements you can use Lambda functions.

Action<string> GetUserName = (string username) => {
  Console.WriteLine(username);
};

try {
  GetUserName(userName);
} catch(Exception ex){

}

Best regards
Stefan

2 Likes

Hello, @StefanSchnell

I learned a little more from your answer. I never thought of using Lambda Functions in this scope. I believe I will use the Invoke Code activity a lot more from today.

Thanks.

1 Like