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
bivhitscarbivhitscar 

Pageblocktable + Inputfield + Save = not saving.

Hi guys,

 

I've searched high and low for some info on what I've done wrong and can't work it out.

 

I basically want to return a list of Prestart_Checklist__c that fall within a given date range and location - this works.

 

Then for each item in the list, I want to present a text box so that a user can update the name of the record. This displays correctly and retrieves the existing data - but the save button seems ineffectual. If I change any values in the inputfields and click save, the page refreshes, but no data is saved.

 

I know I'm missing something simple, but I've wasted too much time on this one to keep hacking away at it, so any suggestions are very welcome.

 

Page:

 

<apex:page action="{!initPage}" standardController="Prestart_Checklist__c" extensions="PSCLEditExtension" >

<apex:form >
    <apex:pageBlock title="Filter results" >
        <apex:pageBlockButtons location="bottom" >
            <apex:commandButton value="Go" reRender="dataTable" />
        </apex:pageBlockButtons>
        <apex:pageBlockSection columns="1">
            <apex:inputField value="{!plantForLocation.Location__c}" />
            <apex:inputField value="{!psclForDate.Week_Ending__c}" />
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>

<apex:form >
    <apex:pageBlock title="Prestart Checklists" >
        <apex:pageBlockButtons location="bottom" >
            <apex:commandButton value="Save" action="{!save}" />
        </apex:pageBlockButtons>
        <apex:pageBlockTable id="dataTable" value="{!thisWeeksPSCLs}" var="pscl">
            <apex:column headerValue="Plant Name"> <apex:outputText value="{!pscl.Plant__r.Name}" /></apex:column>
            <apex:column headerValue="Plant Registration"> <apex:outputText value="{!pscl.Plant__r.Registration_Number__c}" /></apex:column>
            <apex:column headerValue="Prestart Checklist Number"><apex:inputField value="{!pscl.Name}" /></apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:form>
</apex:page>

 

Controller:

 

public with sharing class PSCLEditExtension {
    
    public Prestart_Checklist__c psclForDate{get; set;}
    public Plant__c plantForLocation{get; set;}
    
    public List<Plant__c> listPlant {
        get {
            return [select Id, Name from Plant__c];
        }
    }
    
    public List<Prestart_Checklist__c> listPSCL{get; set;}
    
    public List<Prestart_Checklist__c> thisWeeksPSCLs{
    	get {
	    	return [select Id, Name, Plant__r.Name, Plant__r.Registration_Number__c 
	                from Prestart_Checklist__c 
	    	        where Week_Ending__c = :psclForDate.Week_Ending__c 
	    	        and Plant__r.Location__c = :plantForLocation.Location__c];
} } } public PSCLEditExtension(ApexPages.StandardController controller) { psclForDate = new Prestart_Checklist__c(Week_Ending__c = Date.today().toStartOfWeek()); plantForLocation = new Plant__c(); } public void initPage() { } }

 

Best Answer chosen by Admin (Salesforce Developers) 
Andrew WilkinsonAndrew Wilkinson

I see it as an issue with your get...when you reference the list it is requeried. Which is why I proposed that you restructure it. The reason ne data is being passed is because of the requery. So you will need to restructure the code. Add actionsupport to the field you are using to filter the table and have it fire the qery to populate the list with the newly selected value and then rerender the table.

All Answers

Andrew WilkinsonAndrew Wilkinson

You are using the Standard Controller save and editing a custom list. Create a save method for your custom list.

 

public PageReference saveList(){

    update thisWeeksPSCLs;

    return null; //or some page you want it to go to

}

 

 

Then in your page change the Save commandbuttons action to this new method.

 

<apex:pageBlockButtons location="bottom" >
            <apex:commandButton value="Save" action="{!saveList}" />
        </apex:pageBlockButtons>
bivhitscarbivhitscar

Haha, that's exactly what I had before and it wasn't working either. I've just tried it again and still no luck. Although, I think I just had a brain wave, am I supposed to be using the StandardSetController in this instance? Even if that's the case, I'm not sure why the custom save wouldn't work...

Andrew WilkinsonAndrew Wilkinson

Add a try catch statement and <apex:pageMessages/> to your page.

 

public PageReference saveList(){

    try{

    update thisWeeksPSCLs;

}

catch(DMLException e){

ApexPages.addMessage(new ApexPages.message(ApexPages.severity.FATAL, 'DML Error:' + e));

}

    return null;

}

 

Then in your Visualforce page add under the page tag

 

<apex:pageMessages/>

bivhitscarbivhitscar

Nope, no errors, and no saved data.

bivhitscarbivhitscar

Ok, it actually is saving the records (Last Updated field is updating on the save), but it isn't using the data from the table to do so. I'm stumped.

Andrew WilkinsonAndrew Wilkinson

Your get set is the issue

 

In the get...you need to do the following with your get.

 

    	get {
if(thisWeeksPSCLs == null){ return [select Id, Name, Plant__r.Name, Plant__r.Registration_Number__c from Prestart_Checklist__c where Week_Ending__c = :psclForDate.Week_Ending__c and Plant__r.Location__c = :plantForLocation.Location__c];
}
else
return thisWeeksPSCLs;
}
bivhitscarbivhitscar

Really appreciate your perseverance on this, but I have tried that one before too. Just added it back in, but still no luck.

Andrew WilkinsonAndrew Wilkinson

I would restructure the code like this...

 

public with sharing class PSCLEditExtension {
    
    public Prestart_Checklist__c psclForDate{get; set;}
    public Plant__c plantForLocation{get; set;}
    public List<Prestart_Checklist__c> thisWeeksPSCLs
    
    public List<Plant__c> listPlant {
        get {
            return [select Id, Name from Plant__c];
        }
    }
    
    public List<Prestart_Checklist__c> listPSCL{get; set;}
    
    public void getThisWeeksPSCLs(){
       thisWeeksPSCLS = [select Id, Name, Plant__r.Name, Plant__r.Registration_Number__c 
	                from Prestart_Checklist__c 
	    	        where Week_Ending__c = :psclForDate.Week_Ending__c 
	    	        and Plant__r.Location__c = :plantForLocation.Location__c];
    }
    
    public PSCLEditExtension(ApexPages.StandardController controller) {
		psclForDate = new Prestart_Checklist__c(Week_Ending__c = Date.today().toStartOfWeek());
		plantForLocation = new Plant__c();
                getThisWeeksPSCLs();
    }

    public void saveList(){
        update thisWeeksPSCLs;
    }
	
    public void initPage() {
		
    }
	
}

 

bivhitscarbivhitscar

That won't work because the getter returns void which will kill my pageblocktable. Tested it out of interest and that's exactly what happened. Thanks for the ideas so far though :)

Andrew WilkinsonAndrew Wilkinson

I took a look at your code. So basically you are attempting to filter the results of the pageblocktable by the options selected in the upper form. Well you will need to have an actionmethod to rerender the pageblocktable based on the selected value...have the query grab the correct values and populate the table...edit the fields... and then update. Is this about right?

bivhitscarbivhitscar

Andrew Wilkinson wrote:

I took a look at your code. So basically you are attempting to filter the results of the pageblocktable by the options selected in the upper form. Well you will need to have an actionmethod to rerender the pageblocktable based on the selected value...have the query grab the correct values and populate the table...edit the fields... and then update. Is this about right?


Exactly; and everything works except for the update (which half works).

Andrew WilkinsonAndrew Wilkinson

I didn't see this but you have omitted set...which is needed to pass variables back to the controller...have you tried including set; follwoing your get block? All of this is in regards to your thisWeeksPSCLs variable.

bivhitscarbivhitscar

Yeah, I thought that might be the problem, but I couldn't figure out what syntax to use in the set method. Adding in a default set; doesn't have an effect.

Andrew WilkinsonAndrew Wilkinson

I see it as an issue with your get...when you reference the list it is requeried. Which is why I proposed that you restructure it. The reason ne data is being passed is because of the requery. So you will need to restructure the code. Add actionsupport to the field you are using to filter the table and have it fire the qery to populate the list with the newly selected value and then rerender the table.

This was selected as the best answer
bivhitscarbivhitscar

You're absolutely correct and thanks for sticking it out. The only thing I missed from your suggestion earlier was to add the action to the filter commandbutton.

 

For anyone finding this in the future, here's what it looks like:

 

Page:

 

<apex:page action="{!initPage}" standardController="Prestart_Checklist__c" extensions="PCSLEditExtension" >
<apex:pageMessages />
<apex:form >
    <apex:pageBlock title="Filter results" >
        <apex:pageBlockButtons location="bottom" >
            <apex:commandButton value="Go" action="{!initthisWeeksPSCLs}" reRender="dataTable" />
        </apex:pageBlockButtons>
	<apex:pageBlockSection columns="1">
	    <apex:inputField value="{!plantForLocation.Location__c}" />
	    <apex:inputField value="{!psclForDate.Week_Ending__c}" />
	</apex:pageBlockSection>
    </apex:pageBlock>
</apex:form>
	
<apex:form >
    <apex:pageBlock title="Prestart Checklists" >
	<apex:pageBlockButtons location="bottom" >
            <apex:commandButton value="Save" action="{!savePSCLs}" />
        </apex:pageBlockButtons>
        <apex:pageBlockTable id="dataTable" value="{!thisWeeksPSCLs}" var="pscl">
            <apex:column headerValue="Plant Name"> <apex:outputText value="{!pscl.Plant__r.Name}" /></apex:column>
            <apex:column headerValue="Registration"> <apex:outputText value="{!pscl.Plant__r.Registration_Number__c}" /></apex:column>
            <apex:column headerValue="Prestart Checklist Number"><apex:inputField value="{!pscl.Name}" /></apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:form>
</apex:page>

 

Controller:

 

public with sharing class PCSLEditExtension {
    
    public Prestart_Checklist__c psclForDate{get; set;}
    public Plant__c plantForLocation{get; set;}
    public List<Prestart_Checklist__c> thisWeeksPSCLs{get; set;}
    
    public List<Plant__c> listPlant {
        get {
            return [select Id, Name from Plant__c];
        }
    }
    
    public List<Prestart_Checklist__c> listPSCL{get; set;}
    
    public void initthisWeeksPSCLs() {
    	thisWeeksPSCLs = [select Id, Name, Plant__r.Name, Plant__r.Registration_Number__c, Plant__r.Location__r.Name
			from Prestart_Checklist__c 
			where Week_Ending__c = :psclForDate.Week_Ending__c 
			and Plant__r.Location__c = :plantForLocation.Location__c];
    }
    
    public PCSLEditExtension(ApexPages.StandardController controller) {
	psclForDate = new Prestart_Checklist__c(Week_Ending__c = Date.today().toStartOfWeek());
	plantForLocation = new Plant__c();
	initthisWeeksPSCLs();
    }
	
    public PageReference savePSCLs() {
	update thisWeeksPSCLs;
	return null;
    }
	
    public void initPage() {
		
    }
	
}