The Script activity allows you to create scripts in C# and uses the Context entity. For example, you can use it to get a list of files in the required folder. It can help you determine the names of the files and save data in a context variable. Other ways of using the activity are data parsing or validation, sending and getting emails, as well as using APIs to work with third-party applications.
Moreover, you can use context variables for easier process administration. Read more about context variables in the Process context article.
All the process scripts are separated from each other. You cannot interact with a script from another script. The only interaction point is the process context that is available in any script. All the scripts are represented as separate files and are executed individually when running the Script activity.
Let's create several variables of different types as an example.
To open the settings window, click on the activity on the process diagram.
The Parameters tab displays the basic activity parameters:
- Name. The activity name on the process diagram. Its name is set by default when adding the activity. You can change the name in this field.
You can edit a script or generate a new script project using the Edit script button.
The ELMA RPA Designer window will be collapsed and the script folder will automatically open (../<system files folder>/Users/%Username%/AppData/Local/ELMA_RPA/script). After that, a control panel will be displayed . Here you can add files to the process or cancel editing.
In the folder specified above, a project will be temporarily created containing the four following files:
- Context.cs. The process context file;
- Program.cs. The file containing the simplest console application. It contains information about creating the context and calling the script. The created context is passed to the script. You can use it for script debugging;
- Script.csproj. The project file;
- ScriptActivity.cs. The script file used in the Script activity.
The project is generated for Visual Studio 2019, but you can use any other IDE or editor.
Open the Script.csproj file.
Here you can see the project structure — the files located in the folder. Script.csproj is a root and a special object but can be changed as a usual file. However, we do not recommend changing it unless you are aware of the new .NET Core project files structure.
Context.cs does not need to be changed. It has a generated class with a set of the context variable properties you have created on the Context tab of the ELMA RPA Designer. We will work with them in ScriptActivity.cs and Program.cs.
Open the file ScriptActivity.cs.
Here you can see a generated ScriptActivity class in the Scripts namespace. The class generates an empty Execute method that accepts the context generated in Context.cs.
The Execute method is an entry point for the Script activity. This is the main method that is run first. It is similar to the Main method for any other program. Besides the Execute method, you can create another method in the class and even new classes and structures in the file.
Now we have a logic that we want to check. We can use the generated file Program.cs. By default, it stores a class and a method with the described class objects (Context and ScriptActivity), as well as running the Execute method of the ScriptActivity class instance. This way we model the actions that take place in the activity when the Script is being executed.
We can specify the values of the context variables by simulating a certain process state.
Using NuGet packages
You can use the NuGet packages. ELMA RPA will determine that they need to be used and build the script projects with dependencies to the NuGet packages.
NuGet packages contain a reusable code provided for your projects by other developers. This makes it easier to write the script logic and saves your time. We recommend you to check out some popular NuGet packages.
This is how you can add a NuGet package to Visual Studio 2019. Call the context menu of the project and select Manage NuGet Packages... .
This will open the Visual NuGet Package Manager tab for the current project. You can search for the packages, install their specific versions, update and delete them.
In addition to the visual NuGet Manager, you can install packages in Package Manager Console, in the command line, or manually write them into the project file.
Adding external libraries
You can add external libraries (*.dll) to the script project. ELMA RPA can build a temporary project using the third-party libraries. We recommend using it in extreme cases.
You can add an external library (*.dll) to the project in two ways.
- Open the reference settings window.
In the window that opens click on the Browse… button and select the required library.
- In the project file (Script.csproj) manually write down the following:
<Reference Include="Assembly name">
<HintPath>Path to the library file (*.dll)</HintPath>
After that, save the file changes and confirm the script changes by clicking the button on the control panel.
Visual Studio Code is another convenient tool for editing a code. When installing the necessary extensions, you can use the automatic prompts (IntelliSense) to change the code and easily create configurations for building and debugging. Debugging can be performed by the code tracing.
Read more about the Conditions tab in the Basic activity settings principles article.
Found a typo? Highlight the text, press ctrl + enter and notify us