0

I have a single VisualForce Page and multiple Components, and each components have different controllers for individual operations. But in one of the VisualForce Components I want to upload a document to S3 bucket;

So to achieve this functionality without the view state limit error I have started with a scratch and created a VF page with StandardController "Document" and extension with custom controller and it worked well for me.

But same thing I want to use inside a Visualforce Component, but I am not able to use StandardController.

  • To those who voted to close, the question does not need specific code. It's a question about whether or not a standard controller can be used in a Visualforce Component. Please reopen the question. – DavidSchach Feb 21 '18 at 23:05
  • @Prateek Kumar, consider accepting/upvoting the below posts if they helped you resolve your question (even if you accept your own answer). Thanks – glls Apr 3 '18 at 17:23
0

Visualforce Components can only take custom controllers, and cannot use standard controllers with custom extensions. You will need to build the controller yourself.

See documentation for ways to minimize the view state.

  • Thanks, I got a solution for this, by creating extensions on VF page and using the same extension as a controller on VF component. – Prateek Kumar Feb 22 '18 at 6:15
0

This is my VF Page

<apex:page standardController="Document" extensions="PAP_test_vf">
    <c:PAP_test_vf ></c:PAP_test_vf>    
    <apex:form >  
        <apex:pageBlock title="Attach file" mode="edit">
            <apex:pageBlockSection columns="1" >
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Select a file" for="inputFile3"/>
                    <apex:inputFile id="inputFile3" value="{!doc3.Body}" filename="{!doc3.Name}" fileSize="{!doc3.BodyLength}" contentType="{!doc3.ContentType}" size="40" />
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>


            <apex:pageBlockButtons location="top">
                <apex:commandButton action="{!doAttach2}" value="Attach2" id="attachButton2"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>    
</apex:page>

This is my VF Component

<apex:component controller="PAP_test_vf">
    <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" required="true" value="{!doc1.Body}" filename="{!doc1.Name}" fileSize="{!doc1.BodyLength}" contentType="{!doc1.ContentType}" size="40" />
                </apex:pageBlockSectionItem>

                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="Select a file" for="inputFile2"/>
                    <apex:inputFile id="inputFile2" required="true" value="{!doc2.Body}" filename="{!doc2.Name}" fileSize="{!doc2.BodyLength}" contentType="{!doc2.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:component>

This is my Controller

public class PAP_test_vf {
    //reference to standard controller
    private ApexPages.StandardController controller;
    public Document doc1 {get;set;}
    public Document doc2 {get;set;}
    public Document doc3 {get;set;}


    public PAP_test_vf() {

        doc1 = new Document(); 
        doc2 = new Document(); 
    }
    public PAP_test_vf(ApexPages.StandardController controller) {
        this.controller = controller;
        doc3 = new Document();
    }

    public void doAttach() {        
        String url = PAP_Test_Amazon.uploadFile(doc1.Body,doc1.Name,doc1.ContentType);      
        String url2 = PAP_Test_Amazon.uploadFile(doc2.Body,doc2.Name,doc2.ContentType);
        System.debug(url);
        System.debug(url2);
        doc1.body = null;
        doc2.body = null;
    }
    public void doAttach2() {       


        String url3 = PAP_Test_Amazon.uploadFile(doc3.Body,doc3.Name,doc3.ContentType);     

        System.debug(url3);
        doc3.body = null;        
    }
}

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.