+ Start a Discussion
Btuitasi1Btuitasi1 

Custom Controller for form

I am using an apex form to insert data into a custom object. My code is:
<apex:form>
<apex:pageBlock mode="edit">
      <apex:pageBlockButtons >
        <apex:commandButton action="{!save}" value="Submit" styleClass="btn blu btn-data"/>
      </apex:pageBlockButtons>
      <apex:pageBlockSection columns="1">
        <apex:inputField value="{!Comments__c.Name}"/>
        <apex:inputField value="{!Comments__c.Subject__c}"/>
        <apex:inputField value="{!Comments__c.Comments__c}"/>
      </apex:pageBlockSection>
    </apex:pageBlock>
  </apex:form>
I know this is easily done with a standard controller, but I have a repeater in use already that uses a custom controller. How would I add this capability to my custom controller?

Custom Controller:
public with sharing class challengeDiscuss {
    public List<Challenge__c> getRecentChallenge1()
    {
        return [SELECT Id, Name, CreatedDate, Description__c FROM Challenge__c order by createdDate desc Limit 1];
    }
    public List<Challenge_Comment__c> getRecentComments10()
    {
        return [SELECT Id, Name, CreatedDate, Comment__c FROM Challenge_Comment__c order by createdDate desc, CreatedDate desc Limit 10];
    }
  
}
Thanks!!


Best Answer chosen by Btuitasi1
kaustav goswamikaustav goswami
Declare a variable in your custom controller and use that instance to save the data.

Sample Apex Code

public with sharing class challengeDiscuss {
	// declare the instance
	public Comments__c commentObj {get; set;}
	
	// Constructor of the class
	public challengeDiscuss(){
		commentObj = new Comments__c();
	}
	
    public List<Challenge__c> getRecentChallenge1()
    {
        return [SELECT Id, Name, CreatedDate, Description__c FROM Challenge__c order by createdDate desc Limit 1];
    }
    public List<Challenge_Comment__c> getRecentComments10()
    {
        return [SELECT Id, Name, CreatedDate, Comment__c FROM Challenge_Comment__c order by createdDate desc, CreatedDate desc Limit 10];
    }
	
	public void saveComments(){
		// insert or update comments - depends on your requirement
		try{
			insert commentObj;
		}catch(Exception ex){
			System.debug('#### Error while inseting comments #### ' + ex.getMessage());
		}
		
	}
  
}

Sample VF Page

<apex:form id="theForm">
	<apex:pageBlock mode="edit">
		<apex:pageBlockButtons >
			<apex:commandButton action="{!saveComments}" value="Submit" styleClass="btn blu btn-data" reRender="theForm"/>
		</apex:pageBlockButtons>
		<apex:pageBlockSection columns="1">
			<apex:inputField value="{!commentObj.Name}"/>
			<apex:inputField value="{!commentObj.Subject__c}"/>
		<apex:inputField value="{!commentObj.Comments__c}"/>
		</apex:pageBlockSection>
	</apex:pageBlock>
</apex:form>

Please let me know if this helps.

Thanks,
Kaustav

All Answers

kaustav goswamikaustav goswami
Declare a variable in your custom controller and use that instance to save the data.

Sample Apex Code

public with sharing class challengeDiscuss {
	// declare the instance
	public Comments__c commentObj {get; set;}
	
	// Constructor of the class
	public challengeDiscuss(){
		commentObj = new Comments__c();
	}
	
    public List<Challenge__c> getRecentChallenge1()
    {
        return [SELECT Id, Name, CreatedDate, Description__c FROM Challenge__c order by createdDate desc Limit 1];
    }
    public List<Challenge_Comment__c> getRecentComments10()
    {
        return [SELECT Id, Name, CreatedDate, Comment__c FROM Challenge_Comment__c order by createdDate desc, CreatedDate desc Limit 10];
    }
	
	public void saveComments(){
		// insert or update comments - depends on your requirement
		try{
			insert commentObj;
		}catch(Exception ex){
			System.debug('#### Error while inseting comments #### ' + ex.getMessage());
		}
		
	}
  
}

Sample VF Page

<apex:form id="theForm">
	<apex:pageBlock mode="edit">
		<apex:pageBlockButtons >
			<apex:commandButton action="{!saveComments}" value="Submit" styleClass="btn blu btn-data" reRender="theForm"/>
		</apex:pageBlockButtons>
		<apex:pageBlockSection columns="1">
			<apex:inputField value="{!commentObj.Name}"/>
			<apex:inputField value="{!commentObj.Subject__c}"/>
		<apex:inputField value="{!commentObj.Comments__c}"/>
		</apex:pageBlockSection>
	</apex:pageBlock>
</apex:form>

Please let me know if this helps.

Thanks,
Kaustav
This was selected as the best answer
Btuitasi1Btuitasi1
Any idea why I am getting two submit buttons? One is on the top of the form and one is on the bottom with the same functionality.
kaustav goswamikaustav goswami
It is because you have used pageBlockButtons.

If you want the button to appear in any one position then you need to do something like this

<apex:pageBlockButtons location="top">
            <apex:commandButton action="{!saveComments}" value="Submit" styleClass="btn blu btn-data" reRender="theForm"/>
        </apex:pageBlockButtons>

Please let me know if this helps.

Thanks,
Kaustav
Btuitasi1Btuitasi1
Thank you! That fixed the problem!