Get Outlook Mail Messages - How do you sort emails filtered by oldest to newest

Hi all,

Just wondering how to use the get outlook mail messages activity to filter the emails from oldest to newest. Can this be done in the filter property, example of the current filter code below:

@SQL=(urn:schemas:httpmail:subject like ‘TestJob%’ or urn:schemas:httpmail:subject like ‘%TestJob2%)’

Thanks

I’d like to always just read the oldest 5 emails pls

Hello @TRX,
The Get Outlook Mail Messages activity stores the email in a MailMessage list and you can sort this list by date and time stamps in the header property.

(From email In emailMessages Order By email.Headers("Date")).ToList

Try this workflow. SortEmails.xaml (5.4 KB)

1 Like

Getting Email list - Copy.xaml (7.8 KB)

Hi,

It didn’t seem to work with the suggested syntax. Attached is my workflow as it stands, any help would be much appreciated. Thanks

Hi,

The issue with doing it this way is that the process is only grabbing the top 5 (latest emails) and then processing them oldest to newest, as opposed to grabbing the bottom 5 emails in the inbox to process. The code needs to be applied at the start before the list is even filtered I feel.

Hi, It pulls the top 5 emails because the top property in the Get Outlook Mail Messages might be set to 5.

You need to set the number of emails that you want to get. You could use int32.Maxvalue if you don’t know this count. You can then sort the emails in decreasing order by date and loop 5 times to process the 5 oldest emails.

Please refer this knowledge base for more information on how to use Get Outlook Mail Messages.

Edit: Missed the link.

Replied from phone, excuse typos.

1 Like

Hi,

We unfortunately can only process 5 emails at a time due to how the whole process is scheduled.

We could use the maxvalue if required though. How would we go about sorting the emails in decreasing order by date and looping 5 times please?

Hi,
This updated example workflow should help you create the loop. As in my previous example, the workflow gets the top 5 emails and loops through each email.

You may have to modify the top and filter properties to your need. SortEmails.xaml (10.9 KB)

Getting Email list - test.xaml (16.7 KB)

Hi,

I’ve updated the process as per attached, for some reason it is not picking up the very last email each time upon processing (ie. it is starting from the second email from the bottom of the list). Any ideas please?

Hi,

I managed to solve this by adding an IF statement (condition emailcount<5) within the ‘For Each’, this replaced the ‘While’ loop.

Hi,
Having a loop inside another loop using the same counter variable could have caused this issue for you. Glad to hear that using a foreach loop with an IF condition resolved this for you. :slight_smile:

Please consider marking a post as resolution that best matches your issue description (topic title). This will help other readers find the solution for the problem in the post title. The post you marked as solution is for the issue you had with the loops and for sorting email from oldest to newest.

Aravind

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.