ads

Wednesday, 12 April 2017

Wrapper Class : Delete and Update Selected Records Using Wrapper Class

In below example we will learn about how we can delete and Update selected records using wrapper Class.



Wrapper Class:



global Class Casecls{
    
    List<CaseclsInner> CaseInList = new List<CaseclsInner>();
    public Case CaseType{get;set;}
    
    global casecls(){
    CaseType = new Case();
    }
        
    global Class CaseclsInner{
        
        global Case CaseObj{get;set;}
        global Boolean checkboxObj{get;set;}
        
        global CaseclsInner(){
        }                
    }
    
    public List<CaseclsInner> getAllCaseRecords(){
                        
        for(Case c:[Select Id,CaseNumber,Type FROM Case]){
            CaseclsInner caseInobj = new CaseclsInner();
            caseInobj.CaseObj = c;
            CaseInList.add(caseInobj);            
        }        
        return CaseInList;        
    }
    
    public pagereference DeleteRecords(){
        
        List<Case> cList = new List<Case>();
        //Iterating through the list
        for(integer i=0;i<CaseInList.size();i++)
        {  
            if(CaseInList[i].checkboxObj == true)
            {
                //deleting student records based on lists index
                cList.add(CaseInList[i].CaseObj);
            }                        
        }
        
        delete cList;
            
        pagereference ref = new pagereference('/apex/CaseclsDelete');
        ref.setredirect(true);
        return ref;                
    }
    
    public pagereference ChangeType(){
        
        List<Case> cList = new List<Case>();
        //Iterating through the list
        for(integer i=0;i<CaseInList.size();i++)
        {  
            if(CaseInList[i].checkboxObj == true)
            {                
                CaseInList[i].CaseObj.Type = CaseType.type;                
                cList.add(CaseInList[i].CaseObj);
            }                        
        }
        
        update cList;
            
        pagereference ref = new pagereference('/apex/CaseclsDelete');
        ref.setredirect(true);
        return ref;                        
    }    
}


Visualforce Page:-



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<apex:page Controller="Casecls">
    <apex:form >
        <apex:pageBlock >
            <apex:pageblockSection >
                <apex:commandButton action="{!DeleteRecords}" value="Delete Selected Records"/><br></br>
                <apex:commandButton value="Change Type Of Selected Records" action="{!Changetype}" reRender="theTable"/>
                <apex:inputField value="{!CaseType.type}"/>
            </apex:pageblockSection>
            <apex:pageblockTable value="{!AllCaseRecords}" var="v">
                <apex:column headerValue="Checkbox">
                <apex:inputCheckbox value="{!v.checkboxobj}"/>
                </apex:column>
                <apex:column value="{!v.CaseObj.CaseNumber}"/>
                <apex:column value="{!v.CaseObj.Type}"/>
            </apex:pageblockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Output :-


No comments:

Post a Comment