RPA Solution for Email Automation and Invoice Processing with UiPath

09 Mar, 2020 | 6 minutes read

Robotic Process Automation (RPA) is a technology that can be applied for automation of transactional rule-based tasks where structured data and clear predefined rules and parameters are used.

The main goal of Robotic Process Automation is to ease the work of the humans by replacing the repetitive and boring tasks performed by them with a virtual workforce.

RPA solutions are easily and quickly designed, tested and implemented.

Benefits of RPA

Here are the top 5 benefits of implementing RPA software[1]:

  1. Reduced Cost: By automating tasks, cost savings of nearly 30% can be achieved. Software robots also cost less than a full-time employee.
  2. Better customer experience: Deploying RPA frees up your high-value resources to be put back on the front line ensuring your customer success.
  3. Lower operational risk: By eliminating human errors such as tiredness or lack of knowledge, RPA reduces the rate of errors thereby providing a lower level of operational risk.
  4. Improved internal processes: In order to leverage AI and RPA, companies are forced to define clear governance procedures. This, in turn, allows for faster internal reporting, onboarding, and other internal activities.
  5. It does not replace existing IT systems:  One of the biggest advantages of using a virtual workforce, or an RPA bot is that it does not require you to replace your existing systems. Instead, RPA can leverage your existing systems, the same way a human employee can.

[1] Resources: https://www.provintl.com/blog/top-5-benefits-of-robotics-process-automation-rpa-software

RPA solution for Email Automation and Invoice processing

This blog post is a tutorial that shows how a process of downloading email invoice attachments and processing data from the invoices can be executed by an RPA tool. The RPA tool used in this tutorial is Automation is going to be the future of business industries. The automation has changed the world completely and this is continuing as it made our life very easy. RPA implementation in organizations enables effective redeployment of employees to much more value-added roles. This, in turn, increases employee satisfaction and customer satisfaction. In summary, the advantages offered by RPA cannot be ignored, and its application must be considered in many different industries.

The first process that will be automated in this tutorial is the process of connecting to an email server and download the attachments from unread emails with specific subjects into a specific folder.

Add a new Flowchart in UiPath and drag new “Sequence”. Rename the sequence to “Download Email Invoices”.

(Figure 1: Flowchart in UiPath)
(Figure 1: Flowchart in UiPath)

Open the sequence and drag and drop the activity “Get IMAP Mail Messages”. The following values have to be specified in the properties of this activity:

  • Set MailFolder to “Inbox”
  • Port to 993
  • Server to “imap.gmail.com”
  • Email (can be declared as variable email)
  • Password (can be declared as variable password)
  • Top 30 (to get the first 30 emails)
  • Check Only unread messages
  • Save the Messages Output to msg variable (msg variable type must be of type (System.Collections.Generic.List<Syttem.Net.Mail.MailMessage>))
Figure 2: Get IMAP Mail Messages Activity
(Figure 2: Get IMAP Mail Messages Activity)

Since we want to iterate through each unread email with a specific subject, “For Each” activity needs to be added.  So, we need the following statement: For each mail in msg (Output Message), check if the subject contains “Invoice”. It should look like this:

Figure 3: For Each Mail Activity
(Figure 3: For Each Mail Activity)

“Save Attachment” activity can save all downloaded invoices into a specific folder. Inside Then add “Save Attachment Activity”, populate mail message and Folder Path.

Figure 4: Save Attachment Activity
(Figure 4: Save Attachment Activity)

Until now the process of downloading email attachment is completed. The next step is to extract the data from the downloaded pdf invoices and save the extracted data into an excel file. Navigate back to the Flowchart and add another “Sequence”. Rename the sequence to “PDF Data Extraction”.

(Figure 5: PDF Data Extraction Sequence)

Open the sequence and use “Assign” activity to specify the path to downloaded invoices and to get the files from the specified path. For this purpose, two variables should be created: pdfPath and pdfFiles.

Figure 6: Assign Activities
(Figure 6: Assign Activities)

In the first “Assign” activity use the pdfPath variable and specify the path where the invoices should be downloaded. In the second “Assign” activity use pdfFiles(Variable Type String[]) variable and write the following expression to get the files:  Directory.GetFiles(PdfPath,”*.pdf”).

Drag Another Sequence into the existing one, rename it to “Extract Text from Invoices” and use “For Each” activity to iterate through all downloaded files. Inside “For Each” activity Body drop “Read Pdf Text” Activity to extract the entire text of the invoices. The output of this activity should be stored in the String variable e.g. extractedText. Also, file Type Argument should be String. All this should look like this:

Figure 7: For Each FIle Activity
(Figure 7: For Each FIle Activity)

The next step is to put the extracted text into an array. To do that drag “Assign” activity create new variable arrayText (Variable Type String[]) and use this expression:

extractedText.Split(Environment.NewLine.ToArray, StringSplitOptions.RemoveEmptyEntries)

(Figure 8: For Each File Activity)

For example, the format of the invoices that will be processed is like this:

(Figure 9: Invoice Format)

Let’s say that the following values should be extracted from this invoice: Invoice Number, Invoice Date, Bill To, Address, City, State, Zip Code, Total and Description for each item. We need to use the “Assign” activity for each of these fields and to create variables for each of them. Also, the appropriate expression should be added depending on the position of the field. By using expressions in UiPath we can separate only the values that we need (e.g. from the sixth element of the array split the value that comes after “Invoice Number”).  

invoiceNumber = arrayText(6).Split({“Invoice Number”},StringSplitOptions.None)(1)

invoiceDate = arrayText(7).Split({“Date”},StringSplitOptions.None)(1)

billTo = arrayText(6).Split({“Bill To”},StringSplitOptions.None)(1).Split({“Invoice Number”},StringSplitOptions.None)(0)

address = arrayText(7).Split({“Date”},StringSplitOptions.None)(0)

city = arrayText(8).Split({“,”},StringSplitOptions.None)(0)

state = System.Text.RegularExpressions.Regex.Match(arrayText(8),”[A-Z]{2}(?=\s+\d{5})”).Value

zipCode = System.Text.RegularExpressions.Regex.Match(arrayText(8),”(?<=[A-Z]{2}\s+)\d{5}”).Value

total = arrayText(14).Split({“Total”},StringSplitOptions.None)(1)

(Figure 10: Assign Activities for Data Extraction)

We also want to extract “Product Description” for each item. To do that we have to create a separate array for the items in the table. So drag another “Assign” activity and write expression to Split the table data from “Amount“ to “Total”.

arrayTableText = extractedText.Split({“Amount”},StringSplitOptions.None)(1).Split({“Total”},StringSplitOptions.None)(0)

Drag another “Assign” activity and write an expression to put the arrayTableText into an array.

arrayTableText1 (Variable Type String[]) = arrayTableText.Split(Environment.NewLine.ToArray, StringSplitOptions.RemoveEmptyEntries)

To iterate through each product in the table, one more “For Each” activity should be added.  In the Body, drag another “Assign” activity and write an expression to extract Product Description. Also, the item Type Argument should be a String.

Description = item.Split({“)”},StringSplitOptions.None)(0)

(Figure 11: For Each Item Activity)
(Figure 11: For Each Item Activity)

The next step is to add the extracted data into DataTable. For this purpose, the “Add Data Row” activity should be added. Under Properties in ArrayRow specify the extracted data, and give a name to the Data Table eg. DataTable.

Use the “Close Application” activity to close the pdf reader application. The Selector should be modified for this activity so that the application will be closed for each open file. Eg. <wnd app=’foxitreader.exe’ cls=’classFoxitReader’ title=’*’ />

Create an excel file in which the extracted data will be saved. It should look something like this:

In order to use Excel activities, you must specify “Read Range”, so add this activity at the beginning of the process and populate WorkbookPath with the location of the excel file.

Figure 12: Read Range Activity
(Figure 12: Read Range Activity)

Drag the “Write Range” activity, select the location of the created excel file, set the starting cell to the “A2” position and as an input add DataTable.

The final step is to move the processed invoices into a separate location. To do that drag the “Start Process” activity at the beginning of “For Each” file iteration and in FileName specify the file.ToString. This actually will be the path where the executable files of the application will be opened.

Figure 13: Start Process Activity
(Figure 13: Start Process Activity)

Drag the “Move File” activity right after the “Close Application” activity. In the “From” field add the file.String and in “To” specify the path where the processed invoices should be moved.

If you run this file you will get the following output from two different invoices:

Figure 14: Output in Excel File
(Figure 14: Output in Excel File)

Conclusion:

Automation is going to be the future of business industries. Automation has changed the world completely and it will continue doing so as it made our life very easy. RPA implementation in organizations enables effective redeployment of employees to much more value-added roles. This, in turn, increases employee and customer satisfaction. To sum up, the advantages offered by RPA cannot be ignored, and its application must be considered in many different industries.