You need to sign in to do that
Don't have an account?
Mohammed Azarudeen
How Do I Download Invoice which has Notes and Attachment on List View
Scenario : In the ListView of Invoice Download button is placed. This will Download the Invoice PDFif the invoice has notes and attachments else notes and attachments is null for the invoice then it will download another PDF which has some contents of the invoice.
Problem : I can able to download the corresponding PDF for single invoice selection on the List View. If I select Records(Multiple selection) with notes and attachments and without notes and attachments then it is downloading the INVOICE PDF only for both record.
Below is my Code
How to deal with multiple selection of records with notes and attachments and without notes and attachments. Any help is appreciable. Thanks
Problem : I can able to download the corresponding PDF for single invoice selection on the List View. If I select Records(Multiple selection) with notes and attachments and without notes and attachments then it is downloading the INVOICE PDF only for both record.
Below is my Code
global class DownloadInvoice { private static String API_STATUS_NORMAL = '200'; webService static string getAttachmentByParentId(string sfdcId) { List<id> ids = new List<id>(); if(string.isempty(sfdcId)) { return DownloadInvoiceResponse.errorJson('Parameter sfdcId is required.'); } system.debug('SFDCid'+sfdcId); string[] idsArray = sfdcId.split(','); for(integer i=0; i<idsArray.size();i++) { ids.add(idsArray[i]); } List<Invoice__c> invList = New List<Invoice__c>(); invList = [Select id, name, Print_Invoice__c,Account__r.name from Invoice__c where ID IN: ids]; List<Attachment> attInsert = new List<Attachment>(); if(invList.size() > 0){ for(Invoice__c invc : invList){ String msg = 'Selected invoices are not eligible for Invoice Printing, please check the Account '+invc.Account__r.name; if(invc.Print_Invoice__c == false){ return DownloadInvoiceResponse.errorJson( msg ); } Blob body; ****From here taking Attachment and adding the condition**** Integer WithAttachCount = [SELECT count() FROM Attachment where ParentId IN:ids AND id !=null]; if (WithAttachCount > 0){ PageReference pdf = new PageReference('/apex/InvoicePDF?id='+invc.id); try { if(Test.IsRunningTest()){ System.debug(' ==> as Test Class <== '); body = Blob.valueOf('Some Text'); } else{ System.debug(' ==> as Apex normal Class <== '); body = pdf.getContentAsPDF(); Attachment attach = new Attachment(); attach.Body = body; attach.name= invc.name +'_Invoice_'+ Datetime.Now() +'.PDF'; attach.IsPrivate = false; attach.ParentId = invc.id; attach.contentType = 'application/pdf'; attInsert.add(attach); } } catch (VisualforceException e) { String msg2 = e.getMessage(); return DownloadInvoiceResponse.errorJson( msg2 ); } } if(WithAttachCount <= 0 || WithAttachCount == null){ PageReference pdff = new PageReference('/apex/InvoicesIfAttachIsNull?id='+invc.id); try { if(Test.IsRunningTest()){ body = Blob.valueOf('Some Text'); } else{ System.debug(' ==> as Apex normal Class <== '); body = pdff.getContentAsPDF(); Attachment attach = new Attachment(); attach.Body = body; attach.name= invc.name +'_Invoice_'+ Datetime.Now() +'.PDF'; attach.IsPrivate = false; attach.ParentId = invc.id; attach.contentType = 'application/pdf'; attInsert.add(attach); } } catch (VisualforceException e) { String msg2 = e.getMessage(); return DownloadInvoiceResponse.errorJson( msg2 ); } } } } Insert attInsert; integer totalSizeOfFiles=0; integer totalSizeAnInvoice=0; String invoiceId=''; set<String> remainingsIdsSet=new set<String>(); List<attachment> attachmentList = new List<attachment>(); //for(attachment att:[select ParentId,id,Name,Body,contenttype from attachment where ParentId IN:ids]) { for(attachment att: attInsert) { integer eachFileSize=att.Body.size(); String parentId=att.ParentId; att.contenttype='application/pdf'; if(!invoiceId.equals(parentId)){ invoiceId=parentId; totalSizeAnInvoice=eachFileSize; System.debug('--ID: '+att.id+'. ParentId: '+parentId+'. FileSize: '+eachFileSize+'. TotalInvoiceSize: '+totalSizeAnInvoice); }else if(invoiceId.equals(parentId)){ totalSizeAnInvoice=totalSizeAnInvoice+eachFileSize; System.debug('--ID: '+att.id+'. ParentId: '+parentId+'. FileSize: '+eachFileSize+'. TotalInvoiceSize: '+totalSizeAnInvoice); } if(eachFileSize<4500000 && totalSizeAnInvoice<4500000){ totalSizeOfFiles=totalSizeOfFiles+eachFileSize; System.debug('--ID: '+parentId+'. FileSize: '+eachFileSize+'. TotalFileSize: '+totalSizeOfFiles+'. HeapSize: '+Limits.getHeapSize()); if(totalSizeOfFiles>= 4500000){ System.debug('--Adding to RemIDs ID: '+parentId+'. FileSize: '+eachFileSize); remainingsIdsSet.add(parentId); }else{ attachmentList.add(att); } } } String remainingIds=null; List<String> remainingIdList=new List<String>(remainingsIdsSet); for(integer i=0;i<remainingIdList.size();i++){ if(i==0){ remainingIds=remainingIdList.get(i); }else{ remainingIds=remainingIds+','+remainingIdList.get(i); } } List<Object> dataList = new List<Object>(); for(Attachment at :attachmentList) { Map<String, String> atMap = new Map<String, String>(); atMap.put( 'Name', at.Name ); atMap.put( 'Body', EncodingUtil.base64Encode( at.body )); datalist.add( atMap ); } Map<String, Object> response = new Map<String, Object>(); response.put('status', API_STATUS_NORMAL); if( datalist != null ){ response.put('data',datalist); response.put('id', remainingIds); } return json.serialize( response ); } } }
How to deal with multiple selection of records with notes and attachments and without notes and attachments. Any help is appreciable. Thanks
See lines 43 and 71
The above lines clearly are using invoice Id as you can see.
If you want to handle multiple records that will be not a single line change. You need explain your requirements with your developer who built the code and make code changes to the way pages are built and if necessary the controller class as well.
problem here is,
If i try to download Multiple records its downloading the last one record only.
for example:
If I click on two invoices called,
It is only downloading the second Invoice with the name Invc-00000002.
How to overcome this?
My Updated code