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
Nitin SharmaNitin Sharma 

Creation of contact with the help of vf page

Create a VF page with custom controller to search for all accounts on the basis of name.
If we select one of the account and click on create contact then it should be redirected to a new VF Page to create a contact for that account. When I click on save then I should be redirected to the contact detail page.
<apex:page standardController="Account" extensions="accountSearch" >
    <apex:include pageName="CreateContact"/>
 <apex:form > 
  <apex:sectionHeader title="User Global Search" subtitle="Result"/> 
  <apex:pageBlock >
      <apex:pageBlockSection columns="1">
          <apex:pageBlockSectionItem >
              <label>Quick Search</label>
              <apex:outputPanel >
                  <apex:inputText value="{!searchstring}" label="Input"/>
                  <apex:commandButton value="Search records" action="{!search}" reRender="accPgId" status="ajaxId"/>
                  <apex:commandButton value="create contact" action="{! create}" reRender="ContactPgId" />>
                  &nbsp;<apex:actionStatus startText="Searching..." id="ajaxId"></apex:actionStatus>
              </apex:outputPanel>
          </apex:pageBlockSectionItem>
      </apex:pageBlockSection>
  </apex:pageBlock>
     
      <apex:pageBlock title="Search Account Result" id="accPgId" >  
    <apex:pageblockTable value="{!acc}" var="a">  
      
     <apex:column headerValue="Name" >  
       <apex:outputlink >{!a.Name}</apex:outputlink>
         <apex:inputCheckbox value="{! a.Name}">
             </apex:inputcheckbox>
     </apex:column> 
     <apex:column headerValue="Phone" >  
       <apex:outputlink >{!a.Phone}</apex:outputlink>
     </apex:column>
     
    </apex:pageBlockTable>     
   </apex:pageBlock>  
    </apex:form>
</apex:page>

 
Amit Chaudhary 8Amit Chaudhary 8
Please post your Apex class
Nitin SharmaNitin Sharma
public with sharing class accountSearch {
  
   public list <Account> acc {get;set;}
  
   public string searchstring {get;set;} 
   public string value1 {get;set;} 
   
   
    
   public accountSearch(ApexPages.StandardController controller) {
     
   }  
   public void search(){  
     if(searchstring != null && searchstring != '' ){  
     string searchquery='select Name, Phone from Account where Name like \'%'+searchstring+'%\'  Limit 10';  
     
     acc= Database.query(searchquery); 
     
     }
     }
     
    
   }

 
Amit Chaudhary 8Amit Chaudhary 8
Please check below post. I hope that will help you
1) http://amitsalesforce.blogspot.in/2016/03/wrapper-class-in-salesforce-select-all.html
public with sharing class WrapperDemoController {
    
    public List<AccountWrapper> listAccountWrapper {get; set;}
    public List<Account> selectedAccounts{get;set;}

    public WrapperDemoController ()
    {
            listAccountWrapper = new List<AccountWrapper>();
            searchRecord();
    }
    
    public void searchRecord()
    {
        listAccountWrapper.clear();
            for(Account a: [select Id, Name,BillingState, Website, Phone ,Active__c from Account limit 10]) 
            {
                listAccountWrapper.add(new AccountWrapper(a));
            }
    }

    public void processSelected() 
    {
        selectedAccounts = new List<Account>();
        selectedAccounts.clear();
        for(AccountWrapper wrapAccountObj : listAccountWrapper) 
        {
            if(wrapAccountObj.selected == true) 
            {
                selectedAccounts.add(wrapAccountObj.acc);
            }
        }
    }

    public void ActivateData() 
    {  // Add create contact Code here
        for(Account acc : selectedAccounts )
        {
            acc.Active__c ='Yes';
        }
        update selectedAccounts ;
        searchRecord();
    }

    public void DeActivateData() 
    {// Add create contact Code here
        for(Account acc : selectedAccounts )
        {
            acc.Active__c ='No';
        }
        update selectedAccounts ;
        searchRecord();
    }
    


    // This is our wrapper/container class. 
    public class AccountWrapper 
    {
        public Account acc {get; set;}
        public Boolean selected {get; set;}
        public AccountWrapper(Account a) 
        {
            acc = a;
            selected = false;
        }
    }

}
<apex:page controller="WrapperDemoController">
    <script type="text/javascript">
        function selectAllCheckboxes(obj,receivedInputID){
            var inputCheckBox = document.getElementsByTagName("input");
            for(var i=0; i<inputCheckBox.length; i++){
                if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
                    inputCheckBox[i].checked = obj.checked;
                }
            }
        }
    </script>
    <apex:form >
        <apex:pageBlock id="PB1">
            <apex:pageBlockButtons >
                <apex:commandButton value="Add to Grid" action="{!processSelected}" rerender="table2,PB2"/>
            </apex:pageBlockButtons>

            <apex:pageblockSection title="All Accounts" collapsible="false" columns="1">
                <apex:pageBlockTable value="{!listAccountWrapper}" var="accWrap" id="table" title="All Accounts">
                    <apex:column >
                        <apex:facet name="header">
                            <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                        </apex:facet>
                        <apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/>
                    </apex:column>
                    <apex:column value="{!accWrap.acc.Name}" />
                    <apex:column value="{!accWrap.acc.BillingState}" />
                    <apex:column value="{!accWrap.acc.Phone}" />
                    <apex:column value="{!accWrap.acc.Active__c}" />
                </apex:pageBlockTable>


            </apex:pageblockSection>
        </apex:pageBlock>
        
        <apex:pageBlock id="PB2" >
            <apex:pageBlockButtons >
                <apex:commandButton value="Activate" action="{!ActivateData}" rerender="PB1,PB2"/>
                <apex:commandButton value="DeActivate" action="{!DeActivateData}" rerender="PB1,PB2"/>
            </apex:pageBlockButtons>

                <apex:pageBlockTable value="{!selectedAccounts}" var="c" id="table2" title="Selected Accounts">
                    <apex:column value="{!c.Name}" headerValue="Account Name"/>
                    <apex:column value="{!c.BillingState}" headerValue="Billing State"/>
                    <apex:column value="{!c.Phone}" headerValue="Phone"/>
                    <apex:column value="{!c.Active__c}" headerValue="Active"/>
                </apex:pageBlockTable>
        </apex:pageBlock>

        
    </apex:form>
</apex:page>

Let us know if this will help you


 
Nitin SharmaNitin Sharma
Hi Amit,

According to your code we can fatch account record and activate or deactivate field on account object.
But i want to create contact for selected account .
Amit Chaudhary 8Amit Chaudhary 8
Yes try to change your code like below
public pageReference ActivateData() 
    {
		Contact cont = new Contact();
	
        for(Account acc : selectedAccounts )
        {
            cont.FirstName ='Test';
            cont.LastName ='Test';
            cont.Accountid = acc.id;
			
        }
        insert cont ;
		
		return new pageReference('/'+cont.id)
    }

 
Nitin SharmaNitin Sharma
Not working