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
Irvine DelacroixIrvine Delacroix 

Using checkbox to Delete Multiple Records

Hi Experts,

I'd like your guidance on how to use checkbox in visualforce to delete multiple records.

Below is how my visualforce look like. I want to be able to select multiple attachments then click on the "Delete Selected" button to delete the records that are selected.

User-added image

I'm stuck and I don't know how to achieve this.

I appreciate the help. Thanks much!


Here's my Code:

=====Page=====

<apex:page standardController="Contact" extensions="AttachmentController">
    <apex:form >
        <apex:pageBlock Title="Notes and Attachments">
            <!-- Buttons -->
            <apex:commandButton value="New Note"/>
            <apex:commandButton value="Attach File"/>
            <apex:commandButton value="Delete Selected"/>
            <apex:commandButton value="View All"/>
            <!-- Buttons -->
            <apex:pageBlockSection >
                <apex:pageBlockTable value="{!Attachments}" var="att">                    
                   <apex:column >
                       <apex:inputCheckbox />
                   </apex:column>
                   <apex:column value="{!att.id}"/>
                   <apex:column value="{!att.name}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
 

=====Class=====

 

public class AttachmentController {

    public AttachmentController(ApexPages.StandardController controller) {}

    string AttParentId = apexPages.currentPage().getParameters().get('id');
    
    public List<Attachment> getAttachments(){
        list<attachment> att = [select id, name, body from Attachment where parentId =:AttParentId ];
        return att;
    }
}
Best Answer chosen by Irvine Delacroix
Amit Chaudhary 8Amit Chaudhary 8
Please try Wrapper Class. That will help you
https://developer.salesforce.com/page/Wrapper_Class
Class
public class AttachmentController 
{

	public List<wAttachment> listWrappAtt {get; set;}
	
    public AttachmentController(ApexPages.StandardController controller) 
	{
		listWrappAtt = new List<wAttachment>();
		string AttParentId = apexPages.currentPage().getParameters().get('id');
		
        list<attachment> att = [select id, name, body from Attachment where parentId =:AttParentId ];
		
		for(attachment at : att)
		{
			listWrappAtt.add(new wAttachment(at));
		}
	}
	
	public PageReference processSelected() {
		
		List<Attachment> lstAttToDelete = new List<Attachment>();
		for(wAttachment wAtt: listWrappAtt) 
		{
			if(wAtt.selected  == true)
			{
				lstAttToDelete.add(wAtt.att);
			}
		}
		
		if(lstAttToDelete.size() > 0 )
		{
			Delete lstAttToDelete;
		}

		
	}
	
	public class wAttachment
	{
		public Attachment att {get; set;}
		public Boolean selected {get; set;}
		public wAttachment(Attachment a) 
		{
			att = a;
			selected = false;
		}
	}
}
Page
<apex:page standardController="Contact" extensions="AttachmentController">
    <apex:form >
        <apex:pageBlock Title="Notes and Attachments">
            <!-- Buttons -->
            <apex:commandButton value="New Note"/>
            <apex:commandButton value="Attach File"/>
            <apex:commandButton value="Delete Selected"/>
            <apex:commandButton value="View All"/>
            <!-- Buttons -->
            <apex:pageBlockSection >
                <apex:pageBlockTable value="{!listWrappAtt}" var="a">                    
                   <apex:column >
						<apex:inputCheckbox value="{!a.selected}"/>
                   </apex:column>
                   <apex:column value="{!a.att.id}"/>
                   <apex:column value="{!a.att.name}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Note:- code is not tested you may get come syntex error

 

All Answers

Amit Chaudhary 8Amit Chaudhary 8
Please try Wrapper Class. That will help you
https://developer.salesforce.com/page/Wrapper_Class
Class
public class AttachmentController 
{

	public List<wAttachment> listWrappAtt {get; set;}
	
    public AttachmentController(ApexPages.StandardController controller) 
	{
		listWrappAtt = new List<wAttachment>();
		string AttParentId = apexPages.currentPage().getParameters().get('id');
		
        list<attachment> att = [select id, name, body from Attachment where parentId =:AttParentId ];
		
		for(attachment at : att)
		{
			listWrappAtt.add(new wAttachment(at));
		}
	}
	
	public PageReference processSelected() {
		
		List<Attachment> lstAttToDelete = new List<Attachment>();
		for(wAttachment wAtt: listWrappAtt) 
		{
			if(wAtt.selected  == true)
			{
				lstAttToDelete.add(wAtt.att);
			}
		}
		
		if(lstAttToDelete.size() > 0 )
		{
			Delete lstAttToDelete;
		}

		
	}
	
	public class wAttachment
	{
		public Attachment att {get; set;}
		public Boolean selected {get; set;}
		public wAttachment(Attachment a) 
		{
			att = a;
			selected = false;
		}
	}
}
Page
<apex:page standardController="Contact" extensions="AttachmentController">
    <apex:form >
        <apex:pageBlock Title="Notes and Attachments">
            <!-- Buttons -->
            <apex:commandButton value="New Note"/>
            <apex:commandButton value="Attach File"/>
            <apex:commandButton value="Delete Selected"/>
            <apex:commandButton value="View All"/>
            <!-- Buttons -->
            <apex:pageBlockSection >
                <apex:pageBlockTable value="{!listWrappAtt}" var="a">                    
                   <apex:column >
						<apex:inputCheckbox value="{!a.selected}"/>
                   </apex:column>
                   <apex:column value="{!a.att.id}"/>
                   <apex:column value="{!a.att.name}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Note:- code is not tested you may get come syntex error

 
This was selected as the best answer
souvik9086souvik9086
Use Wrapper class to fix this. You can use wrapper class fields for checkbox and on selection of that box you can get the respective records to be deleted which you can perform in apex class.
Irvine DelacroixIrvine Delacroix
Thanks guys for the response.. Sorry for the daleyed reply. I was able to achieve what I want by using wrapper class. Thanks a lot!