Monday, May 26, 2014

How to Create Visualforce Related List with Edit and Delete Action's?

How to Create Visualforce Related List with Edit and Delete Action's?


I have requirement of create a Visualforce Related List to display opportunity Details in Contact object with Edit and Delete Actions.

Page:- 

<apex:page standardController="Contact" extensions="sampleDetailPagecon">
<style>
.fewerMore { display: none;}
</style>
 <apex:form >
 <apex:pageMessages />
 <apex:detail relatedList="true"></apex:detail>

 <apex:pageblock id="CustomList" title="Related Opportunities">
 <apex:pageblocktable value="{!oppz}" var="o" rendered="{!NOT(ISNULL(oppz))}">
  <apex:column >
             <apex:facet name="header">Action</apex:facet>
                        
  <apex:commandLink action="{!editOpp}" value="Edit">
  <apex:param name="oppid" value="{!o.id}"/>
 </apex:commandLink> &nbsp; | &nbsp;   <apex:commandLink action="{!deleteOpp}" value="Delete">
  <apex:param name="oppid" value="{!o.id}"/>
 </apex:commandLink>
 </apex:column>
 <!-- 
 <apex:column >
   <apex:commandLink action="{!deleteOpp}" value="Delete">
  <apex:param name="oppid" value="{!o.id}"/>
 </apex:commandLink>
 </apex:column>
 -->
 <apex:column value="{!o.Name}"/>
 <apex:column value="{!o.Account.Name}"/>
 <apex:column value="{!o.Type}"/>
    <apex:column value="{!o.CloseDate}"/>    
 </apex:pageblocktable>
 </apex:pageblock>
 </apex:form>
  <apex:relatedList list="Opportunities" />
</apex:page> 

Controller Class :-


public class sampleDetailPagecon {
private List<Opportunity> oppz;
private Contact cntact;
public sampleDetailPageCon(ApexPages.StandardController controller) {
 this.cntact=(Contact)controller.getRecord();
 }
 public List<Opportunity> getOppz()
 {
     Contact con = [Select id, Account.id FROM Contact where id =:cntact.id];
     if(con.Account==null)
     return null;
     oppz = [Select id, Name, Account.Name, CloseDate, Amount, Type from Opportunity where Account.id =: con.Account.Id];
     return oppz;
 }
 public Pagereference editOpp()
 {
     String oppid= ApexPages.currentPage().getParameters().get('oppid'); 
     
    return new PageReference('/006/e?id='+oppid+'&retURL=/006/');
 }
  public Pagereference deleteOpp()
 {
     String oppid= ApexPages.currentPage().getParameters().get('oppid'); 
     
     Opportunity opp = [Select id from Opportunity where id =:oppid limit 1];
     if(opp !=null && opp.id !=null){
      delete opp;
     }
    return null;
 }
}


once this is done you need to add the Visualforce section in Contact object with Edit page layout and add this visualforce section in contact related list.

Thats it !!!!!!

4 comments:

Unknown said...

Good one

Unknown said...

I am getting the FATAL_ERROR|System.QueryException: List has no rows for assignment to SObject

Unknown said...

Hi Srini,

How we can test this code? Please update the details in detailed.

akhilapriya404 said...

The best way to learn is to teach salesforce Online Training Hyderabad

Post a Comment

 
| ,