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
venkatsforcevenkatsforce 

AddRecords using vfpage

Hi

 

   i want to add records through VF page to Standardobjects using buttons(newContact) and it is stored using Controllers not using the StandardControllers.....

 

 

 

Thanks

========

VenkatSforce89

Best Answer chosen by Admin (Salesforce Developers) 
Chamil MadusankaChamil Madusanka

Try following code.

 

Visualforce page

<apex:page Controller="ContactController1">
    <apex:form >
    <apex:pageMessages ></apex:pageMessages>
        <apex:pageBlock title="New Contact" mode="edit">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!insertContact}" value="Save"/>
            </apex:pageBlockButtons>            
            <apex:pageBlockSection title="Contact Details" columns="2">
                <apex:inputField value="{!NewContact.Title}"/>
                <apex:inputField value="{!NewContact.FirstName}"/>
                 <apex:inputField value="{!NewContact.LastName}"/>
                <apex:inputField value="{!NewContact.Email}"/>
                <apex:inputField value="{!NewContact.AccountId}"/>                
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

 

Controller

public with sharing class ContactController1 {
public Contact NewContact{get;set;}

public ContactController1()
{
    NewContact = new Contact();
}

public PageReference insertContact()
{
    try
    {
        insert NewContact ;
       PageReference pr = new PageReference('/' + NewContact.Id);
                pr.setRedirect(true);

                return pr;
    }catch(DMLException ex)
    {
        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Error in insert contact');
            ApexPages.addMessage(myMsg);
            return null;
    }
    
}
    
}

 If you got the answer from this post, Please hit the Kudos button & mark the answer as solution, It might help others running to into similar problem in future.

All Answers

Chamil MadusankaChamil Madusanka

Try following code.

 

Visualforce page

<apex:page Controller="ContactController1">
    <apex:form >
    <apex:pageMessages ></apex:pageMessages>
        <apex:pageBlock title="New Contact" mode="edit">
            <apex:pageBlockButtons >
                <apex:commandButton action="{!insertContact}" value="Save"/>
            </apex:pageBlockButtons>            
            <apex:pageBlockSection title="Contact Details" columns="2">
                <apex:inputField value="{!NewContact.Title}"/>
                <apex:inputField value="{!NewContact.FirstName}"/>
                 <apex:inputField value="{!NewContact.LastName}"/>
                <apex:inputField value="{!NewContact.Email}"/>
                <apex:inputField value="{!NewContact.AccountId}"/>                
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 

 

Controller

public with sharing class ContactController1 {
public Contact NewContact{get;set;}

public ContactController1()
{
    NewContact = new Contact();
}

public PageReference insertContact()
{
    try
    {
        insert NewContact ;
       PageReference pr = new PageReference('/' + NewContact.Id);
                pr.setRedirect(true);

                return pr;
    }catch(DMLException ex)
    {
        ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Error in insert contact');
            ApexPages.addMessage(myMsg);
            return null;
    }
    
}
    
}

 If you got the answer from this post, Please hit the Kudos button & mark the answer as solution, It might help others running to into similar problem in future.

This was selected as the best answer
venkatsforcevenkatsforce

Thanks Chamil...

 

 

i got the solution.........

Chamil MadusankaChamil Madusanka

If you got the answer from this post, Please hit the Kudos button , It might help others running to into similar problem in future.