I have got a Visualforce page which displays a list of attachments from a particular account and its opportunities in a table. In the table I have the ability to select some of the attachments (this functionality is done in Javascript on the page) and a wrapper of these selected attachment is created in the Apex Class.

  //Remote Action method to get the selected attachments
public static AttachmentWrapper getAttachment(String attId) {

    Attachment att = [SELECT Id, Name, Body, BodyLength FROM Attachment WHERE Id = :attId];

    AttachmentWrapper attWrapper = new AttachmentWrapper();
    attWrapper.attEncodedBody = EncodingUtil.base64Encode(att.body);
    attWrapper.attName = att.Name;
    attWrapper.attBodyLength = att.BodyLength;

    return attWrapper;          

public class AttachmentWrapper {
    public String attEncodedBody {get; set;}
    public String attName {get; set;}
    public Integer attBodyLength {get; set;}


I need the ability to sum the BodyLength of only the selected attachments. At the moment my aggregate function sum all the attachments in the table. How can I sum only the attachments that are selected in the Wrapper? Thank you in advance for your help. Sylvie

  • What have you tried so far? Are you doing this in Apex or in JavaScript? Either way, it should be a straightforward for loop with a check on each record.
    – David Reed
    Aug 28 '19 at 11:53
  • wouldn't you need a Boolean isSelected as a property of AttachmentWrapper ? The VF page would preseumably have a checkbox next to each Attachment and when a POST is made, the wrapper objects would get updated with isSelected=true for those selected
    – cropredy
    Aug 28 '19 at 21:31

I ended up by doing it in javascript as follow.

function calculateSize() {
    var total = 0;             

    j$("input[name='att_ids_group[]']:checked").each(function() {
        total += j$(this).data("size");

   if (total > "1500000"){
    j$('#alert').text('Total size of files selected cannot exceed 1,500,000 kb.');       


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.