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
B2000B2000 

Problem With Sending SingleEmail With Attachment

I am trying to send a single email to upto ten contacts.  The email may have an attachment, if selected, so that is why I'm not using MassEmail.  I am getting an error message 'System.NullPointerException: Script-thrown exception'.  Below is the code and debug statements:

 

Thanks.

 

20090508211339.922:Class.CandidateBiddersListClass.sendEmail: line 162, column 17: *****CB: sendEmail: mail=Messaging.SingleEmailMessage[getBccAddresses=null;getCcAddresses=null;getCharset=null;getDocumentAttachments=(00P80000003ZJc7EAG);getFileAttachments=null;getHtmlBody=null;getPlainTextBody=null;getTargetObjectId=00580000001mU0tAAE;getTemplateId=00X800000017ZI4EAM;getToAddresses=null;getWhatId=null;isUserMail=false;]20090508211339.922:Class.CandidateBiddersListClass.sendEmail: line 171, column 21: ERROR-eScript-thrown exception
20090508211339.922:Class.CandidateBiddersListClass.sendEmail: line 172, column 21: ERROR-r2null
20090508211339.922:Class.CandidateBiddersListClass.T1: line 445, column 35:     returning NULL from method public System.PageReference sendEmail() in 1952 ms
System.NullPointerException: Script-thrown exception
public PageReference sendEmail() { //SEND OUT EMAILS TO CONTACT LIST if (selectedEmailTemplateId != null) { for (Contact selC : selectedContacts ) { if (selC.email != null) { Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setTargetObjectId (u.Id); mail.setTemplateId(selectedEmailTemplateId); mail.setSaveAsActivity(false); if (selectedAttachmentId != null ) mail.setDocumentAttachments(new Id[]{selectedAttachmentId});  

system.debug ('*****CB: sendEmail: mail=' + mail);

Messaging.SendEmailResult [] r; try { r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } catch (NullPointerException e) { system.debug ('ERROR-e' + e.getmessage()); system.debug ('ERROR-r2' + r); } } } } return null; }

 

Best Answer chosen by Admin (Salesforce Developers) 
micwamicwa

The answer from another post, does this also help for you too?

 

Fixed it.

Turns out that if I was using the method setDocumentAttachments and was using Id's of records that were of type Notes & Attachments, it wouldn't work.

I created a document and took it's Id, which I used in to test out the method setDocumentAttachments and it worked.

So, I had to take another approach of creating EmailFileAttachments for each attachment and setting this against my email.

Regards

 

All Answers

micwamicwa

The answer from another post, does this also help for you too?

 

Fixed it.

Turns out that if I was using the method setDocumentAttachments and was using Id's of records that were of type Notes & Attachments, it wouldn't work.

I created a document and took it's Id, which I used in to test out the method setDocumentAttachments and it worked.

So, I had to take another approach of creating EmailFileAttachments for each attachment and setting this against my email.

Regards

 

This was selected as the best answer
B2000B2000

You were correct. Thanks.