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
SFDC16SFDC16 

Delete specific record from wrapper class on selected checkbox

I want to delete a specific record (row )from wrapper class on click checkbox. By using a wrapper class I displayed the record on vf page along with a checkbox 

On click checkbox I want delete the record
Sampath SuranjiSampath Suranji
Hi,

When user checked, you can call apex:actionFunction (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_actionFunction.htm) and pass the record Id. then you can delete the record inside the apex class.

regards
Sampath
Akshay_DhimanAkshay_Dhiman
Hi SFDC16,
 
<apex:page controller="AccountSelectClassController" sidebar="false">
    <script type="text/javascript">
        function selectAllCheckboxes(obj,receivedInputID){
            var inputCheckBox = document.getElementsByTagName("input");
            for(var i=0; i<inputCheckBox.length; i++){
                if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
                    inputCheckBox[i].checked = obj.checked;
                }
            }
        }
    </script>
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Delete" action="{!processSelected}" rerender="table"/>

            </apex:pageBlockButtons>
 
            <apex:pageblockSection title="All Accounts" collapsible="false">
 
                <apex:pageBlockTable value="{!wrapAccountList}" var="accWrap" id="table" title="All Accounts">
                    <apex:column >
                        <apex:facet name="header">
                            <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                        </apex:facet>
                        <apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/>
                    </apex:column>
                    <apex:column value="{!accWrap.acc.Name}" />
                    <apex:column value="{!accWrap.acc.BillingState}" />
                    <apex:column value="{!accWrap.acc.Phone}" />
                </apex:pageBlockTable>
                
            </apex:pageblockSection>
        </apex:pageBlock>
    </apex:form>
 
</apex:page>

public class AccountSelectClassController{
 
    //Our collection of the class/wrapper objects wrapAccount
    public List<wrapAccount> wrapAccountList {get; set;}
    public List<Account> selectedAccounts{get;set;}
 
    public AccountSelectClassController(){
        if(wrapAccountList == null) {
            wrapAccountList = new List<wrapAccount>();
            for(Account a: [select Id, Name,BillingState, Website, Phone from Account limit 10]) {
                // As each Account is processed we create a new wrapAccount object and add it to the wrapAccountList
                wrapAccountList.add(new wrapAccount(a));
            }
        }
    }
 
    public void processSelected() {
    selectedAccounts = new List<Account>();
    List<wrapAccount > wrapAccountListAfterDel = new List<wrapAccount >();
        for(wrapAccount wrapAccountObj : wrapAccountList) {
            if(wrapAccountObj.selected == true) {
                selectedAccounts.add(wrapAccountObj.acc);
            }else{
            wrapAccountListAfterDel.add(wrapAccountObj);
            
            }
        }
        delete selectedAccounts;
        wrapAccountList = wrapAccountListAfterDel;       
    }
 
    // This is our wrapper/container class. In this example a wrapper class contains both the standard salesforce object Account and a Boolean value
    public class wrapAccount {
        public Account acc {get; set;}
        public Boolean selected {get; set;}
 
        public wrapAccount(Account a) {
            acc = a;
            selected = false;
        }
    }
}


if you found this answer helpful then please mark it as best answer so it can help others.   

Thanks 
Akshay
Yogeshwar TailorYogeshwar Tailor
Hi,

when you will click delete record button call the below method.
public void deleteRecord(){
    // here i am assuming your wrapper classname is WrapperList and you have created the wrapperlist wrr
    // I am assuming you are showing account record which you want to delete
    Set<Account> st = new Set<Account>();
    for(WrapperList wp: wrr ){
        // i am assuming for checkbox you took boolean variable bool and Account variable acc in your wrapper
        if(wp.bool == true){
            st.add(wp.acc);
        }
    }
    delete st;
}

You have to manipulate you wrapper also here so the deleted records will flush from the list.

Thanks,
Yogesh
SFDC16SFDC16
I have displayed multiple object record on vf page with different page block table. Now I want to delete specific from the table on a selected checkbox.
Yogeshwar TailorYogeshwar Tailor
Hi @SFDC16

You can delete records from id's also. 
Better can you share your controller code so i can help you more.
public void deleteRecord(){
    // here i am assuming your wrapper classname is WrapperList and you have created the wrapperlist wrr
    // I am assuming you are showing account record which you want to delete
    List<Id> st = new List<Id>();
    for(WrapperList wp: wrr ){
        // i am assuming for checkbox you took boolean variable bool and Account variable acc in your wrapper
        if(wp.bool == true){
             //here add the record id's into set
            //st.add(wp.acc);
        }
    }
    Database.delete(st);
}

Thanks,
Yogesh
SFDC16SFDC16
Below is code please check it. public class ServiceCloudHomePage { public Product2 getAll{get;set;}/*-----------------------used for update value on modal---------------------*/ List allProducts=new List(); List allCase =new List(); List allobproblem=new List(); List lstw=new List(); public List getlstWrapperStrtng() { allProducts=[select id,IsActive,Name,ProductCode ,Battery_Type__c,Product_Image__c , Purchase_Date__c,Warranty__c ,Description from Product2 where Battery_Type__c !='Radio Battery' limit 5]; allCase=[select id, Owner.Name,CaseNumber,Description,Product_Code__c,Reason,Status,Origin,CreatedDate From Case limit 5 ]; allobproblem=[select id,Name,Owner.Name, Status__C,Battery_Number__c,Within_Warranty__c,Observed_Problem__c,Description__c,Case__r.CaseNumber from Observance_Managment__c limit 5]; for(Integer i=0; i st = new List(); for(wrapper wp: lstw) { if(wp.isSelected== true) { st.add(wp.aa); } } delete st; } public class wrapper { public boolean isSelected{get;set;} public product2 aa{get;set;} /*---------------------------------Product Fields------------------------*/ public string prName{get;set;} public boolean prIsActive{get;set;} public string prProductCode{get;set;} public string prBatteryType{get;set;} public string prProductImage{get;set;} public Date prPurchaseDate{get;set;} public string prDescription{get;set;} public String prWarranty{get;set;} public String caCaseNumber{get;set;} public String caProductCode{get;set;} public String caReason{get;set;} public String caOrigin{get;set;} public String caStatus{get;set;} public String caDescription{get;set;} public Datetime caCreatedDate{get;set;} public String caOwnerName{get;set;} public String pbName{get;set;} public String pbStatus{get;set;} public String pbOwnerName{get;set;} public String pbBatteryName{get;set;} public String pbwithinwarranty{get;set;} public String pbObservedProblem{get;set;} public String pbDescription{get;set;} public String relatedCaseNumber{get;set;} public wrapper (String prName,boolean prIsActive,string prProductCode,string prBatteryType,string prProductImage,Date prPurchaseDate,string prDescription, String prWarranty, String caCaseNumber, String caDescription,String caProductCode,String caOwnerName,String caReason,String caStatus, String caOrigin, Datetime caCreatedDate,String pbName,String pbStatus,String pbOwnerName,String pbBatteryName,String pbwithinwarranty,String pbObservedProblem,String pbDescription,String relatedCaseNumber) { isSelected=false; this.prName=prName; this.prIsActive=prIsActive; this.prProductCode=prProductCode; this.prBatteryType=prBatteryType; this.prProductImage=prProductImage; this.prPurchaseDate=prPurchaseDate; this.prDescription=prDescription; this.prWarranty=prWarranty; this.caCaseNumber=caCaseNumber; this.caDescription=caDescription; this.caProductCode=caProductCode; this.caOwnerName=caOwnerName; this.caReason=caReason; this.caStatus=caStatus; this.caOrigin=caOrigin; this.caCreatedDate=caCreatedDate; this.pbName=pbName; this.pbStatus=pbStatus; this.pbOwnerName=pbOwnerName; this.pbBatteryName=pbBatteryName; this.pbwithinwarranty=pbwithinwarranty; this.pbObservedProblem=pbObservedProblem; this.pbDescription=pbDescription; this.relatedCaseNumber=relatedCaseNumber; } } public void spin() { long now = datetime.now().gettime(); while(datetime.now().gettime()-now
Yogeshwar TailorYogeshwar Tailor
Hi SFDC16,

Try this code...
 
public class ServiceCloudHomePage {
    public Product2 getAll {get;set;} /*-----------------------used for update value on modal---------------------*/
    List allProducts = new List();
    List allCase = new List();
    List allobproblem = new List();
    List lstw = new List();

    public List getlstWrapperStrtng() {
            allProducts = [select id, IsActive, Name, ProductCode, Battery_Type__c, Product_Image__c, Purchase_Date__c, Warranty__c, Description from Product2 where Battery_Type__c != 'Radio Battery' limit 5];
            allCase = [select id, Owner.Name, CaseNumber, Description, Product_Code__c, Reason, Status, Origin, CreatedDate From Case limit 5];
            allobproblem = [select id, Name, Owner.Name, Status__c, Battery_Number__c, Within_Warranty__c, Observed_Problem__c, Description__c, Case__r.CaseNumber from Observance_Managment__c limit 5];
            //add these records in wrapper list
      
    }

	public void deleteRecord(){
       
        List<Id> st = new List<Id>();
        for(WrapperList wp: wrr ){
            
            if(wp.isSelected == true){
                st.add(wp.recIds);
            }
        }
        Database.delete(st);
      
        //flush records from wrapper also
      
    }	



            public class wrapper {
              	
                public boolean isSelected {get;set;}
				public Id recIds{get;set;}
                public product2 aa {get;set;} 
                public string prName {get;set;}
                public boolean prIsActive {get;set;}
                public string prProductCode {get;set;}
                public string prBatteryType {get;set;}
                public string prProductImage {get;set;}
                public Date prPurchaseDate {get;set;}
                public string prDescription {get;set;}
                public String prWarranty {get;set;}
                public String caCaseNumber {get;set;}
                public String caProductCode {get;set;}
                public String caReason {get;set;}
                public String caOrigin {get;set;}
                public String caStatus {get;set;}
                public String caDescription {get;set;}
                public Datetime caCreatedDate {get;set;}
                public String caOwnerName {get;set;}
                public String pbName {get;set;}
                public String pbStatus {get;set;}
                public String pbOwnerName {get;set;}
                public String pbBatteryName {get;set;}
                public String pbwithinwarranty {get;set;}
                public String pbObservedProblem {get;set;}
                public String pbDescription {get;set;}
                public String relatedCaseNumber {get;set;}
                public wrapper(Id redId,String prName, boolean prIsActive, string prProductCode, string prBatteryType, string prProductImage, Date prPurchaseDate, string prDescription, String prWarranty, String caCaseNumber, String caDescription, String caProductCode, String caOwnerName, String caReason, String caStatus, String caOrigin, Datetime caCreatedDate, String pbName, String pbStatus, String pbOwnerName, String pbBatteryName, String pbwithinwarranty, String pbObservedProblem, String pbDescription, String relatedCaseNumber) {
                    this.recIds = recId;
                    isSelected = false;
                    this.prName = prName;
                    this.prIsActive = prIsActive;
                    this.prProductCode = prProductCode;
                    this.prBatteryType = prBatteryType;
                    this.prProductImage = prProductImage;
                    this.prPurchaseDate = prPurchaseDate;
                    this.prDescription = prDescription;
                    this.prWarranty = prWarranty;
                    this.caCaseNumber = caCaseNumber;
                    this.caDescription = caDescription;
                    this.caProductCode = caProductCode;
                    this.caOwnerName = caOwnerName;
                    this.caReason = caReason;
                    this.caStatus = caStatus;
                    this.caOrigin = caOrigin;
                    this.caCreatedDate = caCreatedDate;
                    this.pbName = pbName;
                    this.pbStatus = pbStatus;
                    this.pbOwnerName = pbOwnerName;
                    this.pbBatteryName = pbBatteryName;
                    this.pbwithinwarranty = pbwithinwarranty;
                    this.pbObservedProblem = pbObservedProblem;
                    this.pbDescription = pbDescription;
                    this.relatedCaseNumber = relatedCaseNumber;
                }
            }
           
please let me know if you have any problem.

Thanks,
Yogesh
 
SFDC16SFDC16
Hi Yogesh , It's Not working,
Yogeshwar TailorYogeshwar Tailor
Hi SFDC16,

Basically here i am not able to see in your code where you are adding the records in wrapper?
 
for(Integer i=0; i st = new List(); for(wrapper wp: lstw) { if(wp.isSelected== true) { st.add(wp.aa); } } delete st; }

here in above code i only gave you hint that take on variable type ID in your wrapper. 

and when you will select the checkboxes you call the deleteRecord method. 

The method will check for which record the the checkbox is true and it will add that record id to list for deleteing them.

Thanks,