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
Ramana123Ramana123 

Can anyone help me how to write test class for the below code.

I have four fields account,contact,phone and email on visualforce page. when I search the account name which exists in the data base then  those email , phone and contact should update to that account. If not found then create a new account with the following fields.

apex Class


public class CreatingandupdatingaccountcontactRecords {
    public string accountName {get;set;}
    public string contactName {get;set;}
    public string emailId     {get;set;} 
    public string phoneNumber {get;set;}   
    List<Account> accountList   = new List<Account>(); 
    List<Account> upsertAccount = new List<Account>();
    List<Contact> contactList   = new List<Contact>();
    List<Contact> upsertContact = new List<Contact>();
    public void checkAccountsandcontacts() {
        accountList = [select Name from Account where Name =:accountName];
        if(accountList.size() > 0) {
            for(Account  a : accountList ) {
                if(a.Name == accountName) {
                    a.Emai_id__c =emailId;
                    a.Phone = phoneNumber; 
                    upsertAccount.add(a);
                }
            }  
        }  
        if(accountList.size() < 0){ 
            Account newAccount = new Account();
            newAccount.Name = accountName;
            newAccount.Emai_id__c = emailId;
            newAccount.Phone = phoneNumber;
            upsertAccount.add(newAccount);
        } 
        upsert upsertAccount;
        contactList =[select LastName from contact where LastName =:contactName];
        if(contactList.size() > 0) {
            for(contact con : contactList) {
                if(con.LastName == contactName ) { 
                    con.Email = EmailId;  
                    con.Phone = phoneNumber;
                    for(Account a1 : accountList) {
                    con.AccountId = a1.Id;
                    }
                    upsertContact.add(con); 
                } 
            }
        }
        else{
            contact newcontact = new contact();
            newcontact.lastName = contactName;
            newcontact.phone = phonenumber;
            newcontact.Email = EmailId;
            for(Account acc:upsertAccount) {
                newcontact.AccountId = acc.Id;
            }
            upsertContact.add(newcontact);   
        }
        upsert upsertContact;  

        if(accountList.size() > 0){
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.info, 'Your record is updated successfully')); 
        }
        if(accountList.size() < 0){
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.info, 'Your record is created successfully'));  
        }
    }  
}

TestClass : 


@istest
public class contactAccountcreationpage_TC {
    public static testmethod void duplicateEmailerrormessage() {
        String accountName = 'testing account';
        Account acc = new Account();
        acc.Name = 'testing account';
        acc.Emai_id__c = 'dinesh@gmail.com';
        acc.Phone = '842631791';
        insert acc;
        
        Account acc1 = new Account();
        acc1.Name = 'testing account';
        acc1.Emai_id__c = 'dinesh@gmail.com';
        acc1.Phone = '842631791';
        insert acc1;
        
        Contact con = new contact();
        con.LastName = 'vini';
        con.AccountId = acc.Id;
        insert con;
        
        CreatingandupdatingaccountcontactRecords obj = new CreatingandupdatingaccountcontactRecords();
        obj.checkAccountsandcontacts();        
    }
}

 
Best Answer chosen by Ramana123
ravi soniravi soni
hi Ramana,
I did some  chages in your Main  Ctrl but logic are same. Replace your Ctrl with Following and copy & paste test class in your org.
//Main Class
public class CreatingandupdatingaccountcontactRecords {
    public string accountName {get;set;}
    public string contactName {get;set;}
    public string emailId     {get;set;} 
    public string phoneNumber {get;set;}   
    List<Account> accountList   = new List<Account>(); 
    List<Account> upsertAccount = new List<Account>();
    List<Contact> contactList   = new List<Contact>();
    List<Contact> upsertContact = new List<Contact>();
    public void checkAccountsandcontacts() {
        accountList = [select Name from Account where Name =:accountName];
        if(accountList.size() > 0) {
            for(Account  a : accountList ) {
                if(a.Name == accountName) {
                    a.Emai_id__c =emailId;
                    a.Phone = phoneNumber; 
                    upsertAccount.add(a);
                }
            }  
        }  
        else{ 
            Account newAccount = new Account();
            newAccount.Name = accountName;
            newAccount.Emai_id__c = emailId;
            newAccount.Phone = phoneNumber;
            upsertAccount.add(newAccount);
        } 
        upsert upsertAccount;
        contactList =[select LastName from contact where LastName =:contactName];
        if(contactList.size() > 0) {
            for(contact con : contactList) {
                if(con.LastName == contactName ) { 
                    con.Email = EmailId;  
                    con.Phone = phoneNumber;
                    for(Account a1 : accountList) {
                    con.AccountId = a1.Id;
                    }
                    upsertContact.add(con); 
                } 
            }
        }
        else{
            contact newcontact = new contact();
            newcontact.lastName = contactName;
            newcontact.phone = phonenumber;
            newcontact.Email = EmailId;
            for(Account acc:upsertAccount) {
                newcontact.AccountId = acc.Id;
            }
            upsertContact.add(newcontact);   
        }
        upsert upsertContact;  

        if(accountList.size() > 0){
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.info, 'Your record is updated successfully')); 
        }
        if(accountList.size() == 0){
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.info, 'Your record is created successfully'));  
        }
    }  
}
//Test Class
@istest
public class CreatingandupdatingaccountcontactRecordsTest {
    @istest
    public static void UnitTest() {
        Account Acc =New Account();
        Acc.Name = 'test';
        Acc.Phone = '9898989809';
         insert Acc;
         Contact Con =New Contact();
       Con.LastName = 'testCon';
        Con.Phone = Acc.Phone;
         insert Con;
       CreatingandupdatingaccountcontactRecords oCreatingandupdatingaccountcontactRecords = new CreatingandupdatingaccountcontactRecords();
        oCreatingandupdatingaccountcontactRecords.accountName = Acc.Name;
        oCreatingandupdatingaccountcontactRecords.contactName = Con.LastName;
        oCreatingandupdatingaccountcontactRecords.emailId = 'Test@test.com';
        oCreatingandupdatingaccountcontactRecords.phoneNumber =Acc.Phone;
        oCreatingandupdatingaccountcontactRecords.checkAccountsandcontacts();
    }
      @istest
    public static void UnitTest2() {
       
       CreatingandupdatingaccountcontactRecords oCreatingandupdatingaccountcontactRecords = new CreatingandupdatingaccountcontactRecords();
        oCreatingandupdatingaccountcontactRecords.accountName = 'test';
        oCreatingandupdatingaccountcontactRecords.contactName ='tescon';
        oCreatingandupdatingaccountcontactRecords.emailId = 'Test@test.com';
        oCreatingandupdatingaccountcontactRecords.phoneNumber ='09098798678';
        oCreatingandupdatingaccountcontactRecords.checkAccountsandcontacts();
    }
}
let me know if it helps you and marking it as best.
Thank You
 

All Answers

AnudeepAnudeep (Salesforce Developers) 
Please highlight the lines of code that are not covered
ravi soniravi soni
hi Ramana,
I did some  chages in your Main  Ctrl but logic are same. Replace your Ctrl with Following and copy & paste test class in your org.
//Main Class
public class CreatingandupdatingaccountcontactRecords {
    public string accountName {get;set;}
    public string contactName {get;set;}
    public string emailId     {get;set;} 
    public string phoneNumber {get;set;}   
    List<Account> accountList   = new List<Account>(); 
    List<Account> upsertAccount = new List<Account>();
    List<Contact> contactList   = new List<Contact>();
    List<Contact> upsertContact = new List<Contact>();
    public void checkAccountsandcontacts() {
        accountList = [select Name from Account where Name =:accountName];
        if(accountList.size() > 0) {
            for(Account  a : accountList ) {
                if(a.Name == accountName) {
                    a.Emai_id__c =emailId;
                    a.Phone = phoneNumber; 
                    upsertAccount.add(a);
                }
            }  
        }  
        else{ 
            Account newAccount = new Account();
            newAccount.Name = accountName;
            newAccount.Emai_id__c = emailId;
            newAccount.Phone = phoneNumber;
            upsertAccount.add(newAccount);
        } 
        upsert upsertAccount;
        contactList =[select LastName from contact where LastName =:contactName];
        if(contactList.size() > 0) {
            for(contact con : contactList) {
                if(con.LastName == contactName ) { 
                    con.Email = EmailId;  
                    con.Phone = phoneNumber;
                    for(Account a1 : accountList) {
                    con.AccountId = a1.Id;
                    }
                    upsertContact.add(con); 
                } 
            }
        }
        else{
            contact newcontact = new contact();
            newcontact.lastName = contactName;
            newcontact.phone = phonenumber;
            newcontact.Email = EmailId;
            for(Account acc:upsertAccount) {
                newcontact.AccountId = acc.Id;
            }
            upsertContact.add(newcontact);   
        }
        upsert upsertContact;  

        if(accountList.size() > 0){
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.info, 'Your record is updated successfully')); 
        }
        if(accountList.size() == 0){
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.info, 'Your record is created successfully'));  
        }
    }  
}
//Test Class
@istest
public class CreatingandupdatingaccountcontactRecordsTest {
    @istest
    public static void UnitTest() {
        Account Acc =New Account();
        Acc.Name = 'test';
        Acc.Phone = '9898989809';
         insert Acc;
         Contact Con =New Contact();
       Con.LastName = 'testCon';
        Con.Phone = Acc.Phone;
         insert Con;
       CreatingandupdatingaccountcontactRecords oCreatingandupdatingaccountcontactRecords = new CreatingandupdatingaccountcontactRecords();
        oCreatingandupdatingaccountcontactRecords.accountName = Acc.Name;
        oCreatingandupdatingaccountcontactRecords.contactName = Con.LastName;
        oCreatingandupdatingaccountcontactRecords.emailId = 'Test@test.com';
        oCreatingandupdatingaccountcontactRecords.phoneNumber =Acc.Phone;
        oCreatingandupdatingaccountcontactRecords.checkAccountsandcontacts();
    }
      @istest
    public static void UnitTest2() {
       
       CreatingandupdatingaccountcontactRecords oCreatingandupdatingaccountcontactRecords = new CreatingandupdatingaccountcontactRecords();
        oCreatingandupdatingaccountcontactRecords.accountName = 'test';
        oCreatingandupdatingaccountcontactRecords.contactName ='tescon';
        oCreatingandupdatingaccountcontactRecords.emailId = 'Test@test.com';
        oCreatingandupdatingaccountcontactRecords.phoneNumber ='09098798678';
        oCreatingandupdatingaccountcontactRecords.checkAccountsandcontacts();
    }
}
let me know if it helps you and marking it as best.
Thank You
 
This was selected as the best answer
Ramana123Ramana123

Hi Anudeep ,

The Highlight lines of code are not covered ,the Updation code is not covered how to cover It 
Thanks In Advance.



public class CreatingandupdatingaccountcontactRecords {
    public string accountName {get;set;}
    public string contactName {get;set;}
    public string emailId     {get;set;} 
    public string phoneNumber {get;set;}   
    List<Account> accountList   = new List<Account>(); 
    List<Account> upsertAccount = new List<Account>();
    List<Contact> contactList   = new List<Contact>();
    List<Contact> upsertContact = new List<Contact>();
    public void checkAccountsandcontacts() {
        accountList = [select Name from Account where Name =:accountName];
        if(accountList.size() > 0) {
            for(Account  a : accountList ) {
                if(a.Name == accountName) {
                    a.Emai_id__c =emailId;
                    a.Phone = phoneNumber; 
                    upsertAccount.add(a);
                }
            }  

        }  
        if(accountList.size() < 0){ 
            Account newAccount = new Account();
            newAccount.Name = accountName;
            newAccount.Emai_id__c = emailId;
            newAccount.Phone = phoneNumber;
            upsertAccount.add(newAccount);
        } 
        upsert upsertAccount;
        contactList =[select LastName from contact where LastName =:contactName];
        if(contactList.size() > 0) {
            for(contact con : contactList) {
                if(con.LastName == contactName ) { 
                    con.Email = EmailId;  
                    con.Phone = phoneNumber;
                    for(Account a1 : accountList) {
                    con.AccountId = a1.Id;
                    }
                    upsertContact.add(con); 
                } 

            }
        }
        else{
            contact newcontact = new contact();
            newcontact.lastName = contactName;
            newcontact.phone = phonenumber;
            newcontact.Email = EmailId;
            for(Account acc:upsertAccount) {
                newcontact.AccountId = acc.Id;
            }
            upsertContact.add(newcontact);   
        }
        upsert upsertContact;  

        if(accountList.size() > 0){
        ApexPages.addmessage(new ApexPages.message(ApexPages.severity.info, 'Your record is updated successfully')); 
        }
        if(accountList.size() < 0){
          ApexPages.addmessage(new ApexPages.message(ApexPages.severity.info, 'Your record is created successfully'));  
        }
    }  
}