2. UiPath - Variables, Data Types and Control Flow - Giri

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

Date and Time (Category)

-DateTime - System.DateTime: Used to store specific time coordinates (mm/dd/yyyy hh:mm:ss). This kind of variable provides a series of specific processing methods (subtracting days, calculating time remaining vs. today, and so on). For example, to get the current time, assign the expression DateTime.Now to a variable of type DateTime. -TimeSpan - System.TimeSpan: Used to store information about a duration (dd:hh:mm:ss). You can use it to measure the duration between two variables of the type DateTime. For example, you can save the time at the start of the process in one variable (of type DateTime), the time at the end in another (of type DateTime) and store the difference in a variable of type TimeSpan.

Good Case Practices - Creating Variables

-Use clear and consistent naming conventions - one of the most common is Camel case (each word in the middle of the phrase is capitalized) -Make sure you define the scope of each variable correctly - remember that a variable defined on a limited scope cannot be used globally. At the same time, in real automation scenarios, it is crucial for variables to be defined only in the scope in which they are used. Making multiple variables unnecessarily global can cause efficiency issues as well as possibility for confusion.

What are some business scenarios in which I will use Switch?

An invoice that has 3 potential statuses (not started, pending, approved) and 3 sets of actions for each one A process of automatically ordering raw materials to 4 suppliers based on certain conditions

General Note on Arrrays

As a good case practice, arrays are used for defined sets of data (for example, the months of the year or a predefined list of files in a folder). Whenever the collection might require size changes, a List is probably the better option.

What are some business scenarios in which I will use GenericValue variables?

Data is extracted from a UI field and forwarded to another workflow without processing Two versions of the same Excel file are being compared column by column. The columns are different in terms of data type, the only relevant thing is which entries have changes

IF Statement demo

If statements in a Sequence We used an 'Input Dialog' activity to get an input value from the user and store it in an Int32 variable. We used an 'If Sequence' activity and defined the condition using the mod operator to check the remainder in a division: (year mod 4 = 0 and year mod 100 <> 0) or (year mod 400 = 0). If the condition is true, the value is a leap year. If Statements in a Flowchart We used an 'Input Dialog' activity to get the input value from the user and stored it in an Int32 variable. We added a 'Flow Decision' activity with the same condition as in the 'If Statement' activity above: (year mod 4 = 0 and year mod 100 <> 0) or (year mod 400 = 0) VB.Net If Operator We defined the project as a sequence and used an 'Input Dialog' activity to get the input value from the user and store it in an Int32 variable. We defined a String variable and used it as an output of an 'Assign' activity. In the value field of the 'Assign' activity, we used the same expression from the previous examples, followed by 2 pieces of text between quotation marks - the first to be assigned when the condition is true. The full expression looks like this: message = if ((year mod 4 = 0 and year mod 100 <> 0) or (year mod 400 = 0) , "Leap Year", "Not a leap Year").

The If Statement

In UiPath, the If statement is exactly how you'd expect it to be: The condition that is verified (with 2 potential outcomes - true or false) The set of actions to be executed when the condition is true (the Then branch) The set of actions to be executed when the condition is false (the Else branch) What is different is that, based on the chosen type of automation project, there are 2 corresponding activities that fulfill the If statement role: The If Statement in sequences The Flow Decision in flowcharts Moreover, the If decision can be used as an operator inside activities.

Arguments

In UiPath, the scope of a variable cannot exceed the workflow in which it was defined. Since business automation projects rarely consist of single workflows, arguments have to be used. Arguments are very similar to variables - they store data dynamically, they have the same data types and they support the same methods. The difference is that they pass data between workflows, and they have an additional property for this - the direction from/to which the data is passed. The direction can be In, Out and In/Out.

flowcharts

In flowcharts, the individual activities are a bit more difficult to read and edit, but the flows between them are much clearer. Use flowcharts when decision points and branching are needed in order to accommodate complex scenarios, workarounds and decision mechanisms.

sequences

In sequences, the process steps flow in a clear succession. Decision trees are rarely used. Activities in sequences are easier to read and maintain, thus, they are highly recommended for simple, linear workflows.

DO while Demo

In the same sequence where the If Statement had been applying the leap year condition, we have added a 'Do While' activity and moved all the previous activities ('Input Dialog' and 'If Statement') in its body. As the condition of the Do While, we have specified the opposite of the condition is the 'If Statement' activity, as we wanted the loop to be repeated as long as the value wasn't a leap year: not((year mod 4 = 0 and year mod 100 <> 0) or (year mod 400 = 0)). In the Flowchart, it was much easier, as we simply dragged an arrow from the 'Message Box' activity in the false branch to the 'Input Dialog' activity above.

Switch Demo

In the sequence workflow, we have used a 'For Each' activity to go through each file in the source folder, by using the 'Directory.GetFiles' method, and perform the following activities: Assign the file information to a newly created FileInfo variable Assign the file name to a String variable by applying the 'Split' method to the FileInfo variable: Split("."c)(0) Switch between the cases resulted by using the 'Substring' method to isolate the year specified in the last 4 characters of the file name (fileName.Substring(fileName.Length-4)) and copy the file in the corresponding folder by using a 'Move File' activity. In the flowchart, most of the activities are reused with some changes applied and there are a couple of new activities introduced: We have created 2 additional variables - an Array of Strings to store the file names and an Int32 variable to be used as the index to go through the Array and replace the 'For Each' activity The 'Flow Switch' activity looks different, but has the same functionality After the 'Flow Switch' activity checks a file and moves it to the corresponding folder, there is a 'Flow Decision' activity to check whether the value of the index variable is equal to the index of the Array. If it's smaller, the index variable is increased by 1 and the process is repeated.

Do While

It executes a specific sequence while a condition is met. The condition is evaluated after each execution of the statements. For example, a robot could perform a refresh command over a website and then check if a relevant element was loaded. It will continue the refresh - check cycle until the element is loaded.

While

It executes a specific sequence while a condition is met. The condition is evaluated before each execution of the statements. In many cases, it is interchangeable with Do While, the only difference being when the condition verification is made. But in some cases, one is preferable over the other. For example, if a Robot would play Blackjack, it should calculate the hand before deciding whether to draw another card.

Switch

It is a type of control flow statement that executes a set of statements out of multiple, based on the value of a specific expression. In other words, we use it instead of an If statement when we need at least 3 potential courses of action. This is done through the condition, which is not Boolean like in the case of If statement, but multiple.

Control Flow Overview

It is the order in which individual statements, instructions or function calls are executed or evaluated in a software project.

For Each

It performs an activity or a series of activities on each element of a collection. This is very useful in data processing. Consider an Array of integers. For Each would enable the robot to check whether each numeric item fulfills a certain condition.

General Note on Variables

Keep in mind that the list of types presented above is not a complete list, but the list of the most common types used. Other types may be used in specific situations. When browsing or searching, you will find most of them under the System and System.Collections categories. In some cases, variables are generated automatically by activities, and their types may vary - for example, an activity that locates and stores a graphic element will automatically generate a variable of UiElement type.

Arrays demo

Let's recap the Activities and Methods used We have started the project as a sequence and created the variable "stringArray" of Array of String type (System.String[]). We have used an 'Assign' activity to populate the variable with text objects. We have used a 'Log Message' activity to print each value in the Array by using its index ('stringArray(0)') To modify an element of the Array we have used an 'Assign' activity with the indexed element (stringArray(0)="Jane") We have used a 'Log Message' activity and the 'string.Join' method to print all the values in the Array.

Loops

Loops are repetitions of a set of operations based on a given condition. In UiPath, the most important loops are: Do While While For Each

Note on Generic Variable

Please remember that GenericValue variables are a temporary solution at most. When it becomes clear what the data type should be, we strongly recommend that you change it to the specific type.

What is Generic Variables?

The GenericValue (UiPath.Core.GenericValue) variable is a type of variable particular to UiPath that can store any kind of data, including text, numbers, dates, and arrays.UiPath Studio has an automatic conversion mechanism of GenericValue variables, which you can guide towards the desired outcome by carefully defining their expressions. Please note that the first element in your expression is used as a guideline for what operation Studio performs. For example, when you try to add two GenericValue variables, if the first one in the expression is defined as a String, the result is the concatenation of the two. If it is defined as an Integer, the result is their sum.

The control flow statements

The activities and methods used to define the decisions to be made during the execution of a workflow. The most common control flow statements are the if/else decision, the loops and the switch. Let's focus on them one by one.

Array Variables

The array variable is a type of variable that enables storing multiple values of the same data type. Think of it as a group of elements with a size that is defined at creation, and each item can be identified by its index. In UiPath Studio, you can create arrays of numbers, of strings, of Boolean values and so on.

Creating Variables

There are 3 ways to create variables in UiPath: From the Variables panel - Open the Variables panel, select the 'Create new Variable' option, and fill in the fields as needed. When you need it, provide its name in the Designer panel or in the desired Properties field. From the Designer panel - Drag an activity with a variable field visible (i.e. 'Assign') and press Ctrl+K. Name it and then check its properties in the Variables panel. From the Properties panel - In the Properties panel of the activity, place the cursor in the field in which the variable is needed (i.e. Output) and press Ctrl+K. Name it and then check its properties in the Variables panel.

The type of automation project

There are 4 predefined types of workflows - Sequence, Flowchart, State Machine and Global Exception Handler.

Collection (category)

This category reunites all the collections of objects, with each object being identified through its index in the collection. Collections are largely used for handling and processing complex data. Some of the most encountered collections are: Array - ArrayOf<T> or System.DataType[]: used to store multiple values of the same data type. The size (number of objects) is defined at creation; List - System.Collections.Generic.List<T>: used to store multiple values of the same data type, just like Arrays. Unlike Arrays, their size is dynamic; Dictionary - System.Collections.Generic.Dictionary<TKey, TValue>: used to store objects in the form of (key, value) pairs, where each of the two can be of a separate data type.

Numeric (Category)

Used to store numbers. There are different sub-types of numerical variables: Int32 - System.Int32 (signed integers): 10, 299, -100, 0x69 Long - System.Int64 (long integers): 5435435343O, -11332424D Double - System.Double (allows decimals, 15-16 digits precision): 19.1234567891011

Variables

Variables are containers that can hold multiple data entries (values) of the same data type. For example, emailAddress can be a variable that holds the value "[email protected]". The value of a variable can change through external input, data manipulation or passing from one activity to another. Variables are configured through their properties. You can set them in the Variables panel. The main properties in UiPath are: Name Type Default Value Scope

FOR Each

We have started the project as a sequence and added the 'Select Folder' activity, that prompts the user to indicate a folder on the machine. The output of the activity is the full path stored in a String variable that we have created. We have created an Array of Strings variable and used in an 'Assign' activity in order to get the file names using the 'Directory.GetFiles' method: We have added a 'For Each' activity to go through each object (file name) in the Array and perform the following activities: Generate the new name of the file by using the 'Replace' and 'Now.ToString' methods and store it in a local String variable: filename.Replace(".pdf", Now.ToString("_yyyy_MM_dd")+".pdf"). Use a 'Log Message' activity to print the new name. Use a 'Move File' activity to change the names of the files in the folder.

What are some business scenarios in which I will use arrays?

When we want to save the names of the months to a variable When a fixed collection of bank accounts has to be stored and used in the payment process When all the invoices paid in the previous month have to be processed When the names of the employees in a certain unit have to be verified in a database

What are some business scenarios in which I will use the If statement?

Whenever there are two courses of action that are not arbitrary, an If statement will most likely be used: Checking the status of a payment (done/not done) and performing a set of operations in each case Making sure that the outcome of the previous operation in the sequence is successful Checking the balance of an account to ensure that there is enough money to pay an invoice Checking if something has happened in a system, like if an element or an image exists and performing an action based on that.

GenericValue Variables

While developing an automation process, there are situations where you are not sure what type of data will be retrieved. In order to find out, you need to run a few tests using a variable with a broad enough spectrum that can catch any type of input. This is where we recommend temporarily using GenericValue Variables.

Data Types

With some exceptions that we will discuss separately, the data types in UiPath are borrowed from VB.Net. Below are some of the most common ones used: -Numeric -Boolean -Date and Time -String -Collection -Generic Value


Set pelajaran terkait

Series 66 Chapter 9 Exam Questions

View Set

8: Concepts of Emergency and Trauma Nursing

View Set

Ch 1 Data Analytics for Accounting and Identifying the Questions (Textbook)

View Set

VTI Clin Lab: Chapter 11 Normal Red Blood Cells

View Set

Eco 13.2 Free trade: absolute and comparative advantage (higher level topic)

View Set

apush exam: period 4 (1800-1848)

View Set