Robotic Process Automation

View Original

How to use the Code stage in Blue Prism RPA

What is the Code stage?
In Blue Prism RPA, a custom code stage is called a "Code Stage." It allows you to write and execute custom code (C#, Visual Basic, or J# .NET) within your Blue Prism process.

Screenshot of the Code stage within a Blue Prism Object within the Object Studio IDE.

Why would you want to use the Code stage?
Using the Code Stage in Blue Prism RPA can be beneficial for various reasons. Here are some of the primary reasons you might use the Code Stage:

  1. Extending functionality: Blue Prism offers a wide range of built-in stages and VBOs (Visual Business Objects) to perform common tasks. However, there might be situations where you need functionality that is not available out-of-the-box. The Code Stage allows you to leverage the power of .NET programming languages (C# or VB.NET) to build custom functionality within your processes.

  2. Complex calculations and transformations: When dealing with complex calculations or data transformations that are difficult to achieve using standard stages, the Code Stage provides an easier way to implement these operations using custom code.

  3. Integration with external systems: You may need to interact with external systems or APIs that do not have pre-built connectors or VBOs in Blue Prism. The Code Stage allows you to write custom code for such integrations, using .NET libraries to communicate with the external systems or APIs.

  4. Reusable code snippets: You can create reusable code snippets within a Code Stage and use them across multiple processes or objects in your Blue Prism solution. This can help reduce development time and improve maintainability.

  5. Optimizing performance: In certain cases, using custom code in a Code Stage can improve the performance of your Blue Prism processes, especially when dealing with large datasets or complex calculations.

  6. Debugging and troubleshooting: When dealing with complex logic, using a Code Stage can make it easier to identify issues and debug your processes. You can add logging and error handling in your custom code to better understand the flow of execution and identify potential problems.

That said the Code stage is not meant to support a giant opus of programming.  It is best suited for targeted, custom functionality that you cannot find in standard BP components or on the Digital Exchange (DX)  (always worth a look, some great components out there). Using custom code can increase the complexity and maintenance requirements of your Blue Prism solution, so it's essential to weigh the benefits against the potential drawbacks.


What kind of skills do you need to use the Code stage?

The basic assumption is you are not a Blue Prism rookie and have a good understanding of the basics of Blue Prism, its components, and workflow. You should feel comfortable and have experience using the different built-in stages, data types, and process flow structures available in Blue Prism.

The biggest skill is having programming knowledge, and specifically in C#, Visual Basic, or J# .NET.  The whole idea of most RPA tools is to minimize true programming and deep technical knowledge but the reality is the majority of RPA developers come from technical backgrounds and education and feel comfortable doing some if/thens and loops.  But if you are new to programming far be it from me to not support your journey, I am all for it, it just will take some trial and error but you will get it.  Check out my tips further down, as now with Chat GPT and co-pilot it is even easier for new programmers to be successful.

What kind of tools do you need to use the Code stage?

So strictly speaking you only really need the Blue Prism software itself. However, practically speaking there are additional tools and resources that can make it easier to work with custom code.

An Integrated Development Environment (IDE)
The Code Stage only provides the standard built-in text editing tool like you have seen in other components like the expression builder screen.  For any code longer than 2 lines I think you will find it much more convenient to write and test your custom code in an IDE. 

Some popular IDEs for C# and Visual Basic .NET are:

  • Visual Studio: A powerful IDE developed by Microsoft, offering extensive support for C# and VB.NET development.

  • Visual Studio Code: A lightweight, open-source code editor developed by Microsoft that supports C# and VB.NET through extensions.

Another free option I have used is the VBA Macro editor in Microsoft Excel.  Since many of our automations deal with Excel and perhaps you dabbled in macros in the past, the Excel Macro tool can be more than sufficient in banging out a small script in VB and running tests.  If you want to use C#/J# you will need to use something else.  VS Code is a great overall IDE.

Screenshot of the Code Options configuration screen where you will see default libraries available to your custom code. This screen is where you can add additional reference libraries and namespace imports for your code.

.NET libraries and NuGet packages
Closely related to the IDE, and a bit deeper into the technical weeds are libraries.  Libraries provide pre-built functionality for developers to build on such as libraries for file management, network connections, working with certain data types.  You may need additional .NET libraries to integrate with external systems or perform specific tasks in your custom code. NuGet is a package manager for .NET that makes it easy to find and install the libraries you need.

This is an area that could be a black box to you and you never need to worry about it or it will be a key reason why you are using the Code stage to begin with.  Again, if you are new to programming and/or BP this is an area you will need to do a lot more research on before wading into the deep end of the pool.

I will show you more about libraries specifically for the Code stage a bit further down in this post.

Some other related tools you may need.

API testing tools: If you're integrating with APIs or web services, tools like Postman or Insomnia can help you test and explore the API endpoints, making it easier to write your custom code.

Version control systems: To manage your custom code effectively, especially in a team setting, consider using version control systems like Git, which can help you track changes, collaborate with others, and maintain a clean codebase.

Documentation and reference material: Familiarize yourself with the Blue Prism documentation and resources for the programming language you're using (C# or VB.NET). You may also need documentation for specific libraries or APIs you're working with.

Debugging and logging tools: While Blue Prism provides some built-in debugging capabilities, you might find it helpful to use external debugging and logging tools for your custom code. These can provide more advanced functionality and better visibility into the execution of your code.

By having access to these tools and resources, you'll be better equipped to create and maintain custom code in your Code Stage.

Here's a step-by-step guide to create a Code Stage in Blue Prism:

  1. Open the Blue Prism and create a new Object.  The Code Stage is only available within Objects.

    

The code stage is available in the palette of stages on the left-hand side of Object Studio.

  1. In the 'Studio' tab, click on the 'Stages' panel on the left side to expand it.

  2. Drag and drop the 'Code' stage onto the canvas (main working area) where you want to include the custom code.

                

  1. Double-click on the Code Stage to open the 'Code Stage Properties' window.  In detail view you will see the normal Inputs and Outputs you will see in other stage components but with now you will see a new tab named “Code”.

Detail view of a BP Code stage. This area is where you would put your custom code. Given the limited editing capability you will want to develop and test your code in a real IDE like Visual Studio Code and then copy and paste it into the Code stage.

This tiny window is your brand new IDE! Just kidding, but this is where your actual C# or Visual Basic .NET code will go.  The best practice recommendation, and what has worked for me, is to develop your code outside of this Code stage to leverage modern and familiar IDE tools such as Visual Studio Code

You could use any IDE or text editor of your choice.  Another useful environment to develop and test in, specifically due to the use of .NET, is the VBA Macro editor in Microsoft Excel.  One of the key factors in your tool suggestion is the ability to get code suggestions, i.e. intellisense, to aid in your development.


  1. In the 'Code Stage Properties' window, you can specify the following:
    a. Name: Provide a name for the Code Stage to describe its purpose.
    b. Language: Choose the programming language you want to use, either C# or Visual Basic .NET.
    c. Inputs: Define any input parameters required for your custom code.
    d. Outputs: Define any output parameters that your custom code will generate.

  2. Write your custom code in the 'Code' tab of the 'Code Stage Properties' window. You can use any available .NET functionality and reference additional libraries if necessary.
    For example, here's a simple C# code snippet to add two numbers:

+ number2;
You would define two input parameters, "Number1" and "Number2," and one output parameter, "Result."

  1. Click the 'Compile' button to compile your custom code. If there are any syntax errors or issues, they will be displayed in the 'Compile Results' tab. Fix any errors before proceeding.

  2. Click 'OK' to close the 'Code Stage Properties' window.

  3. Link the Code Stage to other stages in your process or object using connector arrows.

  4. Run the process or object to test the functionality of your Code Stage.

Remember to use proper error handling and logging in your custom code to ensure smooth execution and easier debugging.

Resources

  • A must have bookmark is https://community.blueprism.com/  there are some many great folks from Blue Prism, Customers, Vendors and outside consultants in the blue prism community willing to help. Definitely sign up and search for any questions you may have on Code stages.

  • https://bpdocs.blueprism.com/  This is the official BP document site, invaluable for research.

  • https://chat.openai.com/  The AI genie is out of the bottle and all the pros/cons that come with it.  Right now it seems like a great source of data and instruction when used correctly.  Just be aware that older versions may not have a lot of BP information in it's model so answers could be lacking or outright wrong.  I have found 4.0 is better. But for .NET specifically, great.

  • Closely related to GPT is https://github.com/features/copilot Again not so much for BP topics specifically for .NET and programming it is a huge asset.