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
SFDC12SFDC12 

wrapperclass account related contacts

Hi everyone,can someone help how to write testclass for the below code..
Thanks in Advance.

public class Displayrelatedlists {
    //1.list of wrapperclass
     public list<AccountContactWrapper> lstAccountContactWrapper { get; set; }
    public list<AccountContactWrapper> lstSelectedAccountContactWrapper { get; set; }
    //list for selecting the account records
    public list<account> selectedAccounts{get;set;} 

    public Displayrelatedlists () {
        //Allocating the memory for the list
        lstSelectedAccountContactWrapper = new list<AccountContactWrapper>();
        if(lstAccountContactWrapper == null) {
            //allocate memory for the wrapperlist
            lstAccountContactWrapper = new list<AccountContactWrapper>();
            for(account a:[select id,name,phone,(select id,name from contacts)from account limit 10]) {
                lstAccountContactWrapper.add(new AccountContactWrapper(a));
            }
        }
    }
 
    public void ProcessSelected() {
        lstSelectedAccountContactWrapper =new list<AccountContactWrapper>();
        selectedAccounts =new list<Account>();
        for(AccountContactWrapper wrapobj:lstAccountContactWrapper){
            if(wrapobj.isSelected==true) {
                selectedAccounts.add(wrapobj.acc);
            } 
        }
       
        for(Account acc:[select id,name,phone,(select id,name from contacts) from account where id in:selectedAccounts]) {
            lstSelectedAccountContactWrapper.add(new AccountContactWrapper(acc)); 
        }
             
    }
    public class AccountContactWrapper {
 
        public Account acc {get;set;}
        public boolean isSelected {get;set;}
   
        public AccountContactWrapper(account a) {
            acc = a;
            isselected=false;
        }
    }
}
Best Answer chosen by SFDC12
Maharajan CMaharajan C
Hi Anusha,

Please try the below test class:
 
@isTest
public class DisplayrelatedlistsTest {
    @isTest static void testDisplayrelatedlistsMethod(){

        // Add if there is any other fields are required for Account Create
        Account acc = new Account(Name = 'Test Account');
        insert acc;
        
        // Add if there is any other fields are required for Account Create
        List<Contact> conlist = new List<Contact>();
        conlist.add(new contact(lastname = 'Test Con', accountId = acc.Id));
        conlist.add(new contact(lastname = 'Test Con1', accountId = acc.Id));
        
        insert conlist;
        
        Displayrelatedlists disp = new Displayrelatedlists();
        
        for(Displayrelatedlists.AccountContactWrapper a : disp.lstAccountContactWrapper){
            a.isSelected = true;
        }
        
        Test.startTest();
        disp.ProcessSelected();
        Test.stopTest();
        
    }
}

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Anusha,

Please try the below test class:
 
@isTest
public class DisplayrelatedlistsTest {
    @isTest static void testDisplayrelatedlistsMethod(){

        // Add if there is any other fields are required for Account Create
        Account acc = new Account(Name = 'Test Account');
        insert acc;
        
        // Add if there is any other fields are required for Account Create
        List<Contact> conlist = new List<Contact>();
        conlist.add(new contact(lastname = 'Test Con', accountId = acc.Id));
        conlist.add(new contact(lastname = 'Test Con1', accountId = acc.Id));
        
        insert conlist;
        
        Displayrelatedlists disp = new Displayrelatedlists();
        
        for(Displayrelatedlists.AccountContactWrapper a : disp.lstAccountContactWrapper){
            a.isSelected = true;
        }
        
        Test.startTest();
        disp.ProcessSelected();
        Test.stopTest();
        
    }
}

Thanks,
Maharajan.C
This was selected as the best answer
SFDC12SFDC12
Thanks a lot maharajan.
pkin pumpkin pum
My new addiction, I really love it. slope unblocked (https://slopeunblocked.online) - 1v1 lol (https://1v1lol.online
taj dekhrataj dekhra
Looks like it doesn't count as DML for the purposes of governor limits, just for CalloutExceptions waveofnation. (https://waveofnation.com/https://waveofnation.com/
Alex RiderrAlex Riderr
Amazing, thanks for sharing. https://synapsex.co/ https://sonicexe.me/
John MarkosJohn Markos
Thanks for sharing this amazing info. https://btroblox.net/ https://tgmacro.net/