function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MyGodItsColdMyGodItsCold 

on insert attachment trigger - can doc be emailed, then deleted?

I want a user to be able to attach a pdf to an object, have an "on insert" trigger be launched, and in that trigger, I want to email that document, then I want to delete the pdf.

 

Is this possible? Would I have to change a switch field on the object, and then have an "on update" trigger to the object do the email then delete the document?

 

 What I want to be able to say is, "No, I'm not saving that pdf in the cloud."

 

[I'm about to prototype, but would appreciate any insights. Thanks in advance.]

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

The on-insert event could probably email the record, then a @future method could delete the attachments. You'll have to do an asynchronous delete to avoid a "row is currently in trigger" error.

All Answers

sfdcfoxsfdcfox

The on-insert event could probably email the record, then a @future method could delete the attachments. You'll have to do an asynchronous delete to avoid a "row is currently in trigger" error.

This was selected as the best answer
MyGodItsColdMyGodItsCold

There is one little problem ... If the file is over a certain size, not all of it gets emailed. I tried to attach a .csv file which was 3,178K, and only 370 K was in the email.

 

Also, just deleting it in the @future code still leaves it in the recycle bin, but there's a command for that!

 

Perhaps if I ask the @future code to count by 2's to get to the national debt, it would give the trigger time to email the doc. Outside of that, is there a way to have the @future code sleep? I suppose I could take the time of now, and add 30 seconds, and have it check the time until it gets past that future time, but I'm pretty sure I'd get to the limit of # of executed lines.

 

Any hints would be well appreciated.

 

[And no, I'm not at Dreamforce, and yes - it's cold here...;-(

sfdcfoxsfdcfox

Odd. I'd have to test that. It should have emailed the entire thing; the async call won't fire until after the email is successfully queued, so the entirety of the file should be there. It might be something else that's going on there that's non-obvious.

 

Yes, you can use the Database.emptyRecycleBin function in the @future method to make sure the attachment can't be revived.

 

You'll only get 200,000 script lines before it blows up, which for counting to the national debt, you'd exceed that. But even counting to about 370,000 or so would only take about 1 second, so I doubt that would help you, either.

 

Are you emailing it by ID, or by Blob content? You could probably just load the Blob into memory, and use the addAttachment function on the EmailMessage directly.