+ Start a Discussion
Sangeetha TSangeetha T 

I wanted to convert a Javascript button that downloads all the attachments of the Email Message

Need help in writing a component that downloads all the attachments of the email message 

Altenative of javascript to work in lightning 
ShivankurShivankur (Salesforce Developers) 
Hi Sangeetha,

Please refer to the code implementation over below link:
https://www.forcetalks.com/blog/files-attachments-and-notes-salesforce-lightning-components-force-com/

You might need to modify few things in the same code to work for Email Message and you should be able to create a download button in lightning too.

Hope above information helps, Please mark as Best Answer so that it can help others in the future.

Thanks.
Sangeetha TSangeetha T
Hi I did tried doing the development as follows 
But when i click on the button no action happens 

Please tell me where I'm going wrong
 
<aura:component controller = "DownloadAllFilesClass" implements="flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" access="global" >
	<lightning:button label="Download All Files" onclick ="{! c.downloadfiles }" />
    
</aura:component>
 
public class DownloadAllFilesClass {
    //https://salesforceInstanceURL/sfc/servlet.shepherd/document/download/contentdocumentIDs
    @AuraEnabled
    public static String generateDownloadFilesLines(Id EmailMessageId){
    String url = '';
    String URLInstance = String.valueOf(System.URL.getSalesforceBaseUrl().gethost());
    String ids = '';
    List<ContentDocumentLink> cdllist = [Select ContentDocumentId from ContentDocumentLink 
                                        where LinkedEntityId =: EmailMessageId];
    if(cdllist != null && cdllist.size() > 0)
    {
        for(ContentDocumentLink cdl : cdllist)
    {
        if (ids != '')
        {
            ids = ids + '/'+cdl.ContentDocumentId;
        }
        else
        {
            ids = cdl.ContentDocumentId;
        }
    }
    }
    if (ids != null && ids !='')
    {
        return 'https://'+URLInstance+'/sfc/servlet.shepherd/document/download/'+ids+'?';
    }
    else
    {
        return 'No document available';
    }
}
}
 
({
	downloadfiles : function(component, event, helper) {
        var action = component.get("c.generateDownloadFilesLines");
        action.setParams({EmailMessageId : component.get("v.RecordId")});
        action.setCallback(this,function(response){
            var state = response.getState();
            if (state === "SUCCESS"){
                if (response.getReturnValue() != 'No document available')
                {
                    var urlEvent = $A.get("e.force:navigateToURL") ;
                    urlEvent.setParams({"url":response.getReturnValue()});
                    urlEvent.fire();
                 
                }
                else
                {
                    var toastEvent = $A.get("e.force:showToast");
                    toastEvent.setParams({
                        title : 'Error',
                        Message : 'No document available',
                        duration : '50000',
                        key : 'info_alt',
                        type : 'error',
                        mode : 'pester',
                    });
                    toastEvent.fire();
                }
                    
                
            }
            else if (state === "INCOMPLETE")
            {
                //do something
            }
                else if (state === "ERROR"){
                    var errors = response.getError();
                    if(errors){
                        if (errors[0] && errors[0].message)
                        {
                            console.log("Error Message:"+errors[0].message);
                        }
                    }
                        else
                        {
                            console.log("Unknown Error");
                        }
                    }
            });
            
            $A.enqueueAction(action);
        }
})