Skip to Content
Technical Articles

Monitor Outlook and Execute Actions with Pre-defined Time Interval using SAP Intelligent RPA

Hey, This is Thousif with another blog post in which I am going to discuss about how we can monitor outlook and execute the required actions in the predefined time intervals using SAP Intelligent RPA. In daily business cases, one can receive ‘n’ number of emails either for purchase orders or for any other typical business operations for processing. It is difficult and frustrating process for human to monitor the outlook after every specific period of time repeatedly and download the respective and perform operations on it, but not for the bot. For similar scenario, I developed a monitor outlook bot, which monitors outlook for every 5 minutes and download the attachments from mails if it has any. The save attachments scenario is written with reference to @Diana Matache’s blog post i.e. Save Attachments from emails. Let’s catch on how to:

Step by step as follows:

  1. Create a new project:
  • Click on File > New project.
  • Give project name, title and client name.
  • Click on save.
  • Create%20a%20project

 

  1. Select Outlook integration from library:
  • Right click on script perspective and click on Include library script to select Outlook integration.
  • Select Outlook integration checkbox and click on save button.
  • Select%20outlook%20integration

 

 

  1. Create a new workflow:
  • Right click on global to create a new workflow.
  • Give the workflow name and click on save.
  • Search for init outlook, followed by a custom activity (in which we will add code according to our business requirement). For this case, I am just saving attachments. Hence, given the name “monitorAndSaveAttachments”.
  • Search for release outlook activity and link it to “monitorAndSaveAttachments” custom activity.
  • Your workflow should be like this:
  • monitorAndSaveAttachmentsScenario

 

 

4. Click on Build to generate the code:

  • Once we click on build following code will get generated.
  • You should be able to see the following structure.

5. Set Interval in Global script:

  • Go to the Global script and go to the line 30 and add the following code.
setInterval(function(){
		ctx.log("5 mins completed");
		var rootData = ctx.dataManagers.rootData.create();
		GLOBAL.scenarios.monitorAndSaveAttachment.start(rootData);
	}, 300000);

 

  • The time interval must be defined in milli seconds. I initialized 300000 for 5 minutes, you can change according to requirement. Your code must look like the following. We are starting our defined scenario for every 5 minutes through this.

Global%20script

 

 

6. Add Logic to the Custom Activity

  • Here, you can extend the logic to your business requirement but for now, I am just saving attachments.
  • As I am saving the attachments which are received in the last 5 minutes, I am going to store the time stamp value of past fifth minute from current time.
  • var today = new Date();
    var dFromDate = new Date(now.setDate(now.getDate() - 1)); //from date
    var iTimestamp = today.getTime() -300000; //timestamp of past 5 minute

 

  • Insert the following code for filtering and saving attachments which have been received in the past 5 minutes.
  • 	var mails =[]
    	var i, j, path, filename, attachments, iAttachmentsCount;
    	// Resets the working email list.
    	ctx.outlook.mail.resetMailCollection();
    	// Search email with subject name
    	ctx.outlook.mail.searchByCriteria({
    		//subject: "Attachment",
    		//fromEmail: "",
            //subject: "",
            //sender: "",
    		hasAttachment: true,
    		date : {from : dFromDate}, //filters all mails from past one day
    		dontThrowExceptionIfNoMailFound: true
    	});
    	// Get the list of email information for the mails that fit the criteria.
    	mails = ctx.outlook.mail.getFilteredTable();
    	if (mails.length) {
    	    for (i = 0; i < mails.length; i++) {
    	        ctx.outlook.mail.retrieveMail({
    	            EntryID: mails[i]['EntryID'],
    	            StoreID: mails[i]['StoreID']
    	        });
    	    }
    	    // Display some information about each email
    	    for (i = 0; i < ctx.outlook.mail.getCollectionLength(); i++) {
    	        var dMailDate = "" + mails[i]['Date'];
    	        var iMailTS = new Date(dMailDate).getTime(); //timestamp of email
    	        iMailTS += 19800000; //converting time stamp to local zone
    	        if (iMailTS >= iTimestamp) { //true when lies in the specified time interval
    	            ctx.log("From:" + mails[i]['Sender']);
    	            ctx.log("Subject:" + ctx.outlook.mail.getSubject(i));
    	            // Get the number of attachments of each email.
    	            iAttachmentsCount = ctx.outlook.mail.getAttachmentsCount(i);
    	            // Get the name of attachments
    	            attachments = ctx.outlook.mail.getAttachmentsName(i);
    	            // Download the attachments if exists.
    	            if (iAttachmentsCount > 0) {
    	                // Save each attachment separately
    	                for (j = 0; j < iAttachmentsCount; j++) {
    	                    filename = attachments[j];
    	                    path = ctx.options.path.log + "\\" + filename;
    	                    ctx.outlook.mail.attachmentSave(i, path, {
    	                        AttachmentName: filename
    	                    });
    	                }
    	            }
    	        }
    
    	    }
    	}
  • Bot will check whether current mail’s timestamp is in the defined time interval, if it is, it will perform the given action.

7. Run the bot

Bot will run automatically for every 5 minutes.

 

CONCLUSION

 

We have seen now seen that how bot can monitor the outlook and execute given actions. We can also see that bot can run itself in the given time interval and perform the given action. You can extend this scenario by sales order creation from received attachments, uploading the data to back end, and any repetitive business action in which outlook attachments are involved.

 

Thank you!

Be the first to leave a comment
You must be Logged on to comment or reply to a post.