web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / If() with Office365.Se...
Power Apps
Answered

If() with Office365.SendEmail attachments

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Using an If() statement, I'm trying to send emails which may or may not have attachments but it keeps telling me the attachments datacard cannot be null or empty:

 

Error: 

O365Email error.jpg

Code:

If(CountRows(DataCardValue44.Attachments)=0,

 

[then] Office365.SendEmail(myEmailAddress, "HTML & attachment", HTMLPOBEmail_1.HtmlText,{IsHtml: true}),

[else] Office365.SendEmail(myEmailAddress, "HTML & attachment", HTMLPOBEmail_1.HtmlText,{Attachments:Table({Name:"Septic Log", ContentBytes:First(DataCardValue44.Attachments).Value, '@odata.type':"" }), IsHtml: true}))

 

So, if the datacard has no attachment it fails, even though I've used an IF(). I thought the If() would stop at THEN but it appears to also check the ELSE for errors? I can use either the THEN or ELSE code as separate calls and they work fine it's just when they are bundle together they fail.

Note: The ELSE statement will fail if there is no attachments but works if there is...

 

Do I need to Remove then re-add back the O365 connector? If so, would this harm my App? This App is currently in use and emails are being used for other functions. This is just an extension of these functions.

 

Any help would be greatly appreciated.

Categories:
  • Verified answer
    Community Power Platform Member Profile Picture
    Microsoft Employee on at

    This response is to help anyone else who has a similar issue as I managed to solve this myself.

     

    Because the attachments are in a Form I found that if I reset the form before running the SendEmail code it works. Seems that CountRows(DataCardValue44.Attachments) = 0 can be true / false / or blank and when it was blank (which means no attachment) it was also false which would run the "add attachments option" code when it shouldn't have.

     

    I add the following BEFORE the above code and it's working now:

    ResetForm(myFormName); 

     

    Hope this helps someone else.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 411 Super User 2026 Season 2

#2
Mohsin Ali Profile Picture

Mohsin Ali 338

#3
WarrenBelz Profile Picture

WarrenBelz 256 Most Valuable Professional

Last 30 days Overall leaderboard