Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
dianamatache
Explorer
3,237
Introduction

This is a submission of the Enhance your bot building with templates blog post series.

In this post I will show you how to automate a process in which you have to search for keywords in Outlook emails. The emails that fit the criteria will be then collected so they can be used for other activities.

What you will learn in this tutorial:

  • How to use the Outlook Library;

  • How to search for a keyword in your emails;

  • How to use wildcards in your search;

  • How to display some details about the emails in the console;


Steps to follow:

1.Create a new workflow;

2.Import Outlook Library Scripts;

3.Add activities in your workflow;

4.Enrich the generated code by your workflow with your custom logic;

Prerequisites:

Microsoft Outlook

Desktop Studio 1.0.8.36

Instructions:

1.Create a new workflow

Create a new project and give it a name.

Go to ‘Workflow’ perspective and create a new workflow.

2.Import Outlook Library Scripts

In the workspace of your new workflow you can use different functions for accessing and manipulating Microsoft Outlook files. In order for your project to compile and run without errors, you first have to enable the Outlook Library scripts in your project:

  • go to “Scripts” perspective;

  • select “Project” tab(bottom-left corner);

  • right click anywhere in the Panel;

  • select “Include library Script”: The “Add Library Script” window pops;

  • enable “Outlook integration”;

  • click on “Save”;



3.Add activities in your workflow

The first activity that you need to add in your workflow is a Custom activity so you can use the functions in the Outlook library. You can give a name and a description for this activity, so you can distinguish better its purpose. I will name mine “SearchKeyWord”. I used the same name for its corresponding step. Using suggestive names will also allow you to better organize your code.

The second activity you should include is an End scenario which indicates that your scenario has ended.

Your workflow should now look like this:


Important: Before you can continue with the rest of the functionalities, you need to build your project. This way the script for the workflow will be generated and we can enhance it.

4.Enrich the generated code by your workflow with your custom logic

After you have built your project, you can proceed to the Scripts perspective and open the script generated by your workflow ( it will have the same name ). Search for your custom activity and step by their name ( here is where the suggestive name comes in handy). You can write the rest of the logic in this step. For this example, I’m searching for emails containing the word “test” anywhere in their body, using wildcards. The function I created contains (in comments) all the possible parameters you can use to search for an email. Please keep in mind that the search is performed in the Outlook accounts you are currently logged in.


Below you can find my code for this step and my comments for each step:
// ----------------------------------------------------------------
// Step: SearchKeyWord
// ----------------------------------------------------------------
GLOBAL.step({
SearchKeyWord: function(ev, sc, st) {
var rootData = sc.data;
ctx.workflow('searchKeyWord', '31e9eec8-fe99-4262-8db7-279548fff558');

// Initializes “Microsoft Outlook” application.
ctx.outlook.init();

var mails = [];
var i;

// Resets the working mails list.
ctx.outlook.mail.resetMailCollection();

// Search the body of email for a keyword. Other criteria are also available.
ctx.outlook.mail.searchByCriteria({
//fromEmail: "",
//subject: "",
//sender: "",
textDescription: "%test%",
//read: 0,
//hasAttachment: 0,
//date: {after : new Date("MM/JJ/AAAA"),before : new Date("MM/JJ/AAAA")},
//maxRow: 10
dontThrowExceptionIfNoMailFound: true
});

// Get the list of mail information for the mails that fit the criteria.
mails = ctx.outlook.mail.getFilteredTable();

// Build the working mails list by retrieving each mail
if (mails.length) {
for (i = 0; i < mails.length; i++) {
ctx.outlook.mail.retrieveMail({
EntryID: mails[i]['EntryID'],
StoreID: mails[i]['StoreID']
});
}

// Display some info about each email.
ctx.log("---------------------------------------------------------");
for (i = 0; i < ctx.outlook.mail.getCollectionLength(); i++) {
ctx.log("Mail no: " + i);
ctx.log("Date: " + mails[i]['Date']);
ctx.log("From: " + mails[i]['Sender']);
ctx.log("Subject: " + ctx.outlook.mail.getSubject(i));
ctx.log("---------------------------------------------------------");

}
}

// Ends “Microsoft Outlook” application.
ctx.outlook.end();

sc.endStep(); // end Scenario
return;
}
});

Conclusion

This blog post should help you to understand the use of the ‘Outlook Library’ and how to search for emails using different criteria. At the end, you should be able to understand these functionalities and use them in your scenarios.
19 Comments