+ Start a Discussion
Aidel BruckAidel Bruck 

How to send an email alert every time a file is uploaded

Hi, 
I need to send an email alert to a couple of users every time a file is uploaded. 
I understand the workflows are not an option for files and I will need to write a trigger. 
I have never written anything like this and I would appreciate some quidance. 
All the best, 
 Aidel
Best Answer chosen by Aidel Bruck
Dushyant SonwarDushyant Sonwar
Hi Aidel ,

Try go through these links . This will help you to write an trigger on contentdocument to send an email.
http://www.sfdc99.com/2014/03/01/sending-emails-using-apex/
https://salesforce.stackexchange.com/questions/8479/send-email-notification-when-a-file-is-uploaded
https://developer.salesforce.com/forums/?id=906F0000000AzjVIAS

Hope this helps.

All Answers

Dushyant SonwarDushyant Sonwar
Hi Aidel ,

Try go through these links . This will help you to write an trigger on contentdocument to send an email.
http://www.sfdc99.com/2014/03/01/sending-emails-using-apex/
https://salesforce.stackexchange.com/questions/8479/send-email-notification-when-a-file-is-uploaded
https://developer.salesforce.com/forums/?id=906F0000000AzjVIAS

Hope this helps.
This was selected as the best answer
Aidel BruckAidel Bruck
Thanks!
I looked at the above and wrote the trigger. 
I have two follow up questions
1) How can I access the name of the account the file is associated with
2) How do I write a test class for this trigger
this is my code
trigger NewFileAlert on ContentDocument (after insert) 
{
// Step 0: Create a master list to hold the emails we'll send
  List<Messaging.SingleEmailMessage> mails = 
  new List<Messaging.SingleEmailMessage>();
  
  for (ContentDocument newDoc : Trigger.new) 
  {
      // Step 1: Create a new Email
      Messaging.SingleEmailMessage mail = 
      new Messaging.SingleEmailMessage();
    
      // Step 2: Set list of people who should get the email
      List<String> sendTo = new List<String>();
      sendTo.add(list of all addresses)
      mail.setToAddresses(sendTo);
    
      // Step 3: Set who the email is sent from
      mail.setReplyTo('myaddress');
      mail.setSenderDisplayName('Aidel Bruck');
    
      // (Optional) Set list of people who should be CC'ed
      //List<String> ccTo = new List<String>();
      //ccTo.add('business@bankofnigeria.com');
      //mail.setCcAddresses(ccTo);

      // Step 4. Set email contents - you can use variables!
      mail.setSubject('New Document Added');
      String body = 'Please note: A new document has been added/n  ';
      body += 'File name: '+ newdoc.title+'/n' ;
      body += 'Created By: '+ newdoc.createdbyid+ '/n';
      body += 'Created Date: '+ newdoc.CreatedDate+ '/n';
      body += 'link to file: '+ System.URL.getSalesforceBaseUrl().getHost()+newdoc.id;
      mail.setHtmlBody(body);
    
      // Step 5. Add your email to the master list
      mails.add(mail);
    }
  // Step 6: Send all emails in the master list
  Messaging.sendEmail(mails);
}