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
Teran@18Teran@18 

account

write test class to this

public with sharing class ContactSearchController{
    public string AccountName{get;set;}
    public list<Contact> lstContacts{get;set;}
    public ContactSearchController(ApexPages.StandardController controller) {
        lstContacts = new list<Contact>();
    }
    public void SearchContacts(){
        lstContacts = new list<Contact>();
        if(AccountName!=null && AccountName!=''){
            for(Contact con:[Select Id,FirstName,LastName,Email,(Select Id,UserName from Users where IsActive=true limit 1) from Contact where Account.Name=:AccountName])
            {
                lstContacts.add(con);
            }
        }
    }
    
    
    
}
Maharajan CMaharajan C
Hi Teran,

Use the below to get the 100% ccoverage:

If any required fields in contact and Account missed then pls add in the below code:

@isTest
public class ContactSearchControllerTest {

    static testmethod void ContactSearchControllerTestone()
    {
        
        contact c = new contact(LastName = 'Test');
        ContactSearchController obj = new ContactSearchController(new ApexPages.StandardController(c));
        
        Account acc = new Account(Name = 'Test Acc');           // Add if any other required fields are there 
        insert acc;
        
        List<Contact> conList = new List<contact>();
        contact c1 = new contact(LastName = 'Test c1',AccountId = acc.Id);     // Add if any other required fields are there 
        contact c2 = new contact(LastName = 'Test c2',AccountId = acc.Id);      // Add if any other required fields are there 
        conList.add(c1);
        conList.add(c2);
        
        insert conList;
        
        obj.AccountName = 'Test Acc';
        obj.SearchContacts();
        
    }
}

Can you please Let me know if it helps or not!!!

If it helps don't forget to mark this as a best answer!!!


Thanks,
Maharajan.C