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
SayasoniSayasoni 

Visualforce List View Page with Edit and Delete

I have a controller which aids in displaying a visualforce page with the Edit & delete functionality.However, my delete doesn't seem to work.

Kindly assist.Am using this post as a guide:

public class OrderDealsExtension {
	
	public List<Deals__c> deals {get;set;}
	public String SelectedDealId {get;set;}
	
	public OrderDealsExtension() {
		loadData();
	}
		
	public void loadData() {
			
		deals = [Select id,Name,Deal_Start_Date__c,Candidates__c,Contact__c,Deal_End_Date__c,Deal_Type__c,Deal_Buy_Price__c,Deal_Client_Rate__c,CreatedDate from Deals__c Order By CreatedDate desc];
	}
	
	public void deleteDeal(){
		if(SelectedDealId == null){
			return;
		}
		//find the deal record within the collection
		Deals__c tobeDeleted = null;
		for(Deals__c d :deals){
			if(d.Id == SelectedDealId){
				tobeDeleted = d;
				break;
			}
			
			//if deal record found delete it
			if(tobeDeleted != null){
				Delete tobeDeleted;
			}
			
			//refresh the data
			loadData();
		}
	}
	
}

Visualforce Page.

<apex:page controller="OrderDealsExtension">
<apex:form id="form" >
<apex:pageBlock title="Deals">
  <apex:pageMessages ></apex:pageMessages>
  <apex:pageBlockTable value="{!deals}" var="d">
     <apex:column >
       <apex:outputLink title="" value="/{!d.id}/e?retURL=/apex/{!$CurrentPage.Name}" style="font-weight:bold">Edit</apex:outputLink>&nbsp;|&nbsp;
       <a href="javascript&colon;if (window.confirm('Are you sure?')) deleteDeal('{!d.Id}');" style="font-weight:bold">Del</a>
     </apex:column>
    <apex:column headervalue="Order Number" >
	<apex:outputLink value="/{!d.id}">
	<apex:outputField value="{!d.Name}"/>
	</apex:outputLink>
</apex:column>
<apex:column value="{!d.Candidates__c}" />
<apex:column value="{!d.Contact__c}" />
<apex:column value="{!d.Deal_Start_Date__c}" />
<apex:column value="{!d.Deal_End_Date__c}" />
<apex:column value="{!d.Deal_Buy_Price__c}" />
<apex:column value="{!d.Deal_Client_Rate__c}" />
  </apex:pageBlockTable>
</apex:pageBlock>

<apex:actionFunction action="{!deleteDeal}" name="DeleteDeal" reRender="form" >
   <apex:param name="dealid" value="" assignTo="{!SelectedDealId}"/>
</apex:actionFunction>
</apex:form>
</apex:page>

 

 

Best Answer chosen by Admin (Salesforce Developers) 
SayasoniSayasoni

I got another simple way of doing it.I used the commandlink function instead of ActionFunction on my VF and also simplified my Delete method in my controller:

 public void deleteDeal(){
        deals = [Select id,Name,Deal_Start_Date__c,Candidates__c,Contact__c,Deal_End_Date__c,Deal_Type__c,Deal_Buy_Price__c,Deal_Client_Rate__c,CreatedDate from Deals__c where id = :dealId];
       if(deals.size() > 0 || deals[0].Id != ''){
       delete deals;
       }
       loadData();
   
    }

 

 <apex:commandLink action="{!deleteDeal}" onclick="if(!confirm('Are you sure?')) return false;">Del
    <apex:param value="{!d.Id}" name="idToDel" assignTo="{!dealId}"/>
</apex:commandLink>

 

All Answers

SayasoniSayasoni

I got another simple way of doing it.I used the commandlink function instead of ActionFunction on my VF and also simplified my Delete method in my controller:

 public void deleteDeal(){
        deals = [Select id,Name,Deal_Start_Date__c,Candidates__c,Contact__c,Deal_End_Date__c,Deal_Type__c,Deal_Buy_Price__c,Deal_Client_Rate__c,CreatedDate from Deals__c where id = :dealId];
       if(deals.size() > 0 || deals[0].Id != ''){
       delete deals;
       }
       loadData();
   
    }

 

 <apex:commandLink action="{!deleteDeal}" onclick="if(!confirm('Are you sure?')) return false;">Del
    <apex:param value="{!d.Id}" name="idToDel" assignTo="{!dealId}"/>
</apex:commandLink>

 

This was selected as the best answer
Ryan McNeelyRyan McNeely
Sayasoni, I tried your suggestion almost verbatum. Could you take a look at my final post here:
https://developer.salesforce.com/forums/ForumsMain?id=906F0000000kEck
Mayank Jain 49Mayank Jain 49
Could somebody suggest me how to write test class for above code.I am not getting coverage for "deleteDeal()" method.
Mukesh Verma 21Mukesh Verma 21
Sayasoni ! above code is working fine but i need to save chage on edit sceen. But there is no save button on edit screen. So please tell me how i need to add save button on it