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
Pavan Kumar 1141Pavan Kumar 1141 

Hi All,Can any

Hi All,
         Can anyone help me to get the below output using VF and Apex classes.Out put format

Thank You.
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Pavan,

Please check the below code similar to your requirement.

Visualforce Page
<apex:page controller="searchname1">
  <apex:form >
   <apex:pageblock id="one" >
    <apex:pageblockSection >
    <apex:pageblocksectionitem >
       <apex:outputlabel >Name</apex:outputlabel>
         <apex:inputtext value="{!name}"/>
         </apex:pageblocksectionitem>
         <apex:commandButton value="go" action="{!search}"/>
          </apex:pageblockSection>
           </apex:pageBlock> 
           <apex:pageBlock rendered="{!searched}" >
            <apex:pageblocktable value="{!mycar}" var="c">
            <apex:column value="{!c.name}"/>
             <apex:column value="{!c.Price__c}"/>
            </apex:pageblocktable>
            <apex:pageblockbuttons >
             <apex:commandButton value="edit" action="{!edit}"/>
            </apex:pageblockbuttons>
               </apex:pageblock>
  </apex:form>
</apex:page>
Apex code:
public with sharing class searchname1 {

    public PageReference edit() {
       
        return null;
    }


   // public String mycar { get; set; }
   public String name { get; set; }
    public list<car__c> mycar { get; set; }
    public boolean searched{get;set;}
    
    //default constructor
    public searchname1(){
    searched=false;
    string namestr=apexpages.currentpage().getparameters().get('name');
    if(null!=namestr){
    name=namestr;
    }
    }
    public PageReference search() {
    searched=true;
     string searchstr1=('%'+name+'%');
    mycar=[select id,Name,price__c from car__c where name like :searchstr1 limit 10 ];
        return null;
    }


    
}


Please check the below screenshot.
User-added image
hope it helps.

Please mark it as best answer if the information is informative.

Thanks
Rahul Kumar
mukesh guptamukesh gupta
Hi Pavan,

Please use below code for Add,Edit and Delete
 
Vfpage:-
Name :- DML Vfpage
<apex:page Controller="editdeleteCOn" >
<apex:form >
 <apex:sectionHeader title="Edit/Delete" subtitle="Auditing Accounts"/>
 <div align='right'>
 <apex:commandButton value="Insert New" action="{!insertNew}" style="width:150px"/>
  </div>
  <br/>
  <apex:pageBlock >
   <apex:pageBlockTable value="{!lstacc}" var="acc">
     <apex:column > 
     {!acc.name}
     <apex:param name="aid" value="{!acc.id}" />
     </apex:column>
     <apex:column value="{!acc.Website}" />
     <apex:column value="{!acc.phone}" />
     <apex:column headerValue="Action" >
      <apex:commandLink value="Edit" action="{!editCon}"> 
      <apex:param name="cid" value="{!acc.id}" assignTo="{!ecid}"/>
      </apex:commandlink>
      &nbsp;&nbsp;/&nbsp;&nbsp;
      <apex:commandLink value="Delete" action="{!deleteCon}">
      <apex:param name="cid" value="{!acc.id}" assignTo="{!dcid}"/>
      </apex:commandLink>
      &nbsp;&nbsp;/&nbsp;&nbsp;
      <apex:commandLink value="Clone" action="{!cloneCon}">
      <apex:param name="cid" value="{!acc.id}" assignTo="{!ccid}"/>
      </apex:commandLink>
     </apex:column>
   </apex:pageBlockTable>
   <apex:detail subject="{!$CurrentPage.parameters.aid}"/>
  </apex:pageBlock>
  </apex:form>
</apex:page>
 
Controller:-
public class editdeleteCOn {
 //global declarations
   public String ecid{get;set;} // commandlink edit property
   public String dcid{get;set;} // commandlink delete property
   public String ccid{get;set;} // commandlink clone property
   list<Account> lstacc = new list<Account>();
   list<Account> lstacc1 = new list<Account>();
   // Displaying accounts on vfpage
    public list<Account> getlstacc () {
      lstacc =[select Name,phone,website from Account];
      return lstacc;
    }
    // To redirect to editpage.
     public PageReference editCon() {
       pagereference ref = new pagereference('/apex/dataedit?id='+ecid);
       ref.setRedirect(False);
       return ref;   
    }
    // to delete the selected record
       public pagereference deleteCon() {
        lstacc1 =[Select id,Name from Account where id=:dcid];
        delete lstacc1;
        pagereference ref = new pagereference('/apex/editdelete');
        ref.setredirect(True);
        return ref;   
    }
     // to redirect to insert page.
    public PageReference insertNew() {
    pagereference ref = new pagereference('/apex/accountinsert');
    ref.setRedirect(True);
    return ref; 
    }
    // for cloning the records.
    public PageReference cloneCon() {
    account c =  [Select id,Name From account where id =:ccid];
    account cloneaccount = c.clone(false);
    insert cloneaccount;
    pagereference ref = new pagereference('/apex/editdelete');
    ref.setRedirect(True);
    return ref; 
    }
}

When you click on Edit link i am redirecting to another vfpage.
 
Vfpage : dataedit
<apex:page standardController="Account" extensions="savecon">
<apex:form >
  <apex:sectionHeader title="Editing Demo" subtitle="Edit"/>
   <apex:pageBlock tabStyle="Account" >
   <apex:pageBlockButtons location="Bottom">
   <apex:commandButton action="{!save}" value="Save"/>
   </apex:pageBlockButtons>
        <apex:pageBlockSection >
          <apex:inputField value="{!account.Name}"/>
          <apex:inputField value="{!account.Website}"/>
          <apex:inputField value="{!account.phone}"/>
        </apex:pageBlockSection>
   </apex:pageBlock>
</apex:form>
</apex:page>
Controller:savecon
public with sharing class savecon {
    ApexPages.StandardController con;
    public savecon(ApexPages.StandardController controller)
       con=controller;
    }
    public pagereference save()
    {
        con.save(); 
        return new pagereference(page.editdelete.getUrl());
    }
}
When you click on insert ,i am displaying another page to insert records from there,
Vfpage: accountinsert
 
<apex:page standardController="Account"  extensions="accountinsertcon">
<apex:form >
  <apex:sectionHeader title="Inserting New" subtitle="Account"/>
   <apex:pageBlock >
      <apex:pageBlockButtons >
      <apex:commandButton value="Save" action="{!save}"/>
      </apex:pageBlockButtons>
      <apex:pageBlockSection >
     <apex:inputField value="{!account.Name}"/>
     <apex:inputField value="{!account.Phone}"/>
     <apex:inputField value="{!account.Website}"/>
     <apex:inputField value="{!account.AnnualRevenue}"/>
     </apex:pageBlockSection>
   </apex:pageBlock>
</apex:form>
</apex:page>

Controller :accountinsertCon
public class accountInsertCOn {
    ApexPages.StandardController con;
    public accountInsertCOn(ApexPages.StandardController controller) {
    con=controller;
    }
    public pagereference save()
    {
       con.save();
       pagereference ref = new pagereference('/apex/editdelete');
       ref.setRedirect(TRUE);
       return ref;
    }
}


Kindly mark my solution as the best answer if it helps you.


Regards

Mukesh

mukesh guptamukesh gupta
Hi Pawan,

I have developed same code as you need, 
User-added image

vfAddNewAccount:-
 
<apex:page standardController="Contact" extensions="addNewContact" id="page">

 <apex:form id="frm">
 
 <table>

            <tr>
                <th>First Name:</th>
                <td><apex:inputText id="fname" value="{!Contact.FirstName}" /></td>
            </tr>
           
           
             <tr>
                <th>Last Name:</th>
                <td><apex:inputText id="lname" value="{!Contact.LastName }"/></td>
            </tr>
           
 
 </table>
    
    <apex:commandButton value="Add Contact" action="{!ShowAccountList}" rerender="cont" oncomplete="this.form.reset();return false;"/>
   <apex:pageBlock id="cont" >
 
    
       <apex:pageblockSection title="Account Details">
            <apex:pageBlockTable value="{!contlist}" var="c">
                <apex:column value="{!c.FirstName}"/>
                <apex:column value="{!c.LastName}"/>
                <apex:column headerValue="Action">
                <apex:commandLink value="Edit" action="{!editAction}">
                <apex:param name="cid"  value="{!c.id}" assignTo="{!cedit}"/>
                </apex:commandLink>
                / &nbsp;
                <apex:commandLink value="Delete" action="{!deleteAction}" onclick="if (!confirm('Do you want to proceed?')) return false;" reRender="cont" >
                <apex:param name="contid" value="{!c.id}" assignTo="{!cdelete}" />
                </apex:commandLink>
                </apex:column>
            </apex:pageBlockTable>
            
       </apex:pageblockSection>  
       
        
   </apex:pageBlock>

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

addNewContact:
public class addNewContact {
  ApexPages.StandardController con;

    public list<contact> contlist{get;set;}
    public list<Contact> cont = new List<Contact>();
    
    public string cedit{get;set;}
    public string cdelete{get;set;}
 
    public addNewContact(ApexPages.StandardController controller) {
     
        con = controller;
        ShowAccountList();
        
    }
    
    public PageReference deleteAction(){
    
        cont = [select id from contact where id=:cdelete];
        delete cont ;
        ShowAccountList();
        return null;
    }
    
    public PageReference editAction(){
       Pagereference ref = new Pagereference('/apex/vfEditContact?id='+cedit);
       ref.setRedirect(false);
       return ref;
    }
    
     public void ShowAccountList(){
        con.save();
        contlist = [Select firstname ,LastName from contact];
       
    }
    
   
}

vfEditContact​:-
<apex:page standardController="contact" extensions="editContact">

 <apex:form >
 <apex:pageMessages />
     <table>
            <tr>
                <th>First Name:</th>
                <td><apex:inputText value="{!Contact.FirstName}"/></td>
            </tr>
           
           
             <tr>
                <th>Last Name:</th>
                <td><apex:inputText value="{!Contact.LastName }"/></td>
            </tr>
            
      </table>
      <br/>
      <br/>
 
      <apex:commandButton value="Edit Contact" action="{!save}"/>

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

editContact :-
public class editContact {

    ApexPages.StandardController con;
    

    public editContact(ApexPages.StandardController controller) {
        con = controller;

    }
    
    
    public pagereference save(){
       con.save();
        
       Pagereference ref = new Pagereference('/apex/vfAddNewAccount');
       ref.setRedirect(true);
       return ref;
        
        
    }
    

}


Please use this code!!

Kindly mark my solution as the best answer if it helps you.


Regards
Mukesh