Activity › Forums › Salesforce® Discussions › How i can delete selected list value on button click in Salesforce?
Tagged: Custom Button, List Button, Salesforce List
-
How i can delete selected list value on button click in Salesforce?
Posted by Shaharyar on August 22, 2017 at 7:04 AMI have select List and i want to delete any of the selected value by clicking on Delete button.
Example:
Suppost i have four value in select List
A
B
C
D
then when i cselet option B
and click on delete button then that value will be deleted from selected list.
William replied 7 years, 11 months ago 4 Members · 4 Replies -
4 Replies
-
Hi Shaharyar,
I have written code for deleting selected contacts.
Apex Controller:-
public class WrapperDeleteContactsAccounts
{
List<WrapContacts> conListWrap ;
List<Account> accounts ;
List<Contact> contacts ;
List<Contact> delContacts;
public WrapperDeleteContactsAccounts()
{
conListWrap = new List<WrapContacts>();
accounts = new List<Account>();
contacts = new List<Contact>();
delContacts = new List<Contact>();
}
public class WrapContacts
{
public Contact con {get;set;}
public Boolean check {get;set;}
public WrapContacts(Contact c)
{
con = c;
check = false;
}
}
public List<WrapContacts> getContacts()
{
contacts = [SELECT Id, LastName, AccountId, Gender__c FROM Contact LIMIT 100];
for(Contact c : contacts)
{
conListWrap.add(new WrapContacts(c));
}
return conListWrap;
}
public PageReference deleteContacts()
{
PageReference PageRefer = new PageReference(‘/apex/WrapperDelPage’);
PageRefer.setRedirect(true);
delContacts = new List<Contact>();
for(WrapContacts c : conListWrap)
{
if(c.check == true)
{
delContacts.add(c.con);
}
}
try
{
delete delContacts;
}
catch(Exception e)
{
System.debug(‘m=====’+e.getMessage());
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,e.getMessage()));
return null;
}
return PageRefer;
}
}Apex Page:-
<apex:page controller=”WrapperDeleteContactsAccounts” sidebar=”false”>
<apex:form >
<apex:pageBlock id=”pb”>
<apex:pageBlockButtons >
<apex:commandButton value=”delete selected” action=”{!deleteContacts}” reRender=”pb” />
</apex:pageBlockButtons>
<apex:pageMessages />
<apex:pageBlockTable value=”{!Contacts}” var=”cont” columns=”5″ Id=”pb1″>
<apex:column headerValue=”select”>
<apex:inputCheckbox value=”{!cont.check}”/>
</apex:column>
<apex:column value=”{!cont.con.LastName}”/>
<apex:column value=”{!cont.con.Gender__c}”/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>Hope this helps.
- [adinserter block='9']
-
hello Shaharyar
Please Try below Code,Its for Contact you may use for other Object
*****Controller****
public class deleteContactController
{
public List<wrapContact> listWrapContact {get; set;}
public class wrapContact
{
public Contact con {get; set;}
public Boolean selected {get; set;}
public wrapContact(Contact c)
{
con = c;
selected = false;
}
}public deleteContactController()
{
listWrapContact = new List<wrapContact>();list<Contact> con = [select id,lastname, (select id from cases) from Contact];
for(Contact c : con)
{
if(c.cases.size() == 0){
listWrapContact.add(new wrapContact(c));
}}
}public void processSelected() {
List<Contact> lstconToDelete = new List<Contact>();
for(wrapContact wcon: listWrapContact)
{
if(wcon.selected == true)
{
lstconToDelete.add(wcon.con);
}
}if(lstconToDelete.size() > 0 )
{
Delete lstconToDelete;
}}
}****Vf Page****
<apex:page Controller = “deleteContactController”>
<apex:form >
<apex:pageBlock Title=”Contacts”>
<apex:commandButton value=”Delete Selected” action=”{!processSelected}”/>
<apex:pageBlockSection >
<apex:pageBlockTable value=”{!listWrapContact}” var=”a”>
<apex:column >
<apex:inputCheckbox value=”{!a.selected}”/>
</apex:column>
<apex:column value=”{!a.con.id}”/>
<apex:column value=”{!a.con.lastname}”/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page> -
hi Shariq, Shubham
Thanks for your response.
I have some more requirements, i want to create 1 more button to update the selected value.
the form should be opened on the same page .
-
We can delete record in two ways
1. By using custom button
2. Visualforce page
By using custom button:
Go to account setup->Account -> Buttons, Links, and Actions->New Button or link
Choose display type list view
Behavior type execute java script
Content Source onclick Java script
{!REQUIRESCRIPT(“/soap/ajax/10.0/connection.js”)}
var isdelete = {!GETRECORDIDS( $ObjectType.Account)};
var deleteRecord= ‘Are you sure to delete ‘ +isdelete.length+ ‘ Records?’;
if(isdelete.length&& (window.confirm(deleteRecord)))
{
sforce.connection.deleteIds(isdelete,function()
{navigateToUrl(window.location.href);
});
}
else if (isdelete.length == 0)
{
alert(“Please select Account record to delete”);
}
Then go to search layout add button to list view
3. Visualforce page
To fulfill this requirement you need a wrapper class, main class and visualforce page
Class
public class AccountDelete {
public list<Account> Acclist{get;set;}
public ID currentPageID;
public list<DeleteExample> accWrap {get;set;}
public AccountDelete(ApexPages.StandardController controller) {
accWrap = new list<DeleteExample>();
//Querying the Student records
Acclist = [select id,name,industry from account];
//Iterating through all the records of the student object
for(integer i =0;i<Acclist.size();i++){
//Creating a new Object of InnerClass
DeleteExample conObj = new DeleteExample(false,Acclist[i]);
accWrap.add(conObj);
}
}
//Method to delete the selected records
public pagereference deletecheckedRecs (){
//Iterating through the list
for(integer i=0;i<accWrap.size();i++){
//Fetching data of the selected records
if(accWrap[i].checkbox == true){
//deleting student records based on lists index
delete accWrap[i].accObj;
}
}
// create visualforce page with name deleteWrapperExample
pagereference ref = new pagereference(‘/apex/deleteWrapperExample’);
ref.setredirect(true);
return ref;
}
//Inner Class
public class DeleteExample{
public boolean checkbox{get;set;}
public account accObj{get;set;}
public DeleteExample(boolean checkbox,account acc)
{
checkbox = checkbox;
accObj = acc;
}
}
}
Visualforce page
<apex:page standardController=”Account” extensions=”AccountDelete”>
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value=”{!accWrap}” var=”acc”>
<apex:column headerValue=”select”>
<apex:inputCheckbox value=”{!acc.checkbox}”/>
</apex:column>
<apex:column headerValue=”id”>
<apex:outputField value=”{!acc.accObj.id}”/>
</apex:column>
<apex:column headerValue=”Name”>
<apex:outputField value=”{!acc.accObj.name}”/>
</apex:column>
<apex:column headerValue=”Industry”>
<apex:outputField value=”{!acc.accObj.Industry}”/>
</apex:column>
</apex:pageBlockTable>
<apex:pageBlockButtons >
<apex:commandButton value=”delete” action=”{! deletecheckedRecs}”/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>
Log In to reply.