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
tantoniotantonio 

Custom controller class does not save data, why?

Below is a class that has a list of all records in an object within a pageblocktable. Inline edits are enabled so I can update the records from this table if I like. I am trying to get a save button functioning that will save the entire list when I click it.

 

Right now when I click save, the data dissapears and does not persist. Please help!

 

Controller Class:

Public class listorgs  implements iterator<org__c>{
        public static List<org__c> newlist {get; set;}
        Integer i {get; set;}
        public ApexPages.StandardController listorgs {get; set;}
        public listorgs(ApexPages.StandardController controller) {
            listorgs = controller;}

        
        public listorgs(){
            newlist = [select id, name , LoginURL__c , HRM_Installed__c, Description__c , Type__c, Pod__c, org_id__c
                       from org__c order by type__c asc];
            i = 0;
                }
        Public boolean hasnext(){
            if(i>= newlist.size()){
                return false;
            } else {
                return true;
            }
        }
        Public Org__c next(){
            if(i==100){return null;}
            i++;
            return newlist[i-1];
        }
        public string page{get;set;}
        public string openpageurl {get;set;}
                
        public void onload()
        {
            page = '';
            openpageurl = '';
        }
        
        public pagereference redirect()
        {
           if( page == 'login')
            {
                openpageurl = 'http://google.com';
            }
            return null;
        }
    public pagereference save(){
                    newlist = [select id, name , LoginURL__c , HRM_Installed__c, Description__c , Type__c, Pod__c, org_id__c
                       from org__c order  by type__c asc ];
            update newlist;
            return null;
            
        }
            
}

 Visualforce page:

<apex:page controller="listorgs" sidebar="false">

    <apex:form >
    
        <Apex:pageblock >
          <apex:pageblockButtons >
            <apex:commandButton action="{!Save}" value="Save"/>
            </apex:pageblockButtons><apex:inlineEditSupport />
        <br/>
        <br/>
            
            <apex:pageblockTable value="{!newlist}" var="obj">
           
               <apex:column >
                   <apex:commandButton onclick="window.open('{!obj.LoginURL__c}')" value="Login" reRender="loginhome">
                       <Apex:outputLink value="{!obj.LoginURL__c}"/>
                   </apex:commandbutton>
               </apex:column>
                
                    

                
                <apex:column headerValue="Username" >
                    <Apex:outputfield value="{!obj.name}"/>
                    
                </apex:column>
                
                <apex:column headerValue="Type" >
                    <Apex:outputfield value="{!obj.Type__c}"/>
                    
                </apex:column>
                
                <apex:column headerValue="Description">
                    <Apex:outputfield value="{!obj.Description__c}"/>
                
                </apex:column>
                
                <apex:column headerValue="Org ID">
                    <Apex:outputfield value="{!obj.Org_ID__c}"/>
                    
                </apex:column>
                
                <apex:column headerValue="POD">
                    <Apex:outputfield value="{!obj.POD__c}"/>
                    
                </apex:column>    
                    
                
                <apex:column headerValue="HRM Installed?">
                    <Apex:outputfield value="{!obj.HRM_Installed__c}"/>
                    
                </apex:column>
                
            </apex:pageblockTable>
        </Apex:pageblock>
        
    </apex:form>
    
</apex:page>

 Thanks for the help!!

Best Answer chosen by Admin (Salesforce Developers) 
Kiran  KurellaKiran Kurella

No need to declare the variable as static. Try replacing the following line:

 

public static List<org__c> newlist {get; set;}

 

with

 

public List<org__c> newlist {get; set;}

All Answers

Kiran  KurellaKiran Kurella

You should not re-query the data in your save method. Try this save method and I hope it will resolve the issue:

 

public pagereference save() {
     update newlist;
     return null;
}

 

tantoniotantonio

Hi Codewizard!

 

I did try that prior to posting and it results in a null pointer exception:

 

System.NullPointerException: Attempt to de-reference a null object
Error is in expression '{!Save}' in component <apex:page> in page loginhome
 
 
Class.listorgs.save: line 44, column 1

 

 

Here is the modified class per your suggestions. Let me know your thoughts. Thanks!!!

Public class listorgs  implements iterator<org__c>{
        public static List<org__c> newlist {get; set;}
        Integer i {get; set;}
        public ApexPages.StandardController listorgs {get; set;}
        public listorgs(ApexPages.StandardController controller) {
            listorgs = controller;}

        
        public listorgs(){
            newlist = [select id, name , LoginURL__c , HRM_Installed__c, Description__c , Type__c, Pod__c, org_id__c
                       from org__c order by type__c asc];
            i = 0;
                }
        Public boolean hasnext(){
            if(i>= newlist.size()){
                return false;
            } else {
                return true;
            }
        }
        Public Org__c next(){
            if(i==100){return null;}
            i++;
            return newlist[i-1];
        }
        public string page{get;set;}
        public string openpageurl {get;set;}
                
        public void onload()
        {
            page = '';
            openpageurl = '';
        }
        
        public pagereference redirect()
        {
           if( page == 'login')
            {
                openpageurl = 'http://google.com';
            }
            return null;
        }
		public pagereference save() { 
            update newlist; 
            return null; 
        }
}

 

 

tantoniotantonio
Line 44 is: update newlist;
MoggyMoggy

Did you tried to exchange the Apex:outputfield... against
apex:inputField , which would make more sense on editing data

and don't requery the data

any reason why that newlist is declared static ? so if a second instance is running it holds the same values?

Kiran  KurellaKiran Kurella

No need to declare the variable as static. Try replacing the following line:

 

public static List<org__c> newlist {get; set;}

 

with

 

public List<org__c> newlist {get; set;}

This was selected as the best answer
Bhawani SharmaBhawani Sharma
Static variable maintains their state only for a request. After that they get reset.
Do not use static keyword for newlist.
tantoniotantonio

It was in fact the static variable list that was my issue. Removing the static fixed. Thanks!!!!