IBM RPA now has a powerful functionality: the ability to execute C# code directly within the WAL script through the runCSharpCode command. In addition to allowing you to make full use of the .NET 4.8 framework, the command also allows you to import external libraries, whether you created them or a third party. This adds a new layer of flexibility and potential to automations.
Before going to a practical example, it is worth understanding how the command works.
How runCSharpCode works
The command has four main parameters, each responsible for an essential part of code execution.
1. Assemblies
Here you define the external libraries (DLLs) you want to use in your code. They must be manually copied to the IBM RPA Client installation folder.
2. Input
The Input parameter allows you to pass values that the script has already obtained throughout the automation. It is made up of three fields:
· Parameter: Name of the variable within C# code.
· Type: Expected .NET type. If the value cannot be converted to the specified type, an alert is displayed.
· Value: Initial value of the variable — This can be a variable or constant. In the case of strings, it is not necessary to use quotation marks.
3. Output
This parameter defines which values your C# code will return to the script.
· Parameter: Name of the variable within C# code.
· Type: Expected .NET type. As with Input, invalid conversions generate an alert.
· Value: Must point to a variable in the script, which will receive the result of the processing.
4. Code
Here you enter the C# code to be executed. only the code, no class definition is required, no access modifiers (public, protected, private), and the variables entered in the Input and Output fields are already part of the code and do not need to be declared again. In addition, some namespaces are already loaded automatically:
· System
· System.Collections.Generic
· System.Data
· System.Linq
Here we put only the instructions that need to be executed. Object Orientation and Design Pattern must be encapsulated in the imported library. The code in this field has a script format having only two parts: a header containing all the using statements, and then the body with the code to be executed.
Practical examples:
· Custom rounding
Currently, RPA Studio offers the truncate command, extract the integer part of a number, the decimals, or round a number using the nearest number logic. But if you want to use the ceiling or floor function, they are not yet available in RPA. Let's see how the problem would be solved in RPA by creating the command for the ceiling function:
We add the command with two input parameters, the dividend and the divisor and one output, the quotient.
In this example the input was started with constants, but it also accepts variables.
Also, note that the output variable is identified as "quotient" in C# code, but in the wal script the name of the variable is "output".
You can wrap this command in a subroutine, allowing it to be reused and called as many times as needed by other routines in the script.
· Importing external libraries.
RPA Studio already has commands to handle the most popular formats, such as JSON, XML, and CSV. However, in some scenarios it is necessary to deal with custom formats or newer standards that do not yet have native support on the platform. For example, TOON (Token-Oriented Object Notation) has become popular because it is more economical than JSON, especially in AI training. However, .NET does not yet have standard libraries for handling TOON.
I've written my own library that converts tabular arrays in TOON format to Dictionary List. Now I can import it into RPA Studio's runCSharpCode command and convert TOON to DataTable.
Here's how the solution turned out:
We use the readAllText command to read the "sales.txt" file by assigning its contents to the toonString variable (Line 3).
Then we use the runCSharpCode command to import the Toon.dll library, which I had previously created, and in C# code, I initialize the output variable "dataTable". I add the columns with the same name as the fields in the TOON file (itemsPurchasedd, and customerName). I finish the C# code with a loop that adds the records found in the file to the dataTable that can also be read by RPA.
You can download the wal script used for this example here and the Toon.dll here.