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.

Vinita_SFDCVinita_SFDC

Hello,

 

Make use of standard controller in your VF page like:

 

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

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

</apex:pageBlockButtons>
<apex:pageBlockSection title="My Content Section" columns="2">
<apex:inputField value="{!account.id}"/>
<apex:inputField value="{!account.name}"/>
<apex:inputField value="{!account.phone}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

 

ezhil_kezhil_k

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: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 ;           
         }
    }