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
hoagieryderhoagieryder 

Email Trigger on Attachment

I am new to this so I assume this is a pretty simple question. I am trying to develop an e-mail alert that is sent when an Attachment is added to an Account. I am trying to get the Account name in the email. When I receive the email the subject just says: "Attachment Added: name". Attachment and Account are related through parentid, but I am having trouble returning account fields on an Attachment trigger. What am I doing wrong?  

 

 

trigger Send_email on Attachment (after insert) {
Attachment attach = trigger.new[0];
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'ja@example.com'};
mail.setToAddresses(toAddresses);
mail.setReplyTo('support@example.com');
mail.setSenderDisplayName('Salesforce Support');
mail.setSubject('Attachment Added : ' + account.name);
mail.setPlainTextBody('work please');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

}

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

account doesn't exist in your trigger, therefore when you code "account.name" that means the field "name" assocaited with the sobject type account, not the name of an instance of account.

 

You'll need to retrieve the account details through SOQL.  I'd suggest you do this up front to ensure you won't blow governor limits in the event of a bulk upload.

 

Something like the following should do it:

 

 

trigger Send_email on Attachment (after insert) {
   List<Id> accIds=new List<Id>();
   for (Attachment att : trigger.new)
   {
      accIds.add(att.parentId);
   }
   Map<Id, Account> accounts=new Map<Id, Account>();
   accounts.putAll([select id, Name from Account where id IN :accIds]);
   Attachment attach = trigger.new[0];
   Messaging.SingleEmailMessage mail = new    Messaging.SingleEmailMessage();
   String[] toAddresses = new String[] {'ja@example.com'};
   mail.setToAddresses(toAddresses);
   mail.setReplyTo('support@example.com');
   mail.setSenderDisplayName('Salesforce Support');
   Account acc=accounts.get(attach.parentId);
   if (null!=acc)
   {
      mail.setSubject('Attachment Added : ' + acc.name);
   }
   mail.setPlainTextBody('work please');
   Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}

 

 

 

 

All Answers

bob_buzzardbob_buzzard

account doesn't exist in your trigger, therefore when you code "account.name" that means the field "name" assocaited with the sobject type account, not the name of an instance of account.

 

You'll need to retrieve the account details through SOQL.  I'd suggest you do this up front to ensure you won't blow governor limits in the event of a bulk upload.

 

Something like the following should do it:

 

 

trigger Send_email on Attachment (after insert) {
   List<Id> accIds=new List<Id>();
   for (Attachment att : trigger.new)
   {
      accIds.add(att.parentId);
   }
   Map<Id, Account> accounts=new Map<Id, Account>();
   accounts.putAll([select id, Name from Account where id IN :accIds]);
   Attachment attach = trigger.new[0];
   Messaging.SingleEmailMessage mail = new    Messaging.SingleEmailMessage();
   String[] toAddresses = new String[] {'ja@example.com'};
   mail.setToAddresses(toAddresses);
   mail.setReplyTo('support@example.com');
   mail.setSenderDisplayName('Salesforce Support');
   Account acc=accounts.get(attach.parentId);
   if (null!=acc)
   {
      mail.setSubject('Attachment Added : ' + acc.name);
   }
   mail.setPlainTextBody('work please');
   Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}

 

 

 

 

This was selected as the best answer
hoagieryderhoagieryder

Thanks, works perfect!

prhannonprhannon

Would it be possible to trigger an alert when an email fails to pass through the client due to size limitations?

bob_buzzardbob_buzzard

Can you clarify what you mean by the client?

sruthi.458sruthi.458

Hi friends,

 

Can you post the test code of above Trigger...

 

Regards,

Srikanth

EssaySeaEssaySea
I know this is an old post, but this is exactly what I need to figure out how to do. My object would be a custom object named "Invoices" as opposed to the Account object and I'd like a nw Note to trigger as well, but otherwise the same need.
The problem is, I've never used code with Salesforce before. I wouldn't know where to put it and what edits to code would be necessary to include the "New Note" action as well.

Any help would be appreciated.

Thanks,
Shane