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
Dilip_VDilip_V 

Data is not committed to the Database using custom controller

I wrote a custom controller(Wizard based) and two VF pages.

My Intention is to capture the details of custom object(Candidate)Using first page I and
Using Next page I am displying the same fields and save button.

But the records are not saving and not displaying in the VF2.

Controller:
Apex Controller:

public with sharing class CanrecordCon {

public Candidate__C Can{ get; private set; }
public CanrecordCon() {
Id id = ApexPages.currentPage().getParameters().get('id');
if(id == null) 
 new Candidate__C();
 else
Can=[SELECT Name, Phone__C FROM Candidate__C WHERE Id = :id];
}

public PageReference Next() 
{
      return Page.newCanrecord1;
 }

public PageReference save() {
try {

insert(Can);
} catch(System.DMLException e) {
ApexPages.addMessages(e);
return null;
}
// After Save, navigate to the default view page:
return (new ApexPages.StandardController(Can)).view();
}
}

Vf-page1:
<apex:page Controller="CanrecordCon" >

<apex:form >
<apex:pageBlock mode="edit">
<apex:pageMessages />
<apex:pageBlockSection >
<apex:inputField value="{!Can.name}" id="No1"/>
<apex:inputField value="{!Can.Phone__c}" id="No2"/>

</apex:pageBlockSection>
<apex:pageBlockButtons location="bottom">
<apex:commandButton value="Next" action="{!Next}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
  
</apex:page>

Vf page2:

<apex:page controller="CanrecordCon" tabstyle="Account">
  
  <apex:form >
<apex:pageBlock >
<apex:pageBlockSection title="Details" collapsible="false"  columns="2">
            <apex:outputField id="a1" value="{!Can.Name}"/> 
            
            <apex:outputField id="a3" value="{!Can.Phone__c}"/>          
        </apex:pageBlockSection></apex:pageBlock>
   <apex:commandButton value="Save" action="{!save}"/>
</apex:form>
</apex:page>

 
Best Answer chosen by Dilip_V
shashi lad 4shashi lad 4
Please make sure you don't have constructor in the controller. The real problem here is the constructor. When you call second page, it initiate the constructor again and erase the variables. I modified the code and tested out good for me. please update the Object name <Group_1__c> to your object.


<apex:page Controller="CanrecordCon" >

<apex:form >
<apex:pageBlock mode="edit">
<apex:pageMessages />
<apex:pageBlockSection >
<apex:inputField value="{!Can.name}" id="No1"/>
<apex:inputField value="{!Can.phone__c}" id="No2"/>

</apex:pageBlockSection>
<apex:pageBlockButtons location="bottom">
<apex:commandButton value="Next" action="{!Next}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
  
</apex:page>



public  class CanrecordCon {

   Group_1__c Can;
    public group_1__c getCan() {
    
    if(can == null) 
     can = new Group_1__c ();
    
    return can;
    }
    
public PageReference Next() 
{
      return Page.newCanrecord1;
 }

public PageReference save() {
try {

insert(Can);
} catch(System.DMLException e) {
ApexPages.addMessages(e);
return null;
}
// After Save, navigate to the default view page:
return (new ApexPages.StandardController(Can)).view();
}
}



page 2:
---------

<apex:page controller="CanrecordCon" tabstyle="Account"> <apex:form > <apex:pageBlock > <apex:pageBlockSection title="Details" collapsible="false" columns="2"> <apex:outputField id="a1" value="{!Can.Name}"/> <apex:outputField id="a3" value="{!Can.phone__c}"/> </apex:pageBlockSection></apex:pageBlock> <apex:commandButton value="Save" action="{!save}"/> </apex:form> </apex:page>
 

All Answers

James LoghryJames Loghry
The next method is not saving the record when you call it in your first VF page, perhaps that's part of the issue? Furthermore, in your VF pages I would add a rerender attribute to the commandButton to rerender the form and show any apex:pageMessages that come up.  For instance, your VF2 page would look like the following:
 
<apex:page controller="CanrecordCon" tabstyle="Account">
  <apex:form id="theform">
<apex:pageMessages id="msgs" />
<apex:pageBlock >
<apex:pageBlockSection title="Details" collapsible="false" columns="2">
            <apex:outputField id="a1" value="{!Can.Name}"/>
            <apex:outputField id="a3" value="{!Can.Phone__c}"/>         
        </apex:pageBlockSection></apex:pageBlock>
   <apex:commandButton value="Save" action="{!save}" rerender="theform"/>
</apex:form>
</apex:page>

Additionally, you could get by combining both of these VF pages into 1 VF page.  To do this you would render one pageBlockSection for the initial load of the page, and then a second pageBlockSection for the output portion of the page. 
Dilip_VDilip_V
Thank you for quick responce.

I tried with your code( with rerender Attibute) but still same problem.

Thanks
shashi lad 4shashi lad 4
Please refere to this example. This can help you.
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_quick_start_wizard.htm
Thanks
shashi
Dilip_VDilip_V
I wrote above class and methods based on the example that you mentioned above.Still same problem.

Thank you.
shashi lad 4shashi lad 4
Please make sure you don't have constructor in the controller. The real problem here is the constructor. When you call second page, it initiate the constructor again and erase the variables. I modified the code and tested out good for me. please update the Object name <Group_1__c> to your object.


<apex:page Controller="CanrecordCon" >

<apex:form >
<apex:pageBlock mode="edit">
<apex:pageMessages />
<apex:pageBlockSection >
<apex:inputField value="{!Can.name}" id="No1"/>
<apex:inputField value="{!Can.phone__c}" id="No2"/>

</apex:pageBlockSection>
<apex:pageBlockButtons location="bottom">
<apex:commandButton value="Next" action="{!Next}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
  
</apex:page>



public  class CanrecordCon {

   Group_1__c Can;
    public group_1__c getCan() {
    
    if(can == null) 
     can = new Group_1__c ();
    
    return can;
    }
    
public PageReference Next() 
{
      return Page.newCanrecord1;
 }

public PageReference save() {
try {

insert(Can);
} catch(System.DMLException e) {
ApexPages.addMessages(e);
return null;
}
// After Save, navigate to the default view page:
return (new ApexPages.StandardController(Can)).view();
}
}



page 2:
---------

<apex:page controller="CanrecordCon" tabstyle="Account"> <apex:form > <apex:pageBlock > <apex:pageBlockSection title="Details" collapsible="false" columns="2"> <apex:outputField id="a1" value="{!Can.Name}"/> <apex:outputField id="a3" value="{!Can.phone__c}"/> </apex:pageBlockSection></apex:pageBlock> <apex:commandButton value="Save" action="{!save}"/> </apex:form> </apex:page>
 
This was selected as the best answer
Dilip_VDilip_V
Thanks a lot shashi for your time.