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
Ethan HotzEthan Hotz 

Saving a list of custom objects using a custom controller

So I've been stuck on this for way too long; I've scoured the forums and made every change I could think of, but I have no idea why I'm not getting the save to function. I've tried using the standard controller's save, and the current setup using the custom save method. The way I've got it set now, the developer console shows that there are DML calls being made, but the changes aren't sticking. 

If someone could tell me where I'm going wrong, I'd really appreciate it.

The VF page:
<apex:page standardController="WeeklyDevStats__c" recordSetVar="weeklyDevList" extensions="MaxHoursController2">
    
 <apex:pageBlock >
 <apex:pageMessages />
  <apex:pageBlockSection >
   <apex:form >
    <apex:selectList label="Week" value="{!selectedWeek}" size="1" multiselect="false" >
     <apex:actionSupport event="onchange" action="{!changeWeek}" />
      <apex:selectOptions value="{!weeks}" />
    </apex:selectList>
    
   </apex:form>
  </apex:pageBlockSection>
  <apex:pageBlockSection >
   <apex:dataTable value="{!weeklyDevList}" var="dev" columnsWidth="150">
      <apex:column headerValue="Developer">
         <apex:outputField value="{!dev.Dev_name__c}"/>
      </apex:column>
      <apex:column headerValue="Max Hours">
         <apex:form ><apex:inputField value="{!dev.MaxHours__c}">
           
         </apex:inputField></apex:form>
      </apex:column>
   </apex:dataTable>
  </apex:pageBlockSection>
 </apex:pageBlock>
 <apex:form ><apex:commandButton value="Save" action="{!saveHours}"/></apex:form>
 
</apex:page>
The controller:
public class MaxHoursController2 {


    public String selectedWeek { get; set; }
    public List<SelectOption> weeks { get; set; }
    public List<WeeklyDevStats__c> weeklyDevList {get; set;}
    
    public Apexpages.standardsetcontroller sc;
    
    public MaxHoursController2(ApexPages.StandardSetController controller) {
        sc = controller;
        selectedWeek = CalendarClass.getInstance().getMonday().format();
        weeks = new List<SelectOption>();
        
        setWeekList();
        setWeeklyDevList(Date.parse(selectedWeek));
    }
    

    public PageReference changeWeek() {
        setWeeklyDevList(Date.valueOf(selectedWeek));
        
        return null;
    }
    
    public PageReference saveHours() {
        
        try {
            update weeklyDevList;
        } catch(DMLException e) {
            Apexpages.addmessages(e);
        }
        return null;
    }
    
    public void setWeekList() {
        for (Date day : CalendarClass.getInstance().getNextMondays(8)) {
            weeks.add(new SelectOption(String.valueOf(day), String.valueOf(day)));
        }
   }

    public void setWeeklyDevList(Date week) {
    
        weeklyDevList = [SELECT Dev_name__c, MaxHours__c FROM WeeklyDevStats__c WHERE Week_dev__c = :week];
    }
    
    public List<WeeklyDevStats__c> getWeeklyDevList() {
        return weeklyDevList;
    }
    
}


 
Best Answer chosen by Ethan Hotz
RaidanRaidan
Hi Ethan,
May I know why you need multiple <apex:form> in your page? Perhaps you can just use one, but move the closing </form> to the last line before your </apex:page>. The code will look something like this:
<apex:page standardController="WeeklyDevStats__c" recordSetVar="weeklyDevList" extensions="MaxHoursController2">
    
 <apex:pageBlock >
 <apex:pageMessages />
  <apex:pageBlockSection >
   <apex:form >
    <apex:selectList label="Week" value="{!selectedWeek}" size="1" multiselect="false" >
     <apex:actionSupport event="onchange" action="{!changeWeek}" />
      <apex:selectOptions value="{!weeks}" />
    </apex:selectList>
    
  </apex:pageBlockSection>
  <apex:pageBlockSection >
   <apex:dataTable value="{!weeklyDevList}" var="dev" columnsWidth="150">
      <apex:column headerValue="Developer">
         <apex:outputField value="{!dev.Dev_name__c}"/>
      </apex:column>
      <apex:column headerValue="Max Hours">
         <apex:inputField value="{!dev.MaxHours__c}">
           
         </apex:inputField>
      </apex:column>
   </apex:dataTable>
  </apex:pageBlockSection>
 </apex:pageBlock>
	<apex:commandButton value="Save" action="{!saveHours}"/>
  </apex:form>
 
</apex:page>


 

All Answers

RaidanRaidan
Hi Ethan,
May I know why you need multiple <apex:form> in your page? Perhaps you can just use one, but move the closing </form> to the last line before your </apex:page>. The code will look something like this:
<apex:page standardController="WeeklyDevStats__c" recordSetVar="weeklyDevList" extensions="MaxHoursController2">
    
 <apex:pageBlock >
 <apex:pageMessages />
  <apex:pageBlockSection >
   <apex:form >
    <apex:selectList label="Week" value="{!selectedWeek}" size="1" multiselect="false" >
     <apex:actionSupport event="onchange" action="{!changeWeek}" />
      <apex:selectOptions value="{!weeks}" />
    </apex:selectList>
    
  </apex:pageBlockSection>
  <apex:pageBlockSection >
   <apex:dataTable value="{!weeklyDevList}" var="dev" columnsWidth="150">
      <apex:column headerValue="Developer">
         <apex:outputField value="{!dev.Dev_name__c}"/>
      </apex:column>
      <apex:column headerValue="Max Hours">
         <apex:inputField value="{!dev.MaxHours__c}">
           
         </apex:inputField>
      </apex:column>
   </apex:dataTable>
  </apex:pageBlockSection>
 </apex:pageBlock>
	<apex:commandButton value="Save" action="{!saveHours}"/>
  </apex:form>
 
</apex:page>


 
This was selected as the best answer
Ethan HotzEthan Hotz
Holy crap; I don't understand why that was causing issues, but fixing that fixed the save issue. Thank you so much.