So, I've created a form to allow users answer a few questions and then attach between 0 and 3 files.
During the questions, there is one that asks if they want to add attachments.
This creates a branch, where selecting "Yes" prompts them with one more question that allows them to upload files.
In Power Automate, I can use the switch to determine if they are including attachments or not.
If not, that's easy. If they select to add attachments, the attachments get uploaded to 'Onedrive for Business' and I want to create a link to the attachments.
I found that using "Compose" (Data Operation), I can then run the following:
split(outputs('Get_response_details')?['body/rb9f8e1615ba8469fba578aa206283c2e'], '"')[7]
I can then add the output to the email body and using the code editing, make it a hyperlink.
<a href="@{outputs('Attachment')}" class="editor-link">Click to view attachment</a>
This works fine. I can also do this to process three files by creating additional "Compose" actions and using the following:
split(outputs('Get_response_details')?['body/rb9f8e1615ba8469fba578aa206283c2e'], '"')[35]
split(outputs('Get_response_details')?['body/rb9f8e1615ba8469fba578aa206283c2e'], '"')[63]
And then in the body, I can add additional links, etc.
The problem!
If I configure this to be able to process up to 3 files, it expects 3, and if a user submits one file it breaks.
The question!
How can I make this more dynamic?
Thoughts include:
Can I parse the attachments info and determine how many times "name" or "link" appears and then loop it that many times to process all files regardless of how many?
Is there another way to break down how many attachments were included? If so, I could create another "switch" and then have a case for each number of attachments. This feels clunky and like bad coding though. I think a loop would be more efficient, but I am not familiar enough with JSON, and Power Automate to know where/how to do this.
Also, if there is a way to do "if/then" statements in the email to display information based on variables being set or not would be very helpful too.
For instance, if the user only submits two files, there's no reason to have a link for 3 files.
So a test like:
if attachment1 has value other than null, display link, else do not display link.
Any suggestions?
Thanks!