+ Start a Discussion
Ajay Kumar 583Ajay Kumar 583 

Need Apex Controller Code and vf page code

Hi , i have created a Button on Account object and Now i need the Apex controller code and visualforce page code Which will create a Contact when the user clicks the button. Please provide the code
Best Answer chosen by Ajay Kumar 583
Santosh Kumar 348Santosh Kumar 348
HI Ajay,

Try below code hope this will work.
VF page: Create_contact
<apex:page standardController="Account" extensions="createCon" action="{!createConAutomatically}">
</apex:page>

Apex Class: createCon
public class createCon { 
    private final Account acct;
    public createCon(ApexPages.StandardController stdController) {
        this.acct = (Account)stdController.getRecord();
    }
    public void createConAutomatically() {
        Contact con = new Contact();
        con.LastName='Test';
        con.AccountId=acct.id;
        //IF you are having some other mandatory fields add it over here with some default value before insert.
       
        insert con;        
    }
}


Create a detail page button on Account and add this vf page.
Do let me know if it helps you and close your query by marking it as solved.

Regards,

Santosh

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Ajay,

Greetings to you!

Below is the sample code which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page StandardController="Account" extensions="DetailPageButtonC">
    <apex:form id="form">
        <apex:pageMessages />
        <apex:pageBlock >
            
            <apex:pageBlockSection title="Create Contact" columns="1">
                <apex:outputField value="{!acc.Name}"/>
                <apex:inputField value="{!con.FirstName}"/> 
                <apex:inputField value="{!con.LastName}"/>
                <apex:inputField value="{!con.Phone}"/>                        
                <apex:inputField value="{!con.Email}"/>   
                <apex:commandButton value="Save" action="{!saveCon}" />
            </apex:pageBlockSection>
            
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class DetailPageButtonC {
    
    public Contact con {get;set;}
    public Account acc {get;set;}
    
    public DetailPageButtonC(ApexPages.StandardController controller) {
        con = new Contact(); 
        String str = ApexPages.CurrentPage().getparameters().get('id');  
        acc = [SELECT Id, Name FROM Account WHERE Id=:str];
    }  
    
    public void saveCon(){
        con.AccountId = acc.Id;
        INSERT con;  
    }
}

Create a detail page button with content source as visualforce page and add it to page layout.

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Ajay Kumar 583Ajay Kumar 583
Hi Khan,

i am getting the below error:

Insert failed. First exception on row 0 with id 0032v00002pPocnAAC; first error: INVALID_FIELD_FOR_INSERT_UPDATE, cannot specify Id in an insert call: [Id]


And also i need to get Contact should be created without providing me the input values. It should create automatically create the some custom contact whenver i click the Button.

Thanks,
Ajay
Santosh Kumar 348Santosh Kumar 348
HI Ajay,

Try below code hope this will work.
VF page: Create_contact
<apex:page standardController="Account" extensions="createCon" action="{!createConAutomatically}">
</apex:page>

Apex Class: createCon
public class createCon { 
    private final Account acct;
    public createCon(ApexPages.StandardController stdController) {
        this.acct = (Account)stdController.getRecord();
    }
    public void createConAutomatically() {
        Contact con = new Contact();
        con.LastName='Test';
        con.AccountId=acct.id;
        //IF you are having some other mandatory fields add it over here with some default value before insert.
       
        insert con;        
    }
}


Create a detail page button on Account and add this vf page.
Do let me know if it helps you and close your query by marking it as solved.

Regards,

Santosh

This was selected as the best answer
Ajay Kumar 583Ajay Kumar 583
Hi Santhosh,

Thanks a lot. its working .  But why the Page is not refershing itself when i click the buttton.?
Santosh Kumar 348Santosh Kumar 348
Ok if you need to redirect to contact detail page then you need to change the method return type and add few extra lines of code to redirect it to the detail page :

Replace the previous method with this one.
public pageReference createConAutomatically() {
        Contact con = new Contact();
        con.LastName='Test';
        con.AccountId=acct.id;
        //IF you are having some other mandatory fields add it over here with some default value before insert.
        insert con;
        PageReference nextPage = new PageReference('/' + con.Id);
        return nextPage;
        
    }


 
Ajay Kumar 583Ajay Kumar 583
Excellent... Thanks Santhosh. It helped me a lot .  Hope you will help me in future Queries. 
Thanks
Santosh Kumar 348Santosh Kumar 348
Anytime, we are here to help each other and grow as a community together.