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
Akshay AlandkarAkshay Alandkar 

need test class for this one ...

public class DelteContactController {
    public account acc {get; set;}
    public List<ContactInfoWrapper> ContactsWrapper {get; set;}
    public DelteContactController(ApexPages.StandardController controller)
    {}
    public List<ContactInfoWrapper> getAccObj(){
        Id id = apexpages.currentpage().getparameters().get('id');
         acc = [SELECT id from account where Id =:id];
        list<contact> conList=[SELECT Id,Account.Name,Name, Email FROM Contact where AccountId=:acc.id];
        ContactsWrapper = new List<ContactInfoWrapper>();
        for(Contact con:conList){
            ContactsWrapper.add(new ContactInfoWrapper(con,false));
        }
        return ContactsWrapper;
    }
    
    
    
    
    //Wrapper class
    public class ContactInfoWrapper
    {
        public Contact sObj{get;set;}
        public Boolean checked {get;set;}
        public ContactInfoWrapper(Contact con,boolean selectedBox)
        {
            sObj = con;
            checked=selectedBox;
        }
        
    }
   
    
    //Delete
    
    public pageReference deletecontact()
    {
        list<Contact> conlist = new list<Contact>();
        for(ContactInfoWrapper c : ContactsWrapper)
        {
            if(c.checked == true)
            {
                
                conlist.add(c.sObj);
            }
        }
        if(conlist.size()>0)
        {
           Delete conlist ;
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Contact Deleted Successfully !!!'));
        }
        pageReference pr = new pageReference('/lightning/o/Account/list?'+acc.id);
        return pr ;
        
        
    }
    
  

}
Best Answer chosen by Akshay Alandkar
Maharajan CMaharajan C
HI Akshay,

Please try the below test class:
 
@isTest
public class DelteContactControllerTest {
    static testmethod void testDelteContact(){

        // Add if there is any other fields are required for create Account
        Account acc = new Account(name = 'Test Account');
        insert acc;
        
        // Add if there is any other fields are required for create contact
        Contact con = new Contact(LastName = 'Test Con', Email = 'Test@test.com',AccountId = acc.Id);
        insert con;
        
        ApexPages.StandardController sc = new ApexPages.StandardController(acc);
        DelteContactController delta = new DelteContactController(sc);
        
        PageReference pageRef = Page.DeleteContact;
        pageRef.getParameters().put('id', acc.Id);
        Test.setCurrentPage(pageRef);
        
        delta.getAccObj();
        
        for(DelteContactController.ContactInfoWrapper conwrap : delta.ContactsWrapper){
            conwrap.checked = true;
        }
        
        delta.deletecontact();
    }
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Can you please post your VF Page also with page name...
Akshay AlandkarAkshay Alandkar
VF page name = DeleteContact
<apex:page id="SortPage" standardController="Account"  extensions="DelteContactController" showHeader="false">
    <apex:form id="myForm">
        
        <apex:sectionHeader title="Contact"/><br/>
        
        <apex:pageBlock >
            
            <apex:pageMessages id="showmsg"></apex:pageMessages>
            <apex:pageBlockButtons location="bottom" >
            <apex:CommandButton value="Delete" action="{!deletecontact}"  reRender="table,showmsg" />
            </apex:pageBlockButtons>
            
            <apex:pageBlockSection title="Contact Records" collapsible="false" columns="1"/>
            <br/>
            <body>                
                <table id="contacttable" class="display">       
                    <thead>
                        <tr>
                            <th style="width:10%;">Select</th>
                            <th style="width:25%;">Name</th>
                            <th style="width:30%;">Account Name</th>
                            <th style="width:25%;">Email Id</th>
                        </tr>
                    </thead>
                    <tbody>
                        <apex:repeat value="{!AccObj}" var="contactinfowrp" id="tableInd">
                            <apex:repeat value="{!contactinfowrp}" var="con">
                                <tr >
                                    <td><apex:inputCheckbox value="{!con.checked}"/></td>
                                    <td>{!con.sObj.Name}</td>
                                    <td>{!con.sObj.Account.Name}</td>
                                    <td>{!con.sObj.Email}</td>
                                </tr>
                            </apex:repeat>
                        </apex:repeat>
                    </tbody>
                </table>
            </body>
            
        </apex:pageBlock>  
    </apex:form>
</apex:page>
Maharajan CMaharajan C
HI Akshay,

Please try the below test class:
 
@isTest
public class DelteContactControllerTest {
    static testmethod void testDelteContact(){

        // Add if there is any other fields are required for create Account
        Account acc = new Account(name = 'Test Account');
        insert acc;
        
        // Add if there is any other fields are required for create contact
        Contact con = new Contact(LastName = 'Test Con', Email = 'Test@test.com',AccountId = acc.Id);
        insert con;
        
        ApexPages.StandardController sc = new ApexPages.StandardController(acc);
        DelteContactController delta = new DelteContactController(sc);
        
        PageReference pageRef = Page.DeleteContact;
        pageRef.getParameters().put('id', acc.Id);
        Test.setCurrentPage(pageRef);
        
        delta.getAccObj();
        
        for(DelteContactController.ContactInfoWrapper conwrap : delta.ContactsWrapper){
            conwrap.checked = true;
        }
        
        delta.deletecontact();
    }
}

Thanks,
Maharajan.C
This was selected as the best answer