You need to sign in to do that
Don't have an account?
tantonio
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!!
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
You should not re-query the data in your save method. Try this save method and I hope it will resolve the issue:
Hi Codewizard!
I did try that prior to posting and it results in a null pointer exception:
Here is the modified class per your suggestions. Let me know your thoughts. Thanks!!!
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?
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;}
Do not use static keyword for newlist.
It was in fact the static variable list that was my issue. Removing the static fixed. Thanks!!!!