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
ezhil_kezhil_k 

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.

Best Answer chosen by Admin (Salesforce Developers) 
Prafull G.Prafull G.
You have to use the fields displayed on page and create the contact record in controller.
The code will look like
Contact con = new Contact();
con.firstname = yourfirstnamevariable;
con.accountid = youraccountidvariable;
// set other values
insert con;

let me know if you are looking for some other information.

All Answers

Prafull G.Prafull G.
You have to use the fields displayed on page and create the contact record in controller.
The code will look like
Contact con = new Contact();
con.firstname = yourfirstnamevariable;
con.accountid = youraccountidvariable;
// set other values
insert con;

let me know if you are looking for some other information.
This was selected as the best answer
ezhil_kezhil_k

Am getting Error:

 

Visualforce Error


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

 

 

Please Help me Where am I going Wrong?

 

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:pageBlockSection>
         <apex:commandButton value="Accept" action="{!accept}" />
         <apex:commandButton value="Decline"  />



      </apex:pageBlock>
    </apex:form>

    </apex:page>

 

Controller:

 

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();
            system.debug('-----------'+rec);
            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 Account_ID__c=:hr.id];    
               
   }
    
    public PageReference accept(){
    
            insert myNewContact;
            PageReference pageRef= new PageReference('/'+ myNewContact.Id);
            return pageRef ;           
         }
    }