1

I have a requirement where I need to calculate the sum of all the attachment file size on selection of inputCheckBox. The VF page looks like below. I have used a wrapper class to display Attachment record with checkboxes.

Visualforce Page The Apex code looks like below:--- This part of the code is invoked by the constructor so that I could show Attachment related to the case on page load:

for(Attachment a : [Select Id, Body, BodyLength, Name, Description, ParentId, ContentType from Attachment where ParentId = :cases.Id and BodyLength < :value])

    caseAttachments.add(new caseAttachmentWrapper(a));

Wrapper is:

public class caseAttachmentWrapper{
public Attachment att{get;set;}
public boolean check{get;set;}
public caseAttachmentWrapper(Attachment att){
this.att = att;
check = false;
}
}

Visualforce page code looks like this: There is a pageblock table that displays attachments like this:

VF code

<apex:pageBlock title="{!$Label.Case_attachment_related}" id="block1" mode="edit">
      <apex:pageBlockSection title="{!$Label.Case_select_attachment} - {!AttachmentSize} Attachment Found" collapsible="true" columns="1" id="section1">
       <b>Total File Size : - {!totalFileSize} MB</b>
       <apex:pageBlockTable value="{!caseAttachments}" var="attached" id="table1">
         <apex:column headerValue="Select" id="column1"> <apex:inputCheckbox value="{!attached.check}" id="check">
            <!-- <apex:actionsupport event="onchange"/>-->
            </apex:inputCheckbox>
          </apex:column>
           <apex:column headerValue="Name" value="{!attached.att.Name}"/>
            <apex:column headerValue="Size" value="{!attached.att.BodyLength}"/>
           <apex:column headerValue="File Type" value="{!attached.att.ContentType}"/>
          <apex:column headerValue="View File"> <apex:outputLink value="/servlet/servlet.FileDownload?file={!attached.att.Id}" target="_blank">View File</apex:outputLink>
         </apex:column> 
       </apex:pageBlockTable>
    </apex:pageBlockSection>
<apex:pageBlock title="{!$Label.Case_attachment_related}" id="block1" mode="edit">
<apex:pageBlockSection title="{!$Label.Case_select_attachment} - {!AttachmentSize} Attachment Found" collapsible="true" columns="1" id="section1">
*<b>Total File Size : - {!totalFileSize} MB</b>*
<apex:pageBlockTable value="{!caseAttachments}" var="attached" id="table1"><apex:column headerValue="Select" id="column1"> <apex:inputCheckbox value="{!attached.check}" id="check">
<!-- <apex:actionsupport event="onchange"/>-->
</apex:inputCheckbox>
</apex:column>
<apex:column headerValue="Name" value="{!attached.att.Name}"/>
<apex:column headerValue="Size" value="{!attached.att.BodyLength}"/>
<apex:column headerValue="File Type" value="{!attached.att.ContentType}"/>
<apex:column headerValue="View File"> <apex:outputLink value="/servlet/servlet.FileDownload?file={!attached.att.Id}" target="_blank">View File</apex:outputLink>
</apex:column> 
</apex:pageBlockTable>
</apex:pageBlockSection>

--- Now when I check the boxes, the corresponding file size should be added in the total file size. Similarly, if I uncheck, the sum should decrease.

One way I was thinking is, since inputCheckBox will set the value in "check" attribute of the wrapper, may be we could utilize the same to calculate the total file size.

Please help me in this. Any help would be appreciated. Let me know if there are any questions.

1
<apex:inputCheckbox value="{!attached.check}">
  <apex:actionSupport event="onchange" action="{!calculateTotalFileSize}">
     <apex:param name="attachmentID" value="{!attached.att.Id}"
                            assignTo="{!selectedAttachmentId}"/>
    </apex:actionSupport>
</apex:inputCheckbox>

public pageRefrence calculateTotalFileSize(){
    // do your addition and subtraction based on condition
}

remove the Body field from SOQL Query,

Query Like this:

for(Attachment a : [Select Id, BodyLength, Name, Description, ParentId, ContentType 
                    From Attachment 
                    Where ParentId = :cases.Id 
                    And BodyLength < :value])
  • Thanks Prakash . However i did what you said. I ll post the code in a while. May be then we can discuss better. – the_phantom Mar 10 '15 at 12:58
0

I did solve this issue. I created a Map of Attachment Ids and their correspnding size on VF page load. On click of these checkbox I am invoking a method that gets the size of these attachments and stores in a temporary variable. I can post the code if anyone wants. Thanks all for the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.