Search code examples
c#oopmethodsblueprism

Declaring And Calling Methods in Blue Prism Global Code Stage


I am a Blue Prism RPA Developer and I want to create methods in my global code and call them from other code stages. In Global code info page, it is stated that this could be done but i could not find a satisfactory resource on the subject. Can anyone share a syntax rule set, maybe a sample global code and code stage code sniplets or guide me in the direction? I use C# and i would much appreciate responses in C#

Note: I am not from a developer background, i have elementary coding know how but i am not entirely knowladgeable about OOP subjects (namely: Classes, Methods, Constructors, Inheritence etc)

I tried declaring one class and one method, that worked, i could call the method but when i tried to add new method and or class it failed, it did not compile with the overall code


Solution

  • If I understand your question correctly, you are looking for an example of a class containing one or more methods, that you can place into the Global Code stage of the Initialise page of your BP Object, and then be able to create/call instances of that class/method in your code-stages from the other pages in that BP Object.

    I don't know how much you know, so I am going to assume every step needs to be explained.

    Since you are using C#, your first stop should be the Code Options tab. Here you should reference any libraries you intend to use (.dll) on the top pane, and their respective namespaces in the bottom pane. BP already includes some basic ones as shown below. It is also very important to change the Language selection to C# (bottom left drop-down), as Visual Basic seems to be the default option:

    enter image description here Next, here is an example of a simple concrete class with some fields, a constructor, a property and a method. You can place this code inside the Global Code window as-is:

    public class SomePerson
    {
        //Class variables
        private string _firstName;
        private string _lastName;
    
        //Constructor
        public SomePerson(string firstName, string lastName)
        {
            this._firstName = firstName;
            this._lastName = lastName;
        }
    
        //Property
        public string FullName
        {
            get
            {
                return string.Format("{0} {1}", this._firstName, this._lastName);
            }
        }
    
        //Method
        public string Hello()
        {
            string myText = "Hello "+FullName+", it is nice to meet you.";
            return myText;
        }
    }
    

    Now you will be able to call instances of this class from inside code-stages and use the property and the method. For example, you could supply the FirstName and LastName in a couple of data items in BP, and then use an instance of the SomePerson class property to get the FullName using this code:

    SomePerson Anyone = new SomePerson(firstName, lastName);
    fullName = Anyone.FullName;
    

    Similarly, you could use the method as follows:

    SomePerson Anyone = new SomePerson(firstName, lastName);
    result = Anyone.Hello();
    

    You could try all this out using a layout like this one:

    enter image description here

    And basically... that's it!. In this manner you can create as many classes (concrete or abstract) and interfaces as you need, just stack them up inside the Global Code pane as you did with this one.

    Finally, make sure you understand that most VBOs (like the Excel one) are written in Visual Basic, so chopped-off stuff will not compile together with C# code; you must use one or the other. Yes, they are both .NET languages, but once you have chosen the BP Object language, you must write your code in that language.