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
Mario EMario E 

How to attach Salesforce Files to Emails in an Apex Class?

I am using LEX and I want to have an Apex Class to include all the Salesforce Files that the Case has as attachments to the email and send the email.

I have a currently working Apex Class that sends the email just fine. I just need to add the attachment code and I am not sure where to start - I tried to use ContentDocumentLink but I have no clue of how to write that code. I doubt EmailFileAttachment works because I am using LEX which Salesforce Files work a bit differently.

 

Here is the current code:

// Mario E.
// Sends Case information to the Interpreting Department
// Email addresses are listed under Custom Labels: custom_VIEmailAddresses

public class custom_VICasesForwarding {

    @InvocableMethod(Label='forwardToInterpretingDept')
    public static void forwardToInterpretingDept(List<ID> caseRecords){
        Set<Id> createEmailAddresses = new Set<Id>();

        List<Case> caseList = [select Id, 
                                    Subject, 
                                    CaseNumber, 
                                    Case_Reason__c, 
                                    Case_Issue__c, 
                                    Description, 
                                    Date_Time_of_Call__c, 
                                    VINumber__c, 
                                    Customer_s_VP__c, 
                                    Outbound_Audio__c,
                                    Case_Thread_ID__c,
                              (select Id from EmailMessages)
                              from Case where Id IN :caseRecords];

        for(Case c: caseList){
            // Get Org Wide Email Address and then set it as Reply To address
            OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'younameit@emailcompany.com'];
            // Define the email
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            if ( owea.size() > 0 ) {
                email.setOrgWideEmailAddressId(owea.get(0).Id);
            }

            // Sets the TO addresses
            String[] toaddr = new String[System.Label.custom_VIEmailAddresses.split(',')];
            string[] ccaddr = new string[]{};

            // Pulls field data from Case to include in the email body
            email.setSubject('FW: ' + c.CaseNumber + ' : ' + c.Case_Reason__c);
            email.setHtmlBody('Case Number: ' + c.CaseNumber
                                + '<br/>' + 
                                'Case Reason: ' + c.Case_Reason__c 
                                + '<br/>' +
                                'Case Issue: ' + c.Case_Issue__c 
                                + '<br/>' + 
                                + '<br/>' +
                                'Date/Time: ' + c.Date_Time_of_Call__c 
                                + '<br/>' + 
                                'VI #: ' + c.VINumber__c
                                + '<br/>' +
                                'Customer\'s VP #: ' + c.Customer_s_VP__c
                                + '<br/>' +
                                'Outbound Audio #: ' + c.Outbound_Audio__c
                                + '<br/>' +
                                + '<br/>' +
                                + '<br/>' +
                                'Case Description: ' + c.Description 
                                + '<br/>' +
                                + '<br/>' +
                                + '<br/>' +
                                '<i>' + '*Note: If there are any null values, this means this information was not provided by the Case Owner prior to this submission.' + '</i>'
                                + '<br/>' +
                                + '<br/>' +
                                + '<br/>' +
                                '<span style="color: #cccccc;">' + c.Case_Thread_ID__c + '</span>'
                                + '<br/>' +
                                + '<br/>');

            email.setToAddresses(toaddr);

            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
        }
        Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();   
    }  
}
Mario EMario E

Updated the code and am getting this error:

"Method does not exist or incorrect signature: void add(Messaging.EmailFileAttachment) from the type List<Id> (98:36)"
 

// Mario E.
// Sends Case information to the Interpreting Department
// Email addresses are listed under Custom Labels: custom_VIEmailAddresses

public class custom_VICasesForwarding 
{

    @InvocableMethod(Label='forwardToInterpretingDept')
    public static void forwardToInterpretingDept(List<ID> caseRecords)
    {
        Set<Id> createEmailAddresses = new Set<Id>();

        List<Case> caseList = [select Id, 
                                    Subject, 
                                    CaseNumber, 
                                    Case_Reason__c, 
                                    Case_Issue__c, 
                                    Description, 
                                    Date_Time_of_Call__c, 
                                    VINumber__c, 
                                    Customer_s_VP__c, 
                                    Outbound_Audio__c,
                                    Case_Thread_ID__c,
                              (select Id from EmailMessages)
                              from Case where Id IN :caseRecords];

        for(Case c: caseList)
        {
            // Get Org Wide Email Address and then set it as Reply To address
            OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'younameit@emailcompany.com'];
            // Define the email
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            if ( owea.size() > 0 ) 
            {
                email.setOrgWideEmailAddressId(owea.get(0).Id);
            }

            // Sets the TO addresses
            String[] toaddr = new String[System.Label.custom_VIEmailAddresses.split(',')];
            string[] ccaddr = new string[]{};

            // Pulls field data from Case to include in the email body
            email.setSubject('FW: ' + c.CaseNumber + ' : ' + c.Case_Reason__c);
            email.setHtmlBody('Case Number: ' + c.CaseNumber
                                + '<br/>' + 
                                'Case Reason: ' + c.Case_Reason__c 
                                + '<br/>' +
                                'Case Issue: ' + c.Case_Issue__c 
                                + '<br/>' + 
                                + '<br/>' +
                                'Date/Time: ' + c.Date_Time_of_Call__c 
                                + '<br/>' + 
                                'VI #: ' + c.VINumber__c
                                + '<br/>' +
                                'Customer\'s VP #: ' + c.Customer_s_VP__c
                                + '<br/>' +
                                'Outbound Audio #: ' + c.Outbound_Audio__c
                                + '<br/>' +
                                + '<br/>' +
                                + '<br/>' +
                                'Case Description: ' + c.Description 
                                + '<br/>' +
                                + '<br/>' +
                                + '<br/>' +
                                '<i>' + '*Note: If there are any null values, this means this information was not provided by the Case Owner prior to this submission.' + '</i>'
                                + '<br/>' +
                                + '<br/>' +
                                + '<br/>' +
                                '<span style="color: #cccccc;">' + c.Case_Thread_ID__c + '</span>'
                                + '<br/>' +
                                + '<br/>');

            email.setToAddresses(toaddr);

            // Get Attachments
            List<id> ContentDocumentIds = new List<id>();
            for(ContentDocumentLink CDLink : [SELECT
                                                ContentDocumentId,
                                                LinkedEntityId
                                                FROM ContentDocumentLink
                                                WHERE LinkedEntityId=:c.id])
            {
                ContentDocumentIds.add(CDLink.ContentDocumentId);
            }
            for(ContentVersion cVersion : [SELECT
                                            Title,
                                            PathOnClient,
                                            FileType,
                                            VersionData
                                            FROM ContentVersion
                                            WHERE ContentDocumentId IN : ContentDocumentIds])
            {
                blob attachBody = cVersion.versiondata;
                system.debug('attachBody : '+attachbody+'---'+cVersion.Title+'    ||TEST.DEBUG||');
                Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
                efa.SetFileName(c.CaseNumber+'-'+cVersion.Title+'.'+cVersion.FileType);
                efa.setBody(attachBody);
                ContentDocumentIds.add(efa);
            }

            email.setEntityAttachments(ContentDocumentIds);

            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });  
        }
    }  
}
Suraj MakandarSuraj Makandar
Hi Mario E,

Please refer below code snippet to attach files:
 
List<id> ContentDocumentids = new List<id>();

for(contentDocumentLink CDLink : [SELECT LinkedEntityid, ContentDocumentid FROM contentDocumentLink WHERE LinkedEntityid=:<b>'Object id'</b>])
            {
               ContentDocumentids.add(CDLink.ContentDocumentid);  
            }
            for ( ContentVersion cversion : [SELECT title, 
                                                    PathOnClient, FileType,
                                                    versiondata 
                                              FROM contentversion 
                                              WHERE ContentDocumentId IN :ContentDocumentids  
                                               AND title LIKE 'WOCF%'])
             {
              blob WOCFbody = cversion.versiondata;
              system.debug('body : '+WOCFbody+'-----------'+cversion.title);
              Messaging.Emailfileattachment efa1 = new Messaging.Emailfileattachment();
              efa.setFileName(war2.opportunity__r.name+'-'+cversion.title+'.'cversion.FileType);
              efa.setBody(WOCFbody);
              fileAttachments.add(efa); 
             }

Link: https://developer.salesforce.com/forums/?id=9060G0000005VtDQAU

Thanks,
Suraj
Mario EMario E

Suraj,
I am not sure how to modify that and make it fit in the rest of my code except for a few such as setFileName() and "WHERE LinkedEntityId =:" <--- those I can do.


Also, I do not see "setEntityAttachments()" anywhere...

 

And I am not sure how to set these as variables: "efa" and "fileAttachments"

Mario EMario E
Here is the modified code but still not working... I need help with tweaking lines 75-105.
// Mario E.
// Sends Case information to the Interpreting Department
// Email addresses are listed under Custom Labels: custom_VIEmailAddresses

public class custom_VICasesForwarding 
{

    @InvocableMethod(Label='forwardToInterpretingDept')
    public static void forwardToInterpretingDept(List<ID> caseRecords)
    {
        Set<Id> createEmailAddresses = new Set<Id>();

        List<Case> caseList = [select Id, 
                                    Subject, 
                                    CaseNumber, 
                                    Case_Reason__c, 
                                    Case_Issue__c, 
                                    Description, 
                                    Date_Time_of_Call__c, 
                                    VINumber__c, 
                                    Customer_s_VP__c, 
                                    Outbound_Audio__c,
                                    Case_Thread_ID__c,
                              (select Id from EmailMessages)
                              from Case where Id IN :caseRecords];

        for(Case c: caseList)
        {
            // Get Org Wide Email Address and then set it as Reply To address
            OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = 'younameit@emailcompany.com'];
            // Define the email
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            if ( owea.size() > 0 ) 
            {
                email.setOrgWideEmailAddressId(owea.get(0).Id);
            }

            // Sets the TO addresses
            String[] toaddr = new String[System.Label.custom_VIEmailAddresses.split(',')];
            string[] ccaddr = new string[]{};

            // Pulls field data from Case to include in the email body
            email.setSubject('FW: ' + c.CaseNumber + ' : ' + c.Case_Reason__c);
            email.setHtmlBody('Case Number: ' + c.CaseNumber
                                + '<br/>' + 
                                'Case Reason: ' + c.Case_Reason__c 
                                + '<br/>' +
                                'Case Issue: ' + c.Case_Issue__c 
                                + '<br/>' + 
                                + '<br/>' +
                                'Date/Time: ' + c.Date_Time_of_Call__c 
                                + '<br/>' + 
                                'VI #: ' + c.VINumber__c
                                + '<br/>' +
                                'Customer\'s VP #: ' + c.Customer_s_VP__c
                                + '<br/>' +
                                'Outbound Audio #: ' + c.Outbound_Audio__c
                                + '<br/>' +
                                + '<br/>' +
                                + '<br/>' +
                                'Case Description: ' + c.Description 
                                + '<br/>' +
                                + '<br/>' +
                                + '<br/>' +
                                '<i>' + '*Note: If there are any null values, this means this information was not provided by the Case Owner prior to this submission.' + '</i>'
                                + '<br/>' +
                                + '<br/>' +
                                + '<br/>' +
                                '<span style="color: #cccccc;">' + c.Case_Thread_ID__c + '</span>'
                                + '<br/>' +
                                + '<br/>');

            email.setToAddresses(toaddr);

            // Get Attachments
            List<id> ContentDocumentIds = new List<id>();

            for(contentDocumentLink CDLink : [SELECT    LinkedEntityid, 
                                                        ContentDocumentid 
                                                        FROM contentDocumentLink 
                                                        WHERE LinkedEntityid=:c.Id])
            {
               ContentDocumentIds.add(CDLink.ContentDocumentid);  
            }
            for (contentVersion cVersion : [SELECT Title, 
                                                    PathOnClient, 
                                                    FileType,
                                                    versiondata 
                                              FROM contentVersion 
                                              WHERE ContentDocumentId IN :ContentDocumentIds])
             {
                blob attachBody = cversion.versiondata;
                system.debug('body : '+attachBody+'-----------'+cversion.title);
                Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
                efa.setFileName(c.CaseNumber+'-'+cversion.title+'.'+cversion.FileType);
                efa.setBody(attachBody);
                fileAttachments.add(efa); 
             }

            email.setEntityAttachments(fileAttachments);

            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });  
        }
    }  
}