Deleting records:
In this scenario we used custom controller to perform delete action, and pageblocktable to display records and command link to delete individual records. In this we used ‘apex:param’ for transmitting individual record ids from vf to apex class.
Visualforce page:
<apex:page controller="DeleteTestingNew" sidebar="false">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockTable value="{!records}" var="r">
<apex:column headerValue="Action">
<apex:commandlink value="Delete" action="{!doDelete}">
<apex:param name="rId" value="{!r.Id}" assignTo="{!rId}"/>
</apex:commandlink>
</apex:column>
<apex:column headerValue="Name">
{!r.name}
</apex:column>
<apex:column headerValue="city">
{!r.City__c}
</apex:column>
<apex:column headerValue="phone">
{!r.Phone__c}
</apex:column>
<apex:column headerValue="country">
{!r.Country__c}
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Purpose:
Displays a list of DataLoadTest__c
records in a table and allows users to delete individual records.
Key Components:
Purpose:
Handles the logic for retrieving records and deleting a selected record based on its Id
.
Key Components:
Apex Class: DeleteTestingNew
Purpose:
Handles the logic for retrieving records and deleting a selected record based on its Id
.
Key Components:
- Class Declaration:
ApexClass:
public with sharing class DeleteTestingNew {
public String rId{get;set;}
DataLoadTest__c delDlt = new DataLoadTest__c();
public PageReference doDelete() {
delDlt=[select Id from DataLoadTest__c where id =:rId];
delete delDlt;
pagereference ref=new pagereference('/apex/newdelete');
ref.setredirect(true);
return ref;
}
List<DataLoadTest__c> lstdlt = new List<DataLoadTest__c>();
public List<DataLoadTest__c> getRecords() {
lstdlt =[select id,name,city__c,country__c,phone__c from DataloadTest__c];
return lstdlt;
}
}
Class Declaration:
public with sharing class DeleteTestingNew {
with sharing
enforces the current user’s sharing rules.
Variables:
public String rId { get; set; }
DataLoadTest__c delDlt = new DataLoadTest__c();
List<DataLoadTest__c> lstdlt = new List<DataLoadTest__c>();
rId
: Stores theId
of the record to be deleted, received from the Visualforce page.delDlt
: Temporary variable to hold the record to delete.lstdlt
: List to store fetched records for display.
doDelete Method
public PageReference doDelete() {
delDlt = [SELECT Id FROM DataLoadTest__c WHERE Id = :rId];
delete delDlt;
PageReference ref = new PageReference('/apex/newdelete');
ref.setRedirect(true);
return ref;
}
- Query: Retrieves the specific
DataLoadTest__c
record usingrId
. - Delete: Deletes the fetched record from the database.
- Redirect: Navigates the user back to the
/apex/newdelete
page after deletion.
getRecords Method
public List<DataLoadTest__c> getRecords() {
lstdlt = [SELECT Id, Name, City__c, Country__c, Phone__c FROM DataLoadTest__c];
return lstdlt;
}
- Fetches all
DataLoadTest__c
records with selected fields.Provides the data to the Visualforce page for display in the table.
Workflow Summary
- Display Records:
- The Visualforce page loads and calls
getRecords()
, which retrieves allDataLoadTest__c
records. - Records are displayed in a table with columns for Name, City, Phone, and Country.
- The Visualforce page loads and calls
- Delete Action:
- Each record row has a “Delete” link.
- Clicking “Delete” assigns the record’s
Id
torId
viaapex:param
and invokesdoDelete()
.
- Processing Deletion:
doDelete()
queries the specific record usingrId
.- Deletes the record from Salesforce.
- Redirects the user to the
/apex/newdelete
page, refreshing the table to reflect the deletion.