Friday 22 March 2013




Although it is highly discourage to write a query like this, rather than listing the fields you need to retrieve, it is possible in Apex and this is how:

String table = 'Account';
Map<String, Schema.SObjectType> m = Schema.getGlobalDescribe() ;
Schema.SObjectType s = m.get(table) ;
Schema.DescribeSObjectResult r = s.getDescribe() ;
Map<String, Schema.SObjectField> fields = r.fields.getMap() ;
string soql = '';
for (String fieldName : fields.keyset()) {
    if (soql != '') {
        soql += ', ';
    }
    soql += fieldName;
}
soql = 'SELECT ' + soql + ' FROM ' + table;
System.debug(soql);
List<Account> acs = Database.query(soql);
System.debug(acs.get(0));

As you know the viewstate is limited to 135KB so loading files larger than this size (if not done properly) will give you this error:

Maximum view state size limit (135KB) exceeded. Actual view state size for this page was 395KB 

So how to write this code properly?
<apex:page standardController="Document" extensions="TestFileUploadPageControllerExtension">
    <apex:form
        <apex:pageBlock title="Attach file" mode="edit">
            <apex:pageBlockSection columns="1" >
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Select a file" for="inputFile"/>
                    <apex:inputFile id="inputFile" value="{!document.Body}" filename="{!document.Name}" fileSize="{!document.BodyLength}" contentType="{!document.ContentType}" size="40" />
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
                 
                 
            <apex:pageBlockButtons location="top">
                <apex:commandButton action="{!doAttach}" value="Attach" id="attachButton"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form
</apex:page>
public class TestFileUploadPageControllerExtension
{
    //reference to standard controller
    private ApexPages.StandardController controller;
     
    public TestFileUploadPageControllerExtension(ApexPages.StandardController controller) {
        this.controller = controller;
    }
     
    public void doAttach() {
        System.Debug('##################################');
        //get uploaded document
        Document doc = (Document)this.controller.getRecord();
        System.Debug('doc: '+doc);
         
        //create attachment from document
        Attachment a = new Attachment();
        a.Body = doc.Body;
        a.ContentType = doc.ContentType;
        a.Name = doc.Name;
        a.ParentId = '0018000000OtgFl'; //hard coded account id
         
        Database.SaveResult sr = Database.Insert(a);
        System.Debug('Attachment SaveResult: '+sr);
 
        doc.body = null; // Ensure that the blob is not included automatically in the serialized image of the standard controller.
    }
}
As noted in the code above, set the body of the new document is set to null to ensure that the blob is not included automatically in the serialized image of the standard controller.

    Links