function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Salesforce Test 5Salesforce Test 5 

Maximum view state size limit (135KB) exceeded in apex class

Hi

I have created a class for uploading file.but when  i am uplaoding any file more than 135KB its showing Maximum view state size limit (135KB) exceeded .how to overcome this..
public class Confirmation {
    public Order__c Order{get;set;}
    public  Attachment attachment {get;set;}
    public boolean showC1RecordType {get;set;}
    public boolean showC2RecordType {get;set;} 
    
    private Id c1RecordTypeId; 
    private Id c2RecordTypeId;
    
    public Confirmation(ApexPages.StandardController controller) {
        c1RecordTypeId = Schema.SObjectType.Order__c.getRecordTypeInfosByName().get('C1').getRecordTypeId();
        c2RecordTypeId = Schema.SObjectType.Order__c.getRecordTypeInfosByName().get('C2').getRecordTypeId();
        
        Order = new Order__c();
        Order = (Order__c)controller.getRecord();
        
        String isButtonClicked = Apexpages.currentPage().getParameters().get('setDefaultValues');
        if(isButtonClicked == 'true'){
            setDefaultValues();
        }
        
        showC1RecordType = false;
        showC2RecordType = false;
        attachment = new Attachment();
        
        
        if(Order.RecordTypeId == c1RecordTypeId){
            showC1RecordType = true;
            showC2RecordType = false;
        }
        else if(Order.RecordTypeId == c2RecordTypeId){
            showC1RecordType = false;
            showC2RecordType = true;
        }
        
    }
    
     public void setDefaultValues(){
        String quoteId = Apexpages.currentPage().getParameters().get('quoteId');
        Quote__c quote = [Select o.id, o.name, o.Quantity_formula__c, o.Opportunity_Product_Detail__r.Opportunity__c,
                    o.Opportunity__c, o.Record_type_name__c,o.Company_Name__c,Opportunity_Product_Detail__r.Opportunity__r.Account__c from Quote__c o WHERE O.id=:quoteId];
        //system.assert(false,quote);     
        Order.Opportunity__c = quote.Opportunity_Product_Detail__r.Opportunity__c;
        Order.Quote__c = quote.id;
       Order.Company__c = quote.Opportunity_Product_Detail__r.Opportunity__r.Account__c;
        
        if(quote.Record_type_name__c == 'C1'){
           Order.RecordTypeId = c1RecordTypeId;
        }
        else {
            Order.RecordTypeId = c2RecordTypeId;
        }
    }
    public PageReference save(){
        if(attachment.Body == null){
            Apexpages.addMessage(new Apexpages.Message(Apexpages.Severity.ERROR, ' record'));
            return null;
        }
        else{
            try{
                insert Order;
                
                attachment.OwnerId = UserInfo.getUserId();
                attachment.ParentId = Order.id;
                insert attachment;
                
                return new PageReference('/'+Order.id);
            }
            catch(DMLException e){
                ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,e.getMessage()));
                return null;
            }
        }
    }
}

 
ProlayProlay
Please use the Transient keyword while declaring the apex controller  variables. It will reduce the ViewState 
Salesforce Test 5Salesforce Test 5
I have used this this public Transient Attachment attachment {get;set;} public Transient boolean showC1RecordType {get;set;} public Transient boolean showC2RecordType {get;set;} but when i am uploading its showing error like.. System.NullPointerException: Attempt to de-reference a null object at line 55
Amol SolankiAmol Solanki
Hi Test,

I have modified your class, I just added constructor and initilized you object.
 
public class Confirmation {
    public Order__c Order{get;set;}
    public  Attachment attachment {get;set;}
    public boolean showC1RecordType {get;set;}
    public boolean showC2RecordType {get;set;} 
    
    private Id c1RecordTypeId; 
    private Id c2RecordTypeId;
    
    public Confirmation(){

        attachment = new Attachment(); 
    }

    public Confirmation(ApexPages.StandardController controller) {
        c1RecordTypeId = Schema.SObjectType.Order__c.getRecordTypeInfosByName().get('C1').getRecordTypeId();
        c2RecordTypeId = Schema.SObjectType.Order__c.getRecordTypeInfosByName().get('C2').getRecordTypeId();
        
        Order = new Order__c();
        Order = (Order__c)controller.getRecord();
        
        String isButtonClicked = Apexpages.currentPage().getParameters().get('setDefaultValues');
        if(isButtonClicked == 'true'){
            setDefaultValues();
        }
        
        showC1RecordType = false;
        showC2RecordType = false;
        attachment = new Attachment();
        
        
        if(Order.RecordTypeId == c1RecordTypeId){
            showC1RecordType = true;
            showC2RecordType = false;
        }
        else if(Order.RecordTypeId == c2RecordTypeId){
            showC1RecordType = false;
            showC2RecordType = true;
        }
        
    }
    
     public void setDefaultValues(){
        String quoteId = Apexpages.currentPage().getParameters().get('quoteId');
        Quote__c quote = [Select o.id, o.name, o.Quantity_formula__c, o.Opportunity_Product_Detail__r.Opportunity__c,
                    o.Opportunity__c, o.Record_type_name__c,o.Company_Name__c,Opportunity_Product_Detail__r.Opportunity__r.Account__c from Quote__c o WHERE O.id=:quoteId];
        //system.assert(false,quote);     
        Order.Opportunity__c = quote.Opportunity_Product_Detail__r.Opportunity__c;
        Order.Quote__c = quote.id;
       Order.Company__c = quote.Opportunity_Product_Detail__r.Opportunity__r.Account__c;
        
        if(quote.Record_type_name__c == 'C1'){
           Order.RecordTypeId = c1RecordTypeId;
        }
        else {
            Order.RecordTypeId = c2RecordTypeId;
        }
    }
    public PageReference save(){
        if(attachment.Body == null){
            Apexpages.addMessage(new Apexpages.Message(Apexpages.Severity.ERROR, ' record'));
            return null;
        }
        else{
            try{
                insert Order;
                
                attachment.OwnerId = UserInfo.getUserId();
                attachment.ParentId = Order.id;
                insert attachment;
                
                return new PageReference('/'+Order.id);
            }
            catch(DMLException e){
                ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,e.getMessage()));
                return null;
            }
        }
    }
}

NOTE: I have not tested this.Just test it and let me know if you face any more issues.
Salesforce Test 5Salesforce Test 5
Hi Amol Its not working.its showing error when i am uplaoding any file more than 135KB its showing Maximum view state size limit (135KB) exceeded
ProlayProlay
Can you try to declare the Attachment object as Static? Static objects are not getting counted in view state 
 
public static Attachment attchment {get;set;}

 
Salesforce Test 5Salesforce Test 5
I have used static but its showing error [image: Error]Error: Confirmation Compile Error: The property Attachment attachment is referenced by Visualforce Page (NewSalesConfirmation) in salesforce.com. Remove the usage and try again. at line 3 column 24
ProlayProlay
Remove the reference from your VF page
Salesforce Test 5Salesforce Test 5
Hi
Its showing same error...
 
<apex:page standardController="Purchase_Order__c" extensions="NewSalesConfirmationExtension">
    <apex:sectionHeader title="Sale Confirmation Orders" subtitle="{!purchaseOrder.Name}" />
    <apex:form >
        <apex:pageMessages ></apex:pageMessages>
        <apex:pageBlock title="Sale Confirmation Order Edit">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save"/>
                <apex:commandButton action="{!cancel}" value="Cancel"/>
            </apex:pageBlockButtons>
            
            <apex:pageBlockSection title="Information" columns="2" collapsible="false" rendered="{!showC1RecordType}">
                <apex:inputField value="{!purchaseOrder.Opportunity__c}"/>
                <apex:inputField value="{!purchaseOrder.Sale_Confirmation_Order_Number__c}" required="true"/>
                <apex:inputField value="{!purchaseOrder.GRN_needed_for_Bill_Processing__c}"/>
                <apex:inputField value="{!purchaseOrder.Sale_Confirmation_Order_Date__c}" required="true"/>
                <apex:inputField value="{!purchaseOrder.No_of_Days_for_collecting_GRN__c}"/>
                <apex:inputField value="{!purchaseOrder.Pre_shipment_Inspection_required__c}" required="true" />
                <apex:inputField value="{!purchaseOrder.Purchase_Frequency__c}" required="true" />
                <apex:inputField value="{!purchaseOrder.Customer_Price__c}"/>
                <apex:inputField value="{!purchaseOrder.Credit_Limit__c}"/>
                <apex:inputField value="{!purchaseOrder.Bill_Submission_Period__c}"/>
                <apex:inputField value="{!purchaseOrder.CurrencyIsoCode}"/>
                <!---<apex:inputField value="{!purchaseOrder.New_Flash_Aidc_Email_sent__c}"/>--->
                <apex:inputField value="{!purchaseOrder.Customer_Ordered_Quantity__c}" required="true" />
               <!--- <apex:inputField value="{!purchaseOrder.New_Flash_Email_sent__c}"/>--->
                <apex:inputField value="{!purchaseOrder.Committed_Dispatch_Date__c}" required="true"/>
                <apex:inputField value="{!purchaseOrder.Delivery_Address__c}" />
                <!---<apex:inputField value="{!purchaseOrder.PO_Uploaded__c}"/>
                <apex:inputField value="{!purchaseOrder.Try__c}"/>
                <apex:inputField value="{!purchaseOrder.CountAttachment__c}"/>--->
            </apex:PageBlockSection>
            
            <apex:pageBlockSection title="Information" columns="2" collapsible="false" rendered="{!showC2RecordType}">
                <apex:inputField value="{!purchaseOrder.Opportunity__c}"/>
                <apex:inputField value="{!purchaseOrder.Sale_Confirmation_Order_Number__c}" required="true"/>
                <apex:inputField value="{!purchaseOrder.GRN_needed_for_Bill_Processing__c}"/>
                <apex:inputField value="{!purchaseOrder.Sale_Confirmation_Order_Date__c}" required="true"/>
                <apex:inputField value="{!purchaseOrder.No_of_Days_for_collecting_GRN__c}"/>
                <apex:inputField value="{!purchaseOrder.Committed_Dispatch_Date__c}" required="true"/>
                <apex:inputField value="{!purchaseOrder.Credit_Limit__c}"/>
                <apex:inputField value="{!purchaseOrder.Customer_Ordered_Quantity__c}"/>
                <apex:inputField value="{!purchaseOrder.CurrencyIsoCode}"/>
                <apex:inputField value="{!purchaseOrder.Customer_Price__c}"/>
                
                <apex:inputField value="{!purchaseOrder.Bill_Submission_Period__c}"/>
                <apex:inputField value="{!purchaseOrder.Application_Area__c}"/>
               
                <!---<apex:inputField value="{!purchaseOrder.CountAttachment__c}"/>
                <apex:inputField value="{!purchaseOrder.Try__c}"/>
                <apex:inputField value="{!purchaseOrder.New_Flash_Email_sent__c}"/>
                 <apex:inputField value="{!purchaseOrder.New_Flash_Aidc_Email_sent__c}"/>---->
            </apex:PageBlockSection>
            
            <apex:pageBlockSection title="Quote Details" columns="1" collapsible="false">
                <apex:inputField value="{!purchaseOrder.Quote__c}"/>
                <apex:inputField value="{!purchaseOrder.Company__c}"/>
                <apex:inputField value="{!purchaseOrder.Delivery_Address__c}"  />
            </apex:PageBlockSection>
            
            <apex:pageBlockSection title="New Flash Details" columns="2" collapsible="false">
                <apex:inputField value="{!purchaseOrder.Application_Area__c}"  rendered="{!showC1RecordType}"/>
                <apex:inputField value="{!purchaseOrder.Remarks__c}"/>
            </apex:PageBlockSection>
            
            <apex:pageBlockSection title="Upload File" columns="2" collapsible="false">
                <apex:pageBlockSectionItem >
                <apex:outputLabel value="File" for="file" />
                <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}" id="file"/>
                </apex:pageBlockSectionItem>
            </apex:PageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
public class NewSalesConfirmationExtension {
    public  Purchase_Order__c purchaseOrder{get;set;}
    public static Attachment attachment {get;set;}
    public  boolean showC1RecordType {get;set;}
    public  boolean showC2RecordType {get;set;} 
    
    private Id c1RecordTypeId; 
    private Id c2RecordTypeId;
    
    
    
    public NewSalesConfirmationExtension(ApexPages.StandardController controller) {
        c1RecordTypeId = Schema.SObjectType.Purchase_Order__c.getRecordTypeInfosByName().get('C1').getRecordTypeId();
        c2RecordTypeId = Schema.SObjectType.Purchase_Order__c.getRecordTypeInfosByName().get('C2').getRecordTypeId();
        
        purchaseOrder = new Purchase_Order__c();
        purchaseOrder = (Purchase_Order__c)controller.getRecord();
        
        String isButtonClicked = Apexpages.currentPage().getParameters().get('setDefaultValues');
        if(isButtonClicked == 'true'){
            setDefaultValues();
        }
        
        showC1RecordType = false;
        showC2RecordType = false;
        attachment = new Attachment();
        
        
        if(purchaseOrder.RecordTypeId == c1RecordTypeId){
            showC1RecordType = true;
            showC2RecordType = false;
        }
        else if(purchaseOrder.RecordTypeId == c2RecordTypeId){
            showC1RecordType = false;
            showC2RecordType = true;
        }
        
    }
    
     public void setDefaultValues(){
        String quoteId = Apexpages.currentPage().getParameters().get('quoteId');
        Quote__c quote = [Select o.id, o.name, o.Quantity_formula__c, o.Opportunity_Product_Detail__r.Opportunity__c,
                    o.Opportunity__c, o.Record_type_name__c,o.Company_Name__c,Opportunity_Product_Detail__r.Opportunity__r.Account__c from Quote__c o WHERE O.id=:quoteId];
        //system.assert(false,quote);     
        purchaseOrder.Opportunity__c = quote.Opportunity_Product_Detail__r.Opportunity__c;
        purchaseOrder.Quote__c = quote.id;
        purchaseOrder.Company__c = quote.Opportunity_Product_Detail__r.Opportunity__r.Account__c;
        
        if(quote.Record_type_name__c == 'C1'){
            purchaseOrder.RecordTypeId = c1RecordTypeId;
        }
        else {
            purchaseOrder.RecordTypeId = c2RecordTypeId;
        }
    }
    public PageReference save(){
        if(attachment.Body == null){
            Apexpages.addMessage(new Apexpages.Message(Apexpages.Severity.ERROR, 'Please upload recent Purchase Order  before saving the record'));
            return null;
        }
        else{
            try{
                insert purchaseOrder;
                
                attachment.OwnerId = UserInfo.getUserId();
                attachment.ParentId = purchaseOrder.id;
                insert attachment;
                
                return new PageReference('/'+purchaseOrder.id);
            }
            catch(DMLException e){
                ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,e.getMessage()));
                return null;
            }
        }
    }
}
I have problem is when i am uploading file more than 135KB its showing error.....

 
ProlayProlay
As per the recommendations please use the AJAX API.