SAP Build Process Automation : Understand the Outlook library to read mails
Hello bot builders !
With SAP Build Process Automation, it is quite easy to build bots to automate your processes. But when it comes to Outlook, things might become a little complicated if you don’t have a quite good understanding of how this library is designed and how it works.
This blog post (which is related to this one, and can be considered as a part of this serie) is here to help !
Overview of the library
Let’s do a quick exercise. Imagine you’re at home and you need to do some homework. You enter the library and here is what you see :
First thing to do : pick the books you need.
Then you sit at your desk, with a stack of books in front of you:
What’s next ? You cannot read all of them at the same time so you need to pick one book from the stack to start your work.
Now, replace the word book with mail. Here is what we get :
- Select the mails you need
- You’ve got a stack of mails
- Pick one mail to work with it
You get the idea. Outlook library works the same way : you will search for emails, and then you will work with what we call the Context Current Email (also known as CCE). Don’t forget this name, as it will be used later in this blog post.
Again, you cannot have multiple Context Current Emails, just like you cannot read multiple books at the same time : you read one book (even partially) then you pick another book and start reading.
First thing to do when you want to work with Outlook in an automation : add a dependency to Oulook library !
Open the settings of your project, and simply add the Outlook library (see below) :
Ok so now we can start working with Outlook. Using the library analogy, to be able to read some books, we must enter the library: that’s why we always use the activity Open Outlook instance before using any other Outlook activity.
And when we are done, we leave the library : we use the Release Outlook instance activity. So we get the below structure :
Ok and now ?
How to select the CCE ?
To select the mails, we use the Search Email activity : it’s the first activity in the category [irpa_outlook] Email so you cannot forget to use it !
Moreover, this activity is quite easy to use ! When you edit its properties, you have a popup where all you need to do is select what you need to search (and also in which folder, etc.) :
Note : searchCriteriasList is… a list ! So you can add as many criteria as you wish.
This Search Mail activity kind of returns a list of mail matching the criteria. It represents the stack of books.
Warning : the Search Mail activity does not exactly return a list that you can save in some variable. The result is stored internally and all the activities will work with this “hidden” result.
Wait… We have a list but we can only work with one item at a time ?
Yes ! And here is how we can do it.
Work with one email at a time
To work with these mails, we need to insert a loop in our automation. For each iteration, we make sure that the CCE exists (at the beginning of the loop) and we move to the next item in the list (at the end of the loop).
A picture is worth a thousand words :
Let’s detail this piece of automation :
- We enter the library
- We search for some mails, depending the criteria we set
- Using this loop we will be able to work with all the found mails
- We need to make sure the CCE exists, using the Is Context Current Email Not Exist activity
- We test the result of the previous instruction. If true, let’s get out from the loop
- Otherwise we go to the default branch
- Here we can do whatever we want with the CCE (get the subject, get the recipients, get the attachments, etc.)
- Once we’re done with this CCE, let’s take the next item in the list and loop
- We leave the library
|In Step 7, do NOT use the Delete Email activities, because they change the Context collection. If you want to delete multiple emails, please have a look at the documentation.|
|There are similar activities which are available to check that the CCE exists. Make sure you’re using Is Context Current Email Not Exist in that case.|
Note : all the activities you can use when the CCE exists are available under the categories emailItem and properties
And now ?
Now it’s up to you to build automations where you can work with tons of mails. For example you can get all the POs you received, and for each one of them, download the attached files and process the PO in S4 !
As you could see, there was no need to write a single line of code, and that’s why working with Outlook has never been so easy !
Note : you can also have a look here to learn the best practices about Oulook on our Help portal.
Find more information on SAP Build Process Automation :
Exchange knowledge: SAP Community | Q&A | Blog
Learn more: Webinars | Help Portal | openSAP
Explore: Product Information | Successful Use Cases
Try SAP Intelligent RPA for Free: Trial Version | Pre-built Bots
Follow us on: LinkedIn, Twitter and YouTube
Well written and very informative. Thanks for sharing
Does the search work, across the entire mailbox, or only in the main inbox folder (without searching sub folders)?
If you do not specify the folder in the search criteria, the bot will search only in the main inbox folder. If you want to search in a given subfolder, you need to set the input with the name of the subfolder.
But sometimes you just don't know upfront which sub-folders exist.
Microsoft provided in their Outlook interop dll a way to allow a search over the entire mailbox, why is it not implemented?
This is really a helpful blog I am really impressed with your work, keep it up the good work
SAP Intelligent RPA is a single unified Cloud-based solution. It includes “on-premise” automation tools as well. This is an end-to-end solution to automate steps. Most of us are familiar with “Macros and VBA Scripts” to automate certain tasks in Microsoft (MS) applications such as Excel.
Thanks a lot ! Glad to help people better understand our tools 🙂
Using the search email, How to add search criteria for subject as element. I tried it for two criteria using 'And' 'OR' logic operator. See sample on the image but they get only one email.
You cannot use OR operator in the search criteria. If you want to search emails containing "Analytics Department" or containing "Finance Department" in the subject, you need to use 2 differents Search mail activities.
I would suggest to use a common automation, where the input would be the search criterion. That way you can call the nested automation twice, using 2 different criteria.
The activity "Is Context Current Email Not Exist" has been deprecated in 2.0.14. Instead we need to use "Check Current Email" to loop through the list of emails returned after the search.
The error I am encountering - the loop iterates one time more than the number of mails in the context. Could you please assist?
Could you please check if the issue still happens with the latest version of the Outlook SDK ?
If so, please ask a question in the Community in a dedicated thread
Thanks Jerome! How can I read mails from the shared mailbox? Could you pls suggest?
You need to use the parameter StoreName of the Search Mails activity (of course, you need to have access to that mail box from your Outlook account)
Thank you! Also, passing 2 mail subjects with OR expression isn't working. Is creating a new search criterion the only way?
Yes. When it comes to that, you need to perform multiple search, and then manage the output with some logic of your own
I couldn't find the icon next to Search criteria input field. I'm using Cloud studio and irpa_outlook V1.26.72.
Just click Email Search Criteria, and then select Custom Data (at the end of the list). The UI will change so you can edit it
Thanks, It worked.
Thank you very much for this blog.
I have one doubt, the forever loop is looping around the whole mailbox but I just wanted to loop around the searched email.
Could anybody please help me in solving this issue?
Thanks in Advance