Skip to Content
0
Jun 11, 2020 at 04:51 PM

SAP IRPA Desktop Studio Outlook Search E-mail by Date

505 Views Last edit Jun 11, 2020 at 05:08 PM 3 rev

Hi!

I have 3 questions in this post! I hope it also provides some valuable information to others.

I am trying to identify e-mails dynamically based on the current date. My code inside the custom activity node works fine if I comment out the date filter. So then I changed my scenario and included the date in the subject of the e-mail. But now I am having troubles putting the date variable inside the subject filter statement:

Below I try to concatenate PRDatafromProvider text with date which I set earlier. Perhaps the syntax is wrong, I am missing "" or ''. I tried putting the date variable between both ' and " but it did not work. I set the date variable correctly and it logs fine.

But this search will not work when I execute my workflow.

Would you be able to point out why?

ctx.outlook.mail.search( { 
                        filter : "\"" + "urn:schemas:httpmail:subject" + "\"" + 
"= 'PRDatafromProvider'" + date + " AND" + "\"" + "urn:schemas:httpmail:read" + "\"" + "= 0", 

Another question is, when I try to search e-mails based on date like below it also behaves strangely:

If I just comment out the date part search works perfectly fine! I shared the whole code on the bottom of my post. Would you be able to assist me on filtering e-mails based on date?

ctx.outlook.mail.search( {
			filter : "\"" + "urn:schemas:httpmail:subject" + "\"" + 
"= 'PRDatafromProvider'" + " AND " + "\"" +  "urn:schemas:httpmail:read" + "\"" + 
"= 0" + "\"" + "urn:schemas:httpmail:date" + "\"" + "= '20200610'",
			maxRow : 10,
			dontThrowExceptionIfNoMailFound: true
		});

Below is the full code which works fine and will get unread e-mails with a subject PRDatafromProvider and download the attachments in that e-mail if there are any then log details about e-mail and the attacment:

At this point I would also like to ask why my searchbyCriteria will not work for me. Did anyone have a similar issue?

SAPLogon760.step( {
	Outlook: function (ev, sc, st) {
		var rootData = sc.data;
		ctx.workflow('createPR', '08371aa9-cce2-4566-b2e3-f2762c26e824');
		// Outlook
		ctx.outlook.init();
		var mails = [];
		var i, j, attachments, noAttachments, path, filename, fromEmail, date;
		// Resets the working mails list.
		ctx.outlook.mail.resetMailCollection();
		
		date = ctx.getDate(),
		ctx.log(date);
		
		ctx.outlook.mail.search( {
			filter : "\"" + "urn:schemas:httpmail:subject" + "\"" + "= 'PRDatafromProvider'" + " AND " + "\"" + 
      "urn:schemas:httpmail:read" + "\"" + "= 0",
			//"urn:schemas:httpmail:date" + "\"" + "= '20200610'",
			maxRow : 10,
			dontThrowExceptionIfNoMailFound: true
		});

		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 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("---------------------------------------------------------");
				// Get the number of attachments for each email.
				noAttachments = ctx.outlook.mail.getAttachmentsCount(i);
				// Get the name of the attachments.
				attachments = ctx.outlook.mail.getAttachmentsName(i);

				if (noAttachments > 0) {
					// Save each attachment separately.
					for (j = 0; j < noAttachments; j++) {
						filename = attachments[j];
						path = ctx.options.path.log + "\\" + filename;
						ctx.outlook.mail.attachmentSave(i, path, {
							AttachmentName: filename
						});
						ctx.log('File downloaded: ' + filename);
					}
				}
			}
		}
		// Ends “Microsoft Outlook” application.
		ctx.outlook.end();
		sc.endStep(); // end Scenario
		return ;
	}
});