0

How can I get the sum of all the length of select Options document I only want the value of the Selected Documents.

docNames = new List<SelectOption>();   

for (Document d : [
    SELECT Id, Name , Body, BodyLength
    FROM Document
    WHERE (FolderId =: UserInfo.getUserId() OR FolderId =: '00lG0000001bBXb')
    AND ContentType = 'application/pdf'
]){
    docNames.add(
        new SelectOption(d.Id,d.Name + ' - ' + String.valueof(d.BodyLength) + ' bytes')
    );
    for (Integer i=0 ; i<= docNames.size() ; i++){
        docNames.BodyLength[i] ;
    }
}

Then I want do a try and catch so I dont let the user submit PDF files that sums up more than 5MB

try {
    if(d.BodyLength/1048576 >= 5) {
        ApexPages.addmessage(new ApexPages.message(
            ApexPages.severity.INFO,
            'The size of the file you are trying to upload is more than 5 MB. Please select another file.')
        );
        System.debug(' size > 5mb');
    }
}
catch(Exception pokemon){ // gotta catch em all!
    System.debug('ERROR:' + e);
}
10
  • It is very difficult to understand what you are trying to accomplish. – Adrian Larson Feb 11 '16 at 0:11
  • @AdrianLarson You cant submit more than 5 MB of documents to any outside API from salesforce, I wanna catch that before sending that out by using the bodylenght. I get the value on each document now, I just need to add them up together, I dotn know how to make that for loop to add up bodylenght – Metawaa Feb 11 '16 at 0:23
  • What type of Exception are you actually expecting? Are you having any problems with your code? You don't actually ask a question as is. – Adrian Larson Feb 11 '16 at 0:25
  • 1
    But why the try/catch? It seems completely irrelevant to what you are trying to accomplish. A good question will only contain what is relevant in addition to making the intention clear. You are also missing a lot of context. Is this on a Visualforce Page? How are they being selected in the first place? – Adrian Larson Feb 11 '16 at 0:34
  • 1
    heap errors can't be caught; hence why you shouldn't include body in the query – cropredy Feb 11 '16 at 18:38
1

It's a bit unclear what you are trying to do. It looks like you are building up a List of SelectOptions and then also trying to track the Body size of all those documents.

One likely cause of heap size issues here is retrieving the actual Body of the Document records in Apex when you only need to know the BodyLength.

Try something like:

List<SelectOption> docNames = new List<SelectOption>();
long totalBodyLength = 0;
Id exFolderId = '00lG0000001bBXb';

for (Document d : [SELECT Id, Name, BodyLength
                   FROM Document
                   WHERE (FolderId = :UserInfo.getUserId() OR FolderId = :exFolderId)
                       AND ContentType = 'application/pdf'] ){

    if(d.BodyLength >= 5 * 1048576) {
        // Exclude large documents that would exceed the Heap size
        // Could probably handle via the WHERE clause
        continue;
        // Note that in theory it should even be possible to create a Document > 5MB
    }

    docNames.add(
        new SelectOption(d.Id, d.Name + ' - ' + String.valueof(d.BodyLength) + ' bytes')
    );
    totalBodyLength += d.BodyLength;
}

Consider using Limits.getHeapSize() and Limits.getLimitHeapSize() to check for current and available heap. As Adrian comments, you won't be able to use the entire heap for the document body.

8
  • Thank you for respond, Here what I am doing now, I am getting the d.bodylength to show on each document in visual force page, then I do Regular Expression on Javascript and try to sum up every single one that has been selected. do you think that is reasonable way to calculate how big are the total of selected PDF files (Document)? – Metawaa Feb 11 '16 at 22:36
  • So your goal is to prevent users from selecting more documents than can held in the heap at any one time? – Daniel Ballinger Feb 11 '16 at 23:04
  • Yes, that is exactly what I want to do. I am sending all files outside of salesforce into my own server make them as one big file and getting them returned, but if its bigger than 5 MB or 6MB i cant remember, salesforce will throw heap error on page – Metawaa Feb 11 '16 at 23:17
  • How are you sending the documents? There are ways to increase the heap size. For instance, there is a pilot on getting a larger heap size on @future calls. – Daniel Ballinger Feb 11 '16 at 23:25
  • 1
    @DanielBallinger The remaining heap size will be slightly less because you are using some. Might be worth mentioning how to compare against the remaining heap space. – Adrian Larson Feb 12 '16 at 18:20

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.