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
Nabeela Mubarak 9Nabeela Mubarak 9 

Hi Kindly help my VF page used in Salesforce Sites. It ks hitting view State Error. Looking for help.

//controller
global class applicationcontrollernew
{
    
    public Wrapperhospital wrapper {get; set;}
    public List<Wrapperhospital> listWrappers {get;set;}
    public Wrapperpharm wrapperpharm {get; set;}
    public List<Wrapperpharm> listpharmWrappers {get;set;}
    
    public WrapperBillObject Wrapperbill {get; set;}
    public List<WrapperBillObject> listWrappersBill {get;set;}
    public List<WrapperBillObject> attachmentlist {get;set;}
    public Patient__c p {get; set;}
    public Contact c {get;set;}
    public Contact c1{get;set;}
    Public Contact c2{get;set;}
    public Application__c app {get;set;}
   
    Public Attachment adhaar{get;set;}
    
    Public Attachment fadhaar{get;set;}
   
    
    Public Attachment madhaar{get;set;}
    Public Attachment ration{get;set;}
  
    
    Public Attachment passport{get;set;}
   
    
    
    Public Attachment Supporting{get;set;}
   
    Public Attachment hospitalpoto {get;set;}
  
    Public Attachment familypoto {get;set;}
  
    
    
    
    public applicationcontrollernew()
    {
        p = new Patient__c();
        c=  new Contact();
        c1=  new Contact();
        c2=  new Contact();  
        app= new Application__c();
        
        adhaar= new Attachment();
        fadhaar= new Attachment();
        madhaar = new Attachment();
        ration = new Attachment();
        passport = new Attachment();
        hospitalpoto = new Attachment();
        familypoto = new Attachment();
        supporting = new Attachment();
        
        
        
        listWrappers = new List<Wrapperhospital>();
        wrapper = new Wrapperhospital();
        listWrappers.add(wrapper);
        listpharmWrappers = new List<Wrapperpharm>();             
        wrapperpharm = new Wrapperpharm();
        listpharmWrappers.add(wrapperpharm);
        listWrappersBill = new List<WrapperBillObject>(); 
       
        attachmentlist  = new  List<WrapperBillObject>();
        Wrapperbill = new WrapperBillObject();
        listWrappersBill.add(Wrapperbill);
        
       
    }
    public class Wrapperhospital 
    {
        public Medical_Facility__c hospital{get;set;} // our target hospital inside Wrapper
       
        public Wrapperhospital() 
        {
            hospital = new Medical_Facility__c(); 
           // every Wrapper have hospital
        }
    }
    
    public void  addhospital() 
    { // called from vf
        Wrapperhospital wrapper = new Wrapperhospital();
        
        
        listWrappers.add(wrapper); // vf render this list with new added wrapper
        
    }
    
    
    public class Wrapperpharm
    {
        public Pharmacy__c Pharmacy{get;set;} // our target Pharmacy inside Wrapper
        public Wrapperpharm() 
        {
            pharmacy = new Pharmacy__c (); // every Wrapper have Pharmacy
        }
    }
    public void  addpharmacy() 
    { // called from vf
        Wrapperpharm  wrapperpharm  = new Wrapperpharm();
        
        
        listpharmWrappers.add( wrapperpharm); // vf render this list with new added wrapper
        
    }
    public class WrapperBillObject
    {
        public Bills_And_Payments__c bill{get;set;} // our target hospital inside Wrapper
        public Attachment billattach{get;set;}

        public WrapperBillObject() 
        {
            bill = new  Bills_And_Payments__c();
            billattach = new Attachment();
           billattach.Name= billattach.name;
          billattach.Body= billattach.body;
          
        }
    }
    public void  addbills() 
    { // called from vf
        WrapperBillObject wrapperbill = new WrapperBillObject();
        listWrappersBill.add( wrapperbill);
        
    
    }
    
   
    public PageReference save() 
    {
          
        String cert = p.CertificateNumber__c;
        
        List<Patient__c> temp = new List<Patient__c>();
        temp = [SELECT Id, CertificateNumber__c FROM Patient__c where CertificateNumber__c =:cert];
        if(temp.size()==0)
        {
            insert p;
           
        }
        
        else
        {
            p.id=temp[0].id;
            update p;
        }
         
        system.debug('Patient' +p);
       
        List<Contact> contactlist = new List<Contact>();
        
            c.Patient__c=p.id;
            c.Contact_Type__c='Applicant';
            contactlist.add(c);
            c1.Patient__c=p.id;
            c1.Contact_Type__c='Emergency Contact';
            contactlist.add(c1);            
            c2.Patient__c=p.Id;
            c2.Contact_Type__c='Emergency Contact';
            contactlist.add(c2);  
        insert contactlist;
        system.debug('Contactlist'+ contactlist);
            app.Patient__c= p.Id;
         app.Applicant_Name__c=p.Applicant_Name__c;
            insert app;
           
        List<Attachment>  attachlist = new List <Attachment>();
       
        
        Attachment adhaar  = new Attachment(parentid=app.id, Name = adhaar.name , Body = adhaar.body);
        attachlist.add(adhaar);
        
        Attachment fadhaar  = new Attachment(parentid=app.id, Name = fadhaar.name , Body = fadhaar.body);
       
         if(fadhaar.name!=null)
             {
        attachlist.add(fadhaar);
             }
            Attachment madhaar  = new Attachment(parentid=app.id, Name = madhaar.name , Body = madhaar.body);
         if(madhaar.name!=null)
             {
        attachlist.add(madhaar);
             }
          
            Attachment passport  = new Attachment(parentid=app.id, Name = passport.name, Body = passport.body);
           attachlist.add(passport);
          
            Attachment hospitalpoto = new Attachment(parentid=app.id, Name = hospitalpoto.name, Body = hospitalpoto.body);
             if(hospitalpoto.name!=null)
             {
        attachlist.add(hospitalpoto);
             }
            Attachment ration =new Attachment(parentid=app.id, Name = ration.name, Body = ration.body);
                attachlist.add(ration);
            Attachment familypoto = new Attachment(parentid=app.id, Name = familypoto.name, Body = familypoto.body);
            if(familypoto.name!=null)
            {
                attachlist.add(familypoto);
            }
        
         Attachment Supporting = new Attachment(parentid=app.id, Name = Supporting.name, Body = Supporting.body);
           
       
        if(Supporting.name!=NULL)
        {
           attachlist.add(Supporting);
        }
            insert attachlist;
            List<Medical_Facility__c> listhospitals = new List<Medical_Facility__c>();
            for (Wrapperhospital wrapper : listWrappers) 
            { 
                wrapper.hospital.Application__c= app.id;
                listhospitals.add(wrapper.hospital);
                
            }
            if(wrapper.hospital.Hospital_Name__c != NULL)
            {
            insert listhospitals;
                system.debug('Hospitals'+listhospitals);
            }
  
        
            List<Pharmacy__c> listpharmacy = new List<Pharmacy__c>();
            for (Wrapperpharm  wrapperpharm  :  listpharmWrappers ) 
            { 
                wrapperpharm.pharmacy.Application__c= app.id;
                listpharmacy.add(wrapperpharm.pharmacy);
                
            }
            if(wrapperpharm.pharmacy.Pharmacy_Name__c != NULL)
            {
            insert listpharmacy;
               system.debug('Pharmcy'+listpharmacy);  
            }
            
            
            List<Bills_And_Payments__c> listofbills = new List <Bills_And_Payments__c>();
       
          
        for (WrapperBillObject wrapperbill : listWrappersBill) 
            { 
                wrapperbill.bill.Application__c= app.id;
               
                listofbills.add(wrapperbill.bill);
            }
          if(wrapperbill.bill.Total_Bill_Amount__c!= 0)
          {
            insert listofbills;
              system.debug('Bill Inserted' +listofbills);
          }
        
         List<Attachment> listattach= new List<Attachment>();
       
        system.debug('Attachment Lsit'+listWrappersBill);
        
     for (integer i=0;i< listWrappersBill.size();i++)
        { 
           Attachment billattachment = new Attachment();
            billattachment = listWrappersBill[i].billattach;
           listWrappersBill[i].billattach.parentid= listWrappersBill[i].bill.id;
            listattach.add(listWrappersBill[i].billattach);
            
            system.debug('Before Inserted' +listattach);
            
        }
        if(listattach.size()>0)
        {
           insert listattach;
            system.debug('Attached list'+listattach);
        }
          
        System.debug('Insert Done');
            PageReference PageRef = new PageReference('/apex/sucess');
            p.clear();
            app.clear();
            return PageRef;
     }




 
Khan AnasKhan Anas (Salesforce Developers) 
Hi Nabeela,

Greetings to you!

In order to avoid this error, minimize your pages' view state. You can optimize your Apex controller code and remove any superfluous Visualforce components used.

Please refer to the below knowledge articles which might help you further with the above issue.

https://help.salesforce.com/articleView?id=000321547&type=1&mode=1 (https://help.salesforce.com/articleView?id=000321547&type=1&mode=1)

https://help.salesforce.com/articleView?id=000323828&type=1&mode=1 (https://help.salesforce.com/articleView?id=000323828&type=1&mode=1)

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Nabeela Mubarak 9Nabeela Mubarak 9
@Khan Anas

Hi Khan Anas,

I followed the Blog and Cleared most of my lists - That didt help me

I used keywords like Static and Transient, that also dint help me.  Getting an error" Attempt ot de reference a null Object".

Kindly help me if you can review my code posted.