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
KeithJKeithJ 

Script-thrown exception when sending email

Anyone ever seen this before?

I get this error when trying to send a Messaging.SingleEmailMessage...

 

 

public void emailCVToContact(SystemContactRecord contactRecord, List<ID> attachmentIds, String fromEmailAddress, String candidateName) { Messaging.SingleEmailMessage singleCVEmail = new Messaging.SingleEmailMessage(); singleCVEmail.setReplyTo(fromEmailAddress); singleCVEmail.setSaveAsActivity(false); singleCVEmail.setSubject('Candidate CV : ' + candidateName); singleCVEmail.setDocumentAttachments(attachmentIds); singleCVEmail.setHtmlBody(' This is a test email'); singleCVEmail.setToAddresses(new String[]{contactRecord.con.Email}); //Now send the email try { List<Messaging.SendEmailResult> emailSendResults = Messaging.sendEmail(new Messaging.SingleEmailMessage[]{singleCVEmail}); //ERROR GENERATED HERE!! for(Messaging.SendEmailResult sentEmailResult : emailSendResults) { if(!sentEmailResult.isSuccess()) { //There was a problem sending the email, so print the error messages //to the system logs List<Messaging.SendEmailError> resultsErrors = sentEmailResult.getErrors(); if(resultsErrors.size() > 0) for(Messaging.SendEmailError sendMailError : resultsErrors) System.debug('***** SEND EMAIL ERROR MESSAGE: ' + sendMailError.getMessage()); } //else sent successfully regardless of post problems else { //break out of loop - only single email enabled numberOfEmailsActuallySent++; break; }//close else }//close for }catch(EmailException ee) { System.debug('***** THERE WAS AN ISSUE SEND THE QUOTE TO THE DESTINATION EMAIL: ' + ee.getMessage()); } }

 

 I've never seen this before...

Any info appreciated!

Thanks

 

Best Answer chosen by Admin (Salesforce Developers) 
KeithJKeithJ

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

KeithJKeithJ

Am I missing something?

I have done this before and didn't have issues. 

The error states: (Line 235 is the sendEmail method)

 

System.NullPointerException: Script-thrown exception

 

Class.SendCVWizardController.emailCVToContact: line 235, column 64

Class.SendCVWizardController.setupEmail: line 206, column 13 External entry point

 

 

 

 

KeithJKeithJ

I now know that the error is thrown from the setDocumentAttachments method.

I did a system.debug on the attachmentIds list parameter and there is indeed an Id in there for an attachment

but it gives me the error when I run it...

Can figure out why. Any ideas?

Thanks

KeithJKeithJ

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
tggagnetggagne

I'm getting a similar error inserting a blob.

David@DeinDealDavid@DeinDeal

Hello! 

 

I have been messing around for a while with this, my problem was I was setting File Name Attachment to null by mistake. 

 

In my case I was seetting efa.setFileName(myObjectAttachment.Descrpition); while it should be myObjectAttachment.Name

 

Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setbody(myObjectAttachment.body);

efa.setFileName(myObjectAttachment.Name);

mail.setFileAttachments(new Messaging.emailFileAttachment[]{efa});

 

In my VF I was using <apex:inputFile value="{!myObjectAttachment.Body}" size="100" title="Attachment" fileName="{!myObjectAttachment.Name}"/> to fetch proper fileName. 

 

Hope this helps.

 

Best, 

 

David.