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
max alexandermax alexander 

Visualforce create child object from visual force search page

HI,
Ihave following VF page
<apex:page standardController="account" extensions="searchform">  
  <apex:form >  
 <apex:inputText value="{!searchstring}" label="Input"/>   
  <apex:commandButton value="Search records" action="{!search}"/>  
  <apex:commandButton value="Clear records" action="{!search}"/>  
   <apex:pageBlock title="Search Result">  
    <apex:pageblockTable value="{!acc}" var="a">  

     <apex:column >  
      <apex:outputlink value="https://cochlear--guest1.cs8.my.salesforce.com/{!a.id}">{!a.Name}</apex:outputlink>  
    
     </apex:column>  
      
      <apex:column value="{!a.personEmail}"/> 
       <apex:column value="{!a.phone}"/> 
       <apex:column value="{!a.personMobilephone}"/>
     
        
       
       
  
    </apex:pageBlockTable>  

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

 </apex:page>
basically it does searches when i open the page, however what i like to include is another button on it that when clicked will let me create custom object
Person__c where it will have only first name, last name and Email address how can i do this?
my controller is
public with sharing class searchform {  
   public list <account> acc {get;set;}  
   public list <Install_Base__c> ib {get;set;}  
   public string searchstring {get;set;}  
   public searchform(ApexPages.StandardController controller) {  
   }  
   public void search(){  
     string searchquery='select name,phone,personEmail,PersonMobilePhone,Latest_Fitting_Date__c from account where name like \'%'+searchstring+'%\' Limit 20';  
     acc= Database.query(searchquery);  
   }  
   public void clear(){  
   acc.clear();  
   }  
  

 }

how is it possible, also
is there  a way to display on that page most recent Person Added to that account? 
any code would be greatly appreciated
SaketJoshiSaketJoshi
Do you intend to create a new record for that object or a new object itself?
max alexandermax alexander
i would like to crate new records using custom object
basicaly i need a VF button whne searhc displayed next to each of hte displayed accounts that when clicked will create new Person__c records asking for name and email adress before creating it.
SaketJoshiSaketJoshi
For that, you need to create some text fields in the visualforce page, and some strings in the controller of the page
Let's start with the controller:

Apex Code:

public String firstName {get;set;}
public String lastName {get;set;}
public String emailAdd {get;set;}

public void addNewPersonRecord() {
   if(!(String.isEmpty(firstName) && String.isEmpty(lastName) && String.isEmpty(emailAdd)) {
        Person__c newRecord = new Person__c();
        newRecord.firstName__c = firstName;
        newRecord.lastName__c = lastName;
        newRecord.email__c = emailAdd;
        insert newRecord;
   }
}

Visualforce Page:

<apex:form>
    First Name: <apex:inputText value="{!firstName}" />
    Last Name: <apex:inputText value="{!lastName}" />
    Email: <apex:inputText value="{!emailAdd}" />

    <apex:commandButton action="{!addNewPersonRecord}" value="Add Record" />
</apex:form>

I think the code is self explainatory, if you need any more help, you might ask

Regards,
-Saket