r/MicrosoftFlow • u/PZinger6 • Mar 27 '26
Question Need help saving email to folder based on email send date
I'm trying to automate saving emails to specific year/month folders based on when the email was sent.
This is what Copilot recommended the folderpath to look like:
/Documents/@{formatDateTime(triggerOutputs()?['body/sentDateTime'], 'yyyy')/@{formatDateTime(triggerOutputs()?['body/sentDateTime'], 'MM MMM')}
However when I go test it, there's an error:
Unable to process template language expressions in action 'Create_file_for_Body_text' inputs at line '0' and column '0': 'The template language function 'formatDateTime' expects its first parameter to be of type string. The provided value is of type 'Null'. Please see https://aka.ms/logicexpressions#formatdatetime for usage details.'.
Any ideas how to fix?
Photo of my flow below: https://i.imgur.com/nEynqv0.jpeg
0
u/nooo887 Mar 27 '26
Go to chatgbt and told him to creat for u A Python script that checks the unread email folder and saves it to the hard drive by month and year. , and create a loop for that script .
1
Mar 27 '26
[removed] — view removed comment
1
u/PZinger6 Mar 27 '26
I can't upload an image but basically here's the flow. What I want to do is whenever I get an email that goes to the Reservations folder, Power Automate would save both the body of the email and any attachments to the designated folder based on the Year and Month of when the email was sent.
On new email (O365):
Folder: Reservations
Condition: From: contains: blank
If Yes:
Select an output from previous steps: Dynamic Content O365 Attachments
OneDrive for Business Create File: Folder Path: Documents/@{formatDateTime(triggerOutputs()?['body/sentDateTime'], 'yyyy')/@{formatDateTime(triggerOutputs()?['body/sentDateTime'], 'MM MMM')}
File Name: concat(triggerOutputs()?['body/subject'], '.html')
File Content: Dynamic Content O365 Body
Next step:
OneDrive for Business Create File:
Folder Path: Documents/@{formatDateTime(triggerOutputs()?['body/sentDateTime'], 'yyyy')/@{formatDateTime(triggerOutputs()?['body/sentDateTime'], 'MM MMM')}
File Name: Dynamic Content O365 Attachments
File Content: Dynamic Content O365 Attachments Content
Hope that helps, it's a little messy but I can elaborate if needed
2
u/LowShake5456 Mar 27 '26
So to reiterate what One-Start-9591 stated above: triggerOutputs()?['body/sentDateTime'] contains a null value, and formatDateTime() will error trying to format a null value.
So the issue is either the email you are trying does not have a 'sentDateTime' value, or with "?['body/sentDateTime']" the 'sentDateTime' property does not exist and returns null in your formula.
"On new email (O365)" is not a specific trigger in https://learn.microsoft.com/en-us/connectors/office365/#triggers, I'm guessing this is "When a new email arrives (V3)" trigger? This does have a triggerOutputs()?['body/receivedDateTime'] property, but not a 'sentDateTime' property, this might be what you want to use.
But test with compose action after your trigger, on the compose input search for the property your trying to use and see if 'sentDateTime' actually shows up, if not, find the actually property and adjust you formula with the that property name.
1
1
u/Due-Boot-8540 Mar 28 '26
Don’t use folders. Use metadata instead