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
Varun TejaVarun Teja 

How to add total number of contacts in custom field if we click add another contact button in VF page

I have created one custom filed i.e. Total_Contacts_Added_till_Now__c In contact object,In VF page If We click 'Add More Contacts' button ,Total_Contacts_Added_till_Now__c  value should be increasd to 1, if I click one more time 'Add More Contacts' button that filed should be automatically increased to 2 .Please help me on this how to do.


<apex:page standardController="Contact">
    <apex:form >
        <apex:pageBlock title="Insert Contact  Record">
            <apex:pageMessages ></apex:pageMessages>
            <apex:pageBlockSection >
                <apex:outputText label="Account Name" value="{!$CurrentPage.parameters.accountName}" />
                <apex:inputField value="{! Contact.FirstName }" />
                <apex:inputField value="{! Contact.LastName }" />
                <apex:inputField value="{! Contact.Birthdate }" />
                <apex:inputField value="{! Contact.Description }" />
                <apex:inputField value="{! Contact.Email }" />
                <apex:inputField value="{! Contact.HomePhone }" />
                <apex:inputField value="{! Contact.Level__c }" />
                <apex:inputField id="total" value="{!Contact.Total_Contacts_Added_till_Now__c}" />
                
            </apex:pageBlockSection>
            <apex:pageblockButtons location="bottom">
                <apex:commandButton style="float:centre" action="{! addMoreContacts }" value="Add More Contacts" />
                <apex:commandButton style="float:centre" value="I don't want to create contact" />
                <apex:commandButton style="float:centre" value="Submit Contact's" />
            </apex:pageblockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Prashant Pandey07Prashant Pandey07
Hi Nagarjuna,

You may use the following code that will populate the total contact and increase by 1 after clicking the Add more contacts.

Let me know if this helps you.
<apex:page standardController="Contact" extensions="contactcount">
    <apex:form >
        <apex:pageBlock title="Insert Contact  Record">
            <apex:pageMessages ></apex:pageMessages>
            <apex:pageBlockSection >
                <apex:outputText label="Account Name" value="{!$CurrentPage.parameters.accountName}" />
                <apex:inputField value="{!con.FirstName}"/>
                <apex:inputField value="{!con.LastName}"/>
                <apex:inputField value="{!con.Birthdate}" />
                <apex:inputField value="{!con.Description}"/>
                <apex:inputField value="{!con.Email}"/>
                <apex:inputField value="{!con.HomePhone}"/>
                <apex:inputField value="{!con.Level__c}"/>
                <apex:inputField id="total" value="{!con.Total_Contacts_Added_till_Now__c}"/>
                
            </apex:pageBlockSection>
            <apex:pageblockButtons location="bottom">
                <apex:commandButton style="float:centre" action="{! addMoreContacts }" value="Add More Contacts" />
             <!--   <apex:commandButton style="float:centre" value="I don't want to create contact" />
                <apex:commandButton style="float:centre" value="Submit Contact's" />-->
            </apex:pageblockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>
 
public class contactcount {
    public contact con{get;set;}
    ApexPages.StandardController controller;
     list<contact> conlistupdate=new list<contact>();
    public contactcount(ApexPages.StandardController controller) {
        this.controller = controller;
        con = (contact) Controller.getRecord();
          
    }
    
    public pagereference addMoreContacts(){
        list<contact> conlist=new list<contact>();
        
         
            integer it; 
               it= [select count() from contact];
                con.Total_Contacts_Added_till_Now__c=it;
                conlistupdate.add(con);
                   contact c=new contact();
                   c.lastname=con.lastname;
                  //similarly add other fields 
                   c.firstname=con.firstname;
                     conlist.add(c);
             if(conlist.size()>0)
             insert conlist;
              
             return null;   
    }
    
}

--
Thanks,
Prashant