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

How to add a Contact?

I have Vf page with two buttons, Accept and Decline, and fields like Name Account ID, mobile number,If Accept  button is clicked, i want the details from vf page to be added as a contact in AccountID mentioned.




Make use of standard controller in your VF page like:


<apex:page standardController="Account">
<apex:pageBlock title="My Content" mode="edit">

<apex:commandButton action="{!save}" value="Accept"/>

<apex:pageBlockSection title="My Content Section" columns="2">
<apex:inputField value="{!}"/>
<apex:inputField value="{!}"/>
<apex:inputField value="{!}"/>



Hi Vinita..

 Actually  Below is my Code...

BUt am getting Error:


System.QueryException: List has no rows for assignment to SObject
Class.devcontroller.<init>: line 18, column 1



Vf Page:


<apex:page standardController="Developer_Detail__c"  extensions="devcontroller">
  <h1>Application Form</h1>
    <apex:form >
        <apex:pageMessages id="error"/>
        <apex:pageBlock >
        <apex:pageBlockSection title="Information">
        <apex:OutputText value="{!Developer_Detail__c.Name}"  />
        <apex:OutputText value="{!Developer_Detail__c.Name__c}" />
        <apex:OutputText value="{!Developer_Detail__c.Acc_ID__c}" />
        <apex:OutputText value="{!Developer_Detail__c.Mobile_Number__c}"/>
         <apex:commandButton value="Accept" action="{!accept}" />
         <apex:commandButton value="Decline"  />






public class devcontroller {

    public Developer_Detail__c rec {set;get;}
    public Developer_Detail__c hr {set;get;}
    public Contact myNewContact {set;get;}
    public Contact myNewCon {set;get;}  
   public devcontroller(ApexPages.StandardController controller) {
            myNewContact = new Contact();
            rec = (Developer_Detail__c) controller.getRecord();
            hr  = [Select Name__c, Mobile_Number__c, Acc_ID__c From Developer_Detail__c where Id =: rec .Id];
             myNewContact.FirstName = hr.Name__c;
             myNewContact.LastName = hr.Name__c;
             myNewContact.Phone= hr.Mobile_Number__c;
             myNewContact.Account=[Select Account_ID__c,Name from Account where];    
    public PageReference accept(){
            insert myNewContact;
            PageReference pageRef= new PageReference('/'+ myNewContact.Id);
            return pageRef ;