Tuesday, March 12, 2013

Displaying records of Account, Contact, Lead and Opportunity with edit and delete link in tabs


Visualforce Page:

<apex:page controller="datarecordscls">
 <apex:form >
     <apex:tabPanel switchType="client" >
         <apex:tab label="Accounts">
            <apex:pageBlock >
                 <apex:PageBlockTable value="{!lstacc}" var="a">
                    <apex:column headerValue="Action" >
                        <apex:CommandLink value="Edit" action="{!editrec}">
                            <apex:Param value="{!a.id}" assignto="{!pid}" name="a"/>
                        </apex:Commandlink> &nbsp;&nbsp;
                        <apex:CommandLink value="Del" action="{!delrec}">
                            <apex:param value="{!a.id}" assignTo="{!aid}" name="ad"/>
                        </apex:commandLink>
                    </apex:Column> 
                    <apex:column value="{!a.name}"/>
                 </apex:PageBlockTable>
             </apex:pageBlock>
         </apex:tab>
         <apex:tab label="Contacts">
             <apex:pageBlock >
                 <apex:PageBlockTable value="{!lstcon}" var="c">
                       <apex:column headerValue="Action" >
                        <apex:CommandLink value="Edit" action="{!editrec}">
                            <apex:Param value="{!c.id}" assignto="{!pid}" name="c"/>
                        </apex:Commandlink> &nbsp;&nbsp;
                        <apex:CommandLink value="Del" action="{!delrec}">
                            <apex:param value="{!c.id}" assignTo="{!cid}" name="cd"/>
                        </apex:commandLink>
                    </apex:Column> 
                    <apex:column value="{!c.name}"/>                 
                 </apex:PageBlockTable>
             </apex:pageBlock>
         </apex:tab>
         <apex:tab label="Leads">
             <apex:pageBlock >
                 <apex:PageBlockTable value="{!lstlead}" var="l">
                   <apex:column headerValue="Action" >
                        <apex:CommandLink value="Edit" action="{!editrec}">
                            <apex:Param value="{!l.id}" assignto="{!pid}" name="l"/>
                        </apex:Commandlink> &nbsp;&nbsp;
                        <apex:CommandLink value="Del" action="{!delrec}">
                            <apex:param value="{!l.id}" assignTo="{!lid}" name="ld"/>
                        </apex:commandLink>
                    </apex:Column> 
                     <apex:column value="{!l.name}"/>
                 </apex:PageBlockTable>
             </apex:pageBlock>
         </apex:tab>
         <apex:tab label="Opportunities">
             <apex:pageBlock >
                 <apex:PageBlockTable value="{!lstopp}" var="o">
                   <apex:column headerValue="Action" >
                        <apex:CommandLink value="Edit" action="{!editrec}">
                            <apex:Param value="{!o.id}" assignto="{!pid}" name="o"/>
                        </apex:Commandlink> &nbsp;&nbsp;
                        <apex:CommandLink value="Del" action="{!delrec}">
                            <apex:param value="{!o.id}" assignTo="{!oid}" name="od"/>
                        </apex:commandLink>
                    </apex:Column> 
                    <apex:column value="{!o.name}"/>                 
                 </apex:PageBlockTable>
             </apex:pageBlock>
         </apex:tab>
     
     </apex:tabPanel>
 </apex:form>
</apex:page>

Apex Class:

public with sharing class datarecordscls {

    public PageReference delrec() {
    if(aid != null){
        Account acc = [Select id from account where id=:aid];
        delete acc;
                       }
    if(cid != null){
        Contact c = [Select id from Contact where id=:cid];
        delete c;
                       }
    if(oid != null){
        Opportunity o =[Select id from Opportunity where id =:oid];
        delete o;
                       }
    if(lid != null){
        Lead l =[Select id from Lead where id=:lid];
        delete l;
                       }
        pagereference ref= new pagereference ('/apex/datarecords');
        ref.setredirect(true);
        return ref;
                                                      }

    public PageReference editrec() {
       pagereference ref = new pagereference('/'+pid+'/e');
       return ref;
                                                    }
    public string aid{get; set;}
    public string cid{get; set;}
    public string oid{get; set;}
    public string lid{get; set;}
                
    public string pid {get; set;}
    public list<Account> lstacc{get; set;}
    public list<Contact> lstcon{get; set;}
    public list<Lead> lstlead{get; set;}
    public list<Opportunity> lstopp{get; set;}
                
    public datarecordscls (){
    
        lstacc=[Select id, name from Account];
        lstcon=[Select id, name from Contact];
        lstLead=[Select id, name from Lead];
        lstopp=[Select id, name from Opportunity];
                                         }            
                                                          }

No comments:

Post a Comment