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
Deepak Tiwari 17Deepak Tiwari 17 

how to create contact form apex controller class

Hi There can any one help me how to use custom controller for this code as i use Standard controller....below is the code...


<apex:page StandardController="Contact" >
<apex:form >
    <apex:pageBlock >        
<apex:pageBlockButtons Location="Top" >
<apex:commandButton action="{!save}" value="Save&New"/>
    <apex:commandButton action="{!save}" value="Save"/>
    <apex:commandButton action="{!cancel}" value="cancel"/>
     </apex:pageBlockButtons>   
        
    <apex:pageBlockSection title="General Information" columns="2" >
     <apex:inputField value="{!Contact.FirstName}" taborderhint="1"/>
     <apex:inputField value="{!Contact.LastName}" taborderhint="2" required="true"/>
     <apex:inputField value="{!Contact.Email}" taborderhint="3" required="true"/>
     <apex:inputField value="{!Contact.Phone}" taborderhint="4" required="true"/>
     </apex:pageBlockSection> 
        
     <apex:pageBlockSection title="Address Information" columns="2" >
     <apex:inputField value="{!Contact.MailingCountry}" taborderhint="5"/>
     <apex:inputField value="{!Contact.MailingCity}" taborderhint="6"/>
     <apex:inputField value="{!Contact.MailingState}" taborderhint="7"/>
     <apex:inputField value="{!Contact.MailingPostalcode}" taborderhint="8" required="true"/>
     </apex:pageBlockSection>  
     
     <apex:pageBlockSection title="Industry Information" columns="2">
     <apex:inputField value="{!contact.Business_Vertical__c}" required="true"/>
     <apex:inputField value="{!contact.Date_Procured__c}" required="true"/>
     <apex:inputField value="{!contact.Division_of_Vertical__c}" required="true"/>
     <apex:inputField value="{!contact.Area_Of_Division__c}"/>
     
     </apex:pageBlockSection>     
     <apex:pageBlockSection title="Amount Information" Columns="2">
     <apex:inputfield value="{!Contact.Amount_Invested__c}" required="true"/>
     <apex:inputfield value="{!Contact.Amount_Earned__c}" required="true"/>    
     </apex:pageBlockSection>
     
   
                              
<apex:pageBlockButtons Location="Bottom">
<apex:commandButton action="{!save}" value="Save&New"/>
    <apex:commandButton action="{!save}" value="Save"/>
    <apex:commandButton action="{!cancel}" value="cancel"/>
     </apex:pageBlockButtons>
    </apex:pageBlock>
   </apex:form>
</apex:page>
Best Answer chosen by Deepak Tiwari 17
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi Deepak,

If you need custom controller along with standard controller then you can use 'Extensions' attribute for <apex:page> tag:
 
<apex:page StandardController="Contact" extensions="NewCutsomController" >
<apex:form >
    <apex:pageBlock >        
<apex:pageBlockButtons Location="Top" >
<apex:commandButton action="{!save}" value="Save&New"/>
    <apex:commandButton action="{!save}" value="Save"/>
    <apex:commandButton action="{!cancel}" value="cancel"/>
     </apex:pageBlockButtons>   
        
    <apex:pageBlockSection title="General Information" columns="2" >
     <apex:inputField value="{!Contact.FirstName}" taborderhint="1"/>
     <apex:inputField value="{!Contact.LastName}" taborderhint="2" required="true"/>
     <apex:inputField value="{!Contact.Email}" taborderhint="3" required="true"/>
     <apex:inputField value="{!Contact.Phone}" taborderhint="4" required="true"/>
     </apex:pageBlockSection> 
        
     <apex:pageBlockSection title="Address Information" columns="2" >
     <apex:inputField value="{!Contact.MailingCountry}" taborderhint="5"/>
     <apex:inputField value="{!Contact.MailingCity}" taborderhint="6"/>
     <apex:inputField value="{!Contact.MailingState}" taborderhint="7"/>
     <apex:inputField value="{!Contact.MailingPostalcode}" taborderhint="8" required="true"/>
     </apex:pageBlockSection>  
     
     <apex:pageBlockSection title="Industry Information" columns="2">
     <apex:inputField value="{!contact.Business_Vertical__c}" required="true"/>
     <apex:inputField value="{!contact.Date_Procured__c}" required="true"/>
     <apex:inputField value="{!contact.Division_of_Vertical__c}" required="true"/>
     <apex:inputField value="{!contact.Area_Of_Division__c}"/>
     
     </apex:pageBlockSection>     
     <apex:pageBlockSection title="Amount Information" Columns="2">
     <apex:inputfield value="{!Contact.Amount_Invested__c}" required="true"/>
     <apex:inputfield value="{!Contact.Amount_Earned__c}" required="true"/>    
     </apex:pageBlockSection>
     
   
                              
<apex:pageBlockButtons Location="Bottom">
<apex:commandButton action="{!save}" value="Save&New"/>
    <apex:commandButton action="{!save}" value="Save"/>
    <apex:commandButton action="{!cancel}" value="cancel"/>
     </apex:pageBlockButtons>
    </apex:pageBlock>
   </apex:form>
</apex:page>

So you can write the additional methods in that controller.

Help us explain your whole requirement properly so that we can give you an better solution.
Thanks.
 

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi,

You can refer  below code and change accordingly.
 
public class createNewContactcontroller {
public String firstName{get;set;}
public String lastName{get;set;}
public String phone{get;set;}
//list <contact> conList;

public pageReference Save(){
    Contact con = new Contact(firstname = firstname, lastname = lastname, phone = phone);
    insert con;
    return null;
}
}
 
<apex:page controller="createNewContactController" >
<apex:pageBlock>
    <apex:form>
        <apex:pageBlockSection>       
            
<apex:inputField value="{!Contact.firstName}"/> 
            <apex:commandButton value="save" action="{!save}" />
        </apex:pageBlockSection> 
    </apex:form>    
</apex:pageBlock>

   Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
                                             
Best Regards
Sandhya
 

 
Bhargavi TunuguntlaBhargavi Tunuguntla
Hi Deepak,

If you need custom controller along with standard controller then you can use 'Extensions' attribute for <apex:page> tag:
 
<apex:page StandardController="Contact" extensions="NewCutsomController" >
<apex:form >
    <apex:pageBlock >        
<apex:pageBlockButtons Location="Top" >
<apex:commandButton action="{!save}" value="Save&New"/>
    <apex:commandButton action="{!save}" value="Save"/>
    <apex:commandButton action="{!cancel}" value="cancel"/>
     </apex:pageBlockButtons>   
        
    <apex:pageBlockSection title="General Information" columns="2" >
     <apex:inputField value="{!Contact.FirstName}" taborderhint="1"/>
     <apex:inputField value="{!Contact.LastName}" taborderhint="2" required="true"/>
     <apex:inputField value="{!Contact.Email}" taborderhint="3" required="true"/>
     <apex:inputField value="{!Contact.Phone}" taborderhint="4" required="true"/>
     </apex:pageBlockSection> 
        
     <apex:pageBlockSection title="Address Information" columns="2" >
     <apex:inputField value="{!Contact.MailingCountry}" taborderhint="5"/>
     <apex:inputField value="{!Contact.MailingCity}" taborderhint="6"/>
     <apex:inputField value="{!Contact.MailingState}" taborderhint="7"/>
     <apex:inputField value="{!Contact.MailingPostalcode}" taborderhint="8" required="true"/>
     </apex:pageBlockSection>  
     
     <apex:pageBlockSection title="Industry Information" columns="2">
     <apex:inputField value="{!contact.Business_Vertical__c}" required="true"/>
     <apex:inputField value="{!contact.Date_Procured__c}" required="true"/>
     <apex:inputField value="{!contact.Division_of_Vertical__c}" required="true"/>
     <apex:inputField value="{!contact.Area_Of_Division__c}"/>
     
     </apex:pageBlockSection>     
     <apex:pageBlockSection title="Amount Information" Columns="2">
     <apex:inputfield value="{!Contact.Amount_Invested__c}" required="true"/>
     <apex:inputfield value="{!Contact.Amount_Earned__c}" required="true"/>    
     </apex:pageBlockSection>
     
   
                              
<apex:pageBlockButtons Location="Bottom">
<apex:commandButton action="{!save}" value="Save&New"/>
    <apex:commandButton action="{!save}" value="Save"/>
    <apex:commandButton action="{!cancel}" value="cancel"/>
     </apex:pageBlockButtons>
    </apex:pageBlock>
   </apex:form>
</apex:page>

So you can write the additional methods in that controller.

Help us explain your whole requirement properly so that we can give you an better solution.
Thanks.
 
This was selected as the best answer
Deepak Tiwari 17Deepak Tiwari 17
Hi Sandhya,

Yo are really helping me... Please help me how can i create multi-select picklist
Deepak Tiwari 17Deepak Tiwari 17
Hi Sandhya
,
Please help me with this trigger. This is showing error ""Error: Compile Error: Extra ';', at 'Lohia'. at line 8 column 37"".

I want if someone change Stage to Closed won this this opportunity will automatically assigned to Rohit Lohia.


trigger OpportunityTrigger on Opportunity__c (before insert, before update) 
{

        for(Opportunity opp : trigger.new) {

        if(opp.StageName == 'Closed Won' && trigger.oldMap.get(opp.Id).StageName == 'Closed Won')

        opp.Assigned_CSD__c = Rohit Lohia;


}


}