+ Start a Discussion

Error: Compile Error: Invalid identifier ' '. Apex identifiers must start with an ASCII letter (a-z or A-Z) followed by any number of ASCII letters (a-z or A-Z), digits (0 - 9), '$', '_'. at line 3 column 23


I am getting bellow error.

i am trying to send an email- Sending a Document as an Attachment, i am getting below error.
Anybody can help me on this , exactely where iam getting the error.

public class SendingDocasattachmentExample {
    public pagereference sendDocAttach() {
        Document doc = [SELECT Id,Name FROM Document WHERE Name = 'attachment'];
        Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
        semail.setDocumentAttachments(new ID[]{doc.id});
        semail.setSubject('Sending Document as attachemnt example');
        String[] sendTo = new String[]{'test.dm2@gmail.com'};
        semail.setPlainTextBody('Please find the attached document details');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});
        return null;

Best Answer chosen by Chinnodu
Khan AnasKhan Anas (Salesforce Developers) 
Hi Chinnu,

Greetings to you!

I don't see anything visibly wrong with the above code on line 3. You might be getting this error because of copy & paste issue. You can get characters that are not visible in the code, particularly when you copy/paste code.

Delete and re-type all the characters. Re-typing the code (not using copy/paste for anything) fixes the issue.

Also, setDocumentAttachments() is deprecated. You should use setEntityAttachments() instead. Please refer to below link for more information:

Try below code:
public class SendingDocasattachmentExample {
    public pagereference sendDocAttach() {
        Document doc = [SELECT Id,Name FROM Document WHERE Name = 'attachment'];
        Id documentIds = doc.id;
        Messaging.SingleEmailMessage semail = new Messaging.SingleEmailMessage();
        semail.setEntityAttachments(new ID[]{doc.id});
        semail.setSubject('Sending Document as attachemnt example');
        String[] sendTo = new String[]{'test.dm2@gmail.com'};
        semail.setPlainTextBody('Please find the attached document details');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});
        return null;

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas