• Eriko Yamamura
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
Hello,
I would like to create a custom action to download record attachments as a zip in LightningExperience.
I created a controller and Visualforce to download attachments obtained using zippex https://github.com/pdalcol/Zippex.
public class ImageZipController {
    
  public final ParentObject__c parent {get; set;}
  private Id parentObjId;
    
  public ImageZipController(ApexPages.StandardController stdController) {
      this.order = (order__c)stdController.getRecord();
  }
  
    public PageReference downloadAttachments(){
	orderId = order.Id;

    String objectId = parentObjId;
    String docName =  'test.zip'; 
    List<Attachment> attachments = [SELECT Name, Body FROM Attachment WHERE ParentId = :objectId];
    System.debug('>>> attachments ' + attachments.size());
    Zippex sampleZip = new Zippex();
    for(Attachment file : attachments) {
      sampleZip.addFile('folder/' + file.Name, file.Body, null);
    }
    try{
      Document doc = new Document();
      doc.FolderId = UserInfo.getUserId();
      doc.Name = docName;
      doc.Body = sampleZip.getZipArchive();
      insert doc;
      System.debug('>>> doc ' + doc.Id);
      return new PageReference('/servlet/servlet.FileDownload?file=' + doc.Id);
    } catch ( Exception ex ) {
      System.debug('>>> ERROR ' + ex);
    }
    return null;
  }

}

But even small files will give errors.
Error: System.LimitException: Apex CPU time limit exceeded

I researched myself and found that using @future can avoid it, but I don't know what code to write.
I do not have enough knowledge as a developer.
Please Help me.