+ Start a Discussion
Sangeetha TSangeetha T 

Urgent Help Required - Lightning component not downloading multiple attachments

Hi All 
I have created a Lightning component buttton to download attachments related to email message 
Now I'm able to fetch all attachment IDS as per debug log 
But only one file is downloading 
In Email Messages having more that one attachment only the first attachment is downloading. How to fix this 
Please help me with the code correction 

I have tried using various scenarios but could not achieve the mutiple attachments downloading
<aura:component controller ='DownloadAllFilesClass' implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction" 
                access="global" >
       
	<lightning:button label="Download All Files" onclick ="{! c.downloadfiles }" variant ="brand" iconName = "utility:download"/>
</aura:component>
 
({
    downloadfiles : function(component, event, helper) {
        var action = component.get("c.generateDownloadFilesLines");
        console.log("This component"+component.get("v.recordId"));
        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")
            {
                
            }
                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);
        }
})
 
public without sharing class DownloadAllFilesClass {
    @AuraEnabled
    public static String[] generateDownloadFilesLines(Id EmailMessageId){
    String[] URL1 =new String[]{};
    String URLInstance = String.valueOf(System.URL.getSalesforceBaseUrl().gethost());
    String URLAppend = URLInstance.SubStringBefore('.my');    
    System.debug('Appended String::'+URLAppend);
    List<ID> idslist = new List<ID>();
    List<Attachment> cdllist = [Select ID,ParentID from Attachment where ParentID =:EmailMessageId];
    System.debug('EmailMessageID:: '+EmailMessageId);
    System.debug('List of Ids related to EmailMessageID:: '+cdllist);  
    System.debug('URLInstance:: '+URLInstance); 
        if(cdllist != null && cdllist.size() > 0)
    {
             for(Attachment cdl : cdllist)
    {
             idslist.add(cdl.ID);
    }
    }
    if (idslist != null && idslist.size() > 0)
    {
        for(ID i : idslist)
        {
          String URL2 = 'https://'+URLAppend+'--c.documentforce.com/servlet/servlet.FileDownload?file='+i+'&operationContext=S1';
          system.debug('URL2::'+URL2);
          URL1.add(URL2);
        }
        system.debug('AttachmentIds:: '+idslist);
        system.debug('DownloadURL:: '+URL1); 
        
    }
        return URL1;
}
}

 
Sangeetha TSangeetha T
Hi team please help me sort out this