+ Start a Discussion
Srinivas223Srinivas223 

Sending email with attachments from libraries

Hello All,

I am trying to send an email to owner of the record with an attachment from libraries. Here is the Sample code
set<id> ContentDocId = new set<id>();
list<ContentWorkspaceDoc> contentFiles = new list<ContentWorkspaceDoc>();
contentFiles = [ SELECT id, ContentDocument.Id,ContentDocument.CreatedDate,ContentDocument.LastModifiedDate,ContentDocument.IsDeleted,ContentDocument.SystemModstamp,ContentDocument.Title,ContentWorkspace.Name FROM ContentWorkspaceDoc where ContentDocument.Title in : selectedFiles ];
        if(contentFiles.size()>0)
       for(ContentWorkspaceDoc content: contentFiles) 
         ContentDocId.add(content.ContentDocument.Id);             
        system.debug('------------------ ' + contentFiles);
       
        List <ContentVersion> allDocuments = new List <ContentVersion>();
        if(!ContentDocId.isEmpty())
allDocuments = [SELECT Id, Title, FileType, VersionData, isLatest, ContentDocumentId FROM ContentVersion where isLatest = true AND ContentDocumentId in:ContentDocId];
        attachments = new List<Messaging.EmailFileAttachment>{};
        
        email = new Messaging.singleEmailMessage();
       
     emailTemplate et = [select id, name, subject, body from emailTemplate where name ='Test Template'];
     email.toAddresses = new String[] { 'recordOwnerEmail' };       
    for (ContentVersion document: allDocuments) {      
     Messaging.EmailFileAttachment attachment = new                            Messaging.EmailFileAttachment();
      attachment.setBody(document.VersionData);
      attachment.setFileName(document.Title);
      attachment.setFileAttachments(document); // this line causing the error
      
    }
    
  // Error: Method does not exist or incorrect signature: void setFileAttachments(ContentVersion) from the type Messaging.EmailFileAttachment

I appreciate if you can help me.

Thank you!
IGNACIO SARMIENTO LOSADA1IGNACIO SARMIENTO LOSADA1
Hi Srinivas223,

The method is not added in the correct object. I should be in "Messaging.singleEmailMessage()". Delete line 22 and try this:
email.setFileAttachments(document);
Let me know if it works.

Thanks
Regards,
Ignacio




 
Srinivas223Srinivas223
Hello Ignacio,

Thank you for your time.
I tried it with email.setFileAttachments(document);
But still the same problem.
 
IGNACIO SARMIENTO LOSADA1IGNACIO SARMIENTO LOSADA1
Try this:
map<Id,ContentVersion> allDocuments;
if(!ContentDocId.isEmpty()){
            allDocuments = new map<Id,ContentVersion>([SELECT Id, Title, FileType, VersionData, isLatest, ContentDocumentId FROM ContentVersion where isLatest = true AND ContentDocumentId in:ContentDocId]);
}
list<id> allDocumentsIds = new list<Id>(allDocuments.keySet());
email.setEntityAttachments(allDocumentsIds);
I think this will solve your problem. You have to add the ContentVersion Ids in 'setEntityAttachments' method . Your could check more information here:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm

Let me know if that solves your issue.

Thanks
Regards,
Ignacio
IGNACIO SARMIENTO LOSADA1IGNACIO SARMIENTO LOSADA1
is it solved your issue? thanks
Srinivas223Srinivas223
No. It didnt worked.
Thank you!
Srinivas223Srinivas223
public void sendMailtoOppOwner(){
        system.debug('---------------- '+ selectedFiles);
        set<id> ContentDocId = new set<id>();
        list<ContentWorkspaceDoc> contentFiles = new list<ContentWorkspaceDoc>();
        contentFiles = [ SELECT id, ContentDocument.Id,ContentDocument.CreatedDate,ContentDocument.LastModifiedDate,ContentDocument.IsDeleted,ContentDocument.SystemModstamp,ContentDocument.Title,ContentWorkspace.Name
                           FROM ContentWorkspaceDoc where ContentDocument.Title in : selectedFiles ];
        if(contentFiles.size()>0)
            for(ContentWorkspaceDoc content: contentFiles) ContentDocId.add(content.ContentDocument.Id);             
        system.debug('------------------ ' + contentFiles);
        system.debug('------content ids --- '+ ContentDocId );
       // List<Messaging.EmailFileAttachment> attachments = new List<Messaging.EmailFileAttachment>{};
      /*  List <ContentVersion> allDocuments = new List <ContentVersion>();
        if(!ContentDocId.isEmpty())
        allDocuments = [SELECT Id, Title, FileType, VersionData, isLatest, ContentDocumentId
                              FROM ContentVersion where isLatest = true AND ContentDocumentId in:ContentDocId]; */
                              
        map<Id,ContentVersion> allDocuments;
            if(!ContentDocId.isEmpty()){
                        allDocuments = new map<Id,ContentVersion>([SELECT Id, Title, FileType, VersionData, isLatest, ContentDocumentId FROM ContentVersion where isLatest = true AND ContentDocumentId in:ContentDocId]);
            }
            list<id> allDocumentsIds = new list<Id>(allDocuments.keySet());
            system.debug('--- allDocumentsIds  --- '+ allDocumentsIds );
          //  email.setEntityAttachments(allDocumentsIds);                      
        attachments = new List<Messaging.EmailFileAttachment>{};
        
        
        List<Messaging.Emailfileattachment> fileAttachments = new    List<Messaging.Emailfileattachment>();
        //if(allDocuments.size() > 0)
       // list<id> allDocumentsIds = new list<Id>();
       // allDocumentsIds.addAll(allDocuments.keySet());
        email = new Messaging.singleEmailMessage();
       
        emailTemplate et = [select id, name, subject, body from emailTemplate where name ='Test Template'];
        email.toAddresses = new String[] { record owner email }; 
        //email.setEntityAttachments(allDocumentsIds);

        
   
  for (ContentVersion document: allDocuments.values()) {      
     Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
      attachment.setBody(document.VersionData);
      attachment.setFileName(document.Title);
      attachments.add(document.id); // i think this is problem creater
     
    }
    system.debug('------------------  ' +fileAttachments);
    
    //email.setFileAttachments(fileAttachments);
    email.setEntityAttachments(attachments); 
    
  
    }

i think im doing wrong with attachments.add(document.id);

 
IGNACIO SARMIENTO LOSADA1IGNACIO SARMIENTO LOSADA1
try this 
public void sendMailtoOppOwner(){
        	//MY CONSTANTS
        	list<String> selectedFiles = new list<String>{'MyExample'};
        	String toAddress = 'youremail@gmail.com';
        	//***************
        	
            set<id> ContentDocId = new set<id>();
            list<ContentWorkspaceDoc> contentFiles = new list<ContentWorkspaceDoc>();
            contentFiles = [ SELECT id, ContentDocument.Id,ContentDocument.CreatedDate,ContentDocument.LastModifiedDate,ContentDocument.IsDeleted,ContentDocument.SystemModstamp,ContentDocument.Title,ContentWorkspace.Name
                               FROM ContentWorkspaceDoc where ContentDocument.Title in : selectedFiles ];
            if(contentFiles.size()>0)
                for(ContentWorkspaceDoc content: contentFiles) ContentDocId.add(content.ContentDocument.Id);             
            system.debug('------------------ ' + contentFiles);
            system.debug('------content ids --- '+ ContentDocId );
                                  
            map<Id,ContentVersion> allDocuments;
            if(!ContentDocId.isEmpty()){
                allDocuments = new map<Id,ContentVersion>([SELECT Id, Title, FileType, VersionData, isLatest, ContentDocumentId FROM ContentVersion where isLatest = true AND ContentDocumentId in:ContentDocId]);
            }
            list<id> allDocumentsIds = new list<Id>(allDocuments.keySet());
            system.debug('--- allDocumentsIds  --- '+ allDocumentsIds );
            
            List<Messaging.Emailfileattachment> fileAttachments = new list<Messaging.Emailfileattachment>();
        	Messaging.singleEmailMessage email = new Messaging.singleEmailMessage();
            email = new Messaging.singleEmailMessage();
            emailTemplate et = [select id, name, subject, body from emailTemplate where name ='Test Template'];
            email.toAddresses = new String[] { toAddress }; 

            for (ContentVersion document: allDocuments.values()) {      
                Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
                attachment.setBody(document.VersionData);
                attachment.setFileName(document.Title);                    
            }
        	system.debug('------------------  ' +fileAttachments);
        
        	email.setEntityAttachments(allDocumentsIds); 
    }