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
TCS CRMOnDemandTCS CRMOnDemand 

SingleEmailMessage -> setDocumentAttachments method throws Compilation Error

When trying to add mulitple attachments to a emailmessage using the setDocumentAttachments method of SingleEmailMessage , Apex code throws compilation error
 

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
Attachment[] attachment = [Select Id from Attachment where ParentId =:solId];
 
if(attachment.size() > 0)
{
  ID[] attchmentId = new ID[attachment.size()];
 
  for(Integer g=0; g<attachment.size(); g++)
   attchmentId[g] = attachment[g].Id;
                       
  mail.setDocumentAttachments(attchmentId);
}  

 
Is there something being missed from above code ?
 
TCS CRMOnDemandTCS CRMOnDemand

Hi,

Use String[] as a parameter to setDocumentAttachments() method. I think its a bug in the Apex. As per documentation, above method taks array of ID. But It gives compilation error for that. setDocumentAttachments() runs successfully when string array is passed as a parameter.

One important point is: This string array should contain the ID of Document entity and not Attachment entity.

If you pass the Attachment entity Id then it will throw run time exception (INVALID_CROSS_REFERENCE_KEY).

Regards

V. R.