+ Start a Discussion
Luffy Carl_opLuffy Carl_op 

Hello to Batch Remove records from page.

There are a list in my page, whose records is coming from object, quering by apex class. 
How can I batch remove records from this list?
 
Best Answer chosen by James Loghry
Rajiv Bhatt 16Rajiv Bhatt 16
What you need here is a Wrapper Class. This is explained very well on this post. https://developer.salesforce.com/page/Wrapper_Class Please go through it very carefully, it addresses exactly the problem that you are facing and has the solution. Create a wrapper class ( how they have created cContact in the example) , you would need a similar class. In your case the members would be of type WorkTime__C instead of Contact. Please go through it very carefully, understand it and them give an attempt by updating your controller and vf page. Let us know if you still face any issues. 

All Answers

Rajiv Bhatt 16Rajiv Bhatt 16
can you share some snashots, object names so that we can clearly understand the issue? Help us to help you.
Luffy Carl_opLuffy Carl_op
User-added image
There is a object named WorkTime(WorkTime__c),with fields named loginTime(BeginTime__c),LogoutTime(EndTime__c), I want user the checkbox to Batch Remove Records, (there is a button named "DELETE SELECT RECORDS", but I don't konw how to get this records in class, which choosed by me in page.
Can you help me?
Rajiv Bhatt 16Rajiv Bhatt 16
Please share the VF Page and the controller code as well so that we can share the precise solution.
Luffy Carl_opLuffy Carl_op
<apex:page controller="AdminPageCtrl" tabStyle="WorkTime__c">

<apex:form >
        
        Subordinates:<apex:selectList multiselect="false" value="{!selectedsubordinate}" size="1" style="width:230px">
                <apex:selectOptions value="{!optionSubordinate}"></apex:selectOptions>
              </apex:selectList>
              
        <apex:commandButton value="Show All Reports" action="{!showAllWorks}">
            <apex:param name="button1" value="Button 1" />
        </apex:commandButton>    
        
        <apex:actionStatus id="loadingStatus" startText="Please Wait. Processing..." />
        
        <apex:pageBlockButtons >
            
            <apex:commandButton value="Delete The Selected Records"/>
           
        </apex:pageBlockButtons>

        <apex:pageBlockTable value="{!relist}" var="rec" id="myTable">
  /*I want to add a checkbox, some records are selected will send to class,by the button "Delete The Selected Records"  .But I don't know how to write this code: 
           <apex:column>
                <apex:inputCheckbox >
                    <apex:param value="{!rec.Name}" name="muRecId" />  
                </apex:inputCheckbox>
            </apex:column>
  */          
            <apex:column headerValue="ID" value="{!rec.Name}" /> 
            <apex:column headerValue="LoginTime" value="{!rec.BeginTime__c}" /> 
            <apex:column headerValue="LogoutTime" value="{!rec.EndTime__c}" /> 
            <apex:column headerValue="Notes"> 
                <apex:inputText value="{!rec.Notes__c}" />
            </apex:column>
            <apex:column headerValue="Update">
                <apex:commandLink action="{!saveSingleRecord}" rerender="thePageBlock" status="loadingStatus">
                    Update
                    <apex:param value="{!rec.Notes__c}" name="myRecId" />  
                </apex:commandLink>
            </apex:column>
            <apex:column headerValue="Delete">
                <apex:commandLink action="{!deleteRecord}" rerender="myTable" status="loadingStatus">
                    Delete
                    <apex:param value="{!rec.Name}" name="deRecId" />  
                </apex:commandLink>
            </apex:column>
        </apex:pageblocktable>        
    </apex:pageBlock>
</apex:form>
</apex:page>

 This is my Page. 
public with sharing class AdminPageCtrl{
    
    public List<User> userlist {get; set;}
    public List<SelectOption> optionSubordinate {get; set;}
    public String selectedsubordinate {get; set;}
    
    public List<WorkTime__C> relist {get; set;}
    
    public AdminPageCtrl(){
        relist = new List<WOrkTime__C>();
        
        
        optionSubordinate = new List<SelectOption>();
            List<User> userlist = [SELECT Name, MyPosition__c From user
                        Where MyPosition__c like 'Department%' 
                            or MyPosition__c like 'Admin%' or MyPosition__c like 'Bos%'
                        Order By MyPosition__c ASC
                       ];
            if(userlist != null && !userlist.isEmpty()){
                    for(user me : userlist){
                        optionSubordinate.add(new SelectOption(me.Name, me.Name + ' - ' + me.MyPosition__c));
                    }
            }
            
    }
    
    public pageReference showAllWorks(){
        
        relist = [select  Name, BeginTime__c, WorkerRef__r.Name, EndTime__c, Notes__c from WorkTime__c 
            where WorkerRef__r.Name = :selectedsubordinate
            Order by BeginTime__c DESC
            ];
        
        if (relist.isEmpty()) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'This Employee has not ATTENDANCE INFORMATION.'));
            return null;
        }   
        return null;     
    }
    
    public void saveSingleRecord(){
        string recId = ApexPages.currentpage().getParameters().get('myRecId');
        WorkTime__c mySingleRec;
        for (WorkTime__c c : relist) {
            if (recId == c.Notes__c) {
                mySingleRec = c;
                break;
            }
        }
        if (mySingleRec != null)
            update mySingleRec;
    }
    
    public pageReference deleteRecord(){
        string recId = ApexPages.currentpage().getParameters().get('deRecId');
        for (WorkTime__c c : relist) {
            if (c.Name == recId)
                delete c;
        }
        return showAllWorks();
    }
    
}

I got the problems for a long time. Please help me. How to delete multi-records in the page's list.
Rajiv Bhatt 16Rajiv Bhatt 16
What you need here is a Wrapper Class. This is explained very well on this post. https://developer.salesforce.com/page/Wrapper_Class Please go through it very carefully, it addresses exactly the problem that you are facing and has the solution. Create a wrapper class ( how they have created cContact in the example) , you would need a similar class. In your case the members would be of type WorkTime__C instead of Contact. Please go through it very carefully, understand it and them give an attempt by updating your controller and vf page. Let us know if you still face any issues. 
This was selected as the best answer
Luffy Carl_opLuffy Carl_op
I Got it.And it works perfectly. I believe that i already have a deep understanding in this wrapper.Thank you.
Rajiv Bhatt 16