+ Start a Discussion
m3umaxm3umax 

Upload file to attach to an email and allow user to preview file. Possible or not?

I want to implement a use case like this:

 

1. System allows a user to send an email to a client. User is prompted for a to: address, subject and body text.

2. There is an input file field and a button labelled "Upload" which allows a user to browse for a file to upload which will be attached to the email. The user is allows to repeat this step multiple times to upload multiple attachments.

3. Each time a file is uploaded, the page is refreshed and a table at the bottom of the screen lists the file names of all the files uploaded so far.

4. User can click on the file names of the uploaded files shown in the table. A new window opens with a preview of the uploaded file

5. The user can finally click the "Send" button on the form which will send the email along with all attachments uploaded.

6. The user can close the send email window at any time. If they do so, any files uploaded during the session are deleted from memory.

 

Is something like this even possible? I have been doing a lot of research on using the messaging.singleemailmessage class and can't find any examples remotely like what I want to achieve.

 

I can't get my head around where the uploaded attachments would be stored temporarily which the user is composing the email.

 

According to what I read, an attachment must have a "Parent ID". Thus, if I coded it to put the attachments uploaded against my custom object, what happens if the user aborts the email send operation? The files will still exist against the object. How would I get the system to automatically delete the files if the user aborts?

Best Answer chosen by Admin (Salesforce Developers) 
b-Forceb-Force

You need to store attachments against one of your temporary custom object,

 

If user abbort the functionality, you can capture window unload event and delet all records in your temporary custom object , It will delete all attachment associated with that record.

 

Also every when your send Email functionality will be launched , you can delete all remaining attachment against that custom object, 

 

To avoid synch issue , you can delete only those records which are older that 5  mins

 

 

Hope this trick will help u.

 

Thanks,

bForce

All Answers

b-Forceb-Force

You need to store attachments against one of your temporary custom object,

 

If user abbort the functionality, you can capture window unload event and delet all records in your temporary custom object , It will delete all attachment associated with that record.

 

Also every when your send Email functionality will be launched , you can delete all remaining attachment against that custom object, 

 

To avoid synch issue , you can delete only those records which are older that 5  mins

 

 

Hope this trick will help u.

 

Thanks,

bForce

This was selected as the best answer
m3umaxm3umax

Thanks for that. I thought it might have to be that way.

 

Can you tell me how I would "capture" the window unload event. Would that be via a trigger or javascript?

 

Also, I don't want to delete all the attachments after the email is send. I want to save the attachment to the custom object and then also create a new activity record with the text of the body of the email to keep a perfect record of all communications gone out to the customer about this custom object.

 

So when you advise me to delete all attachments associated with the custom object on window unload, that would not work. I need some way to only delete the attachments the user just uploaded during that email session. Is there a way to identify only files uploaded during a session?

b-Forceb-Force

You can capture this by simple javascript 

 

https://developer.mozilla.org/en/DOM/window.onunload

 

Thanks,

bForce