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
ChinnoduChinnodu 

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

Hi,

I am getting bellow error.

Hi, 
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.

Thanks,
Chinnu
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.setToAddresses(sendTo);
        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:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm

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.setToAddresses(sendTo);
        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