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
kROOMkROOM 

I am new to coding and need help in making a test class. Hopefully you can also teach me how to use test codes using TestDataBuilder Class. Instructions are written per code. Thank You

TRIGGER:

trigger ContactTrigger on Contact (before insert,before update) {
    if(Trigger.isBefore){
        if(Trigger.isInsert){
         ContactTriggerHandler.onBeforeInsert(Trigger.New);
        }
        if(Trigger.isUpdate){
            ContactTriggerHandler.onBeforeUpdate(Trigger.New);
        }
    }
}

CLASS:

public class ContactTriggerHandler {
    
    public static void onBeforeInsert(List<Contact> contactList){
        setPrimaryContact(contactList);
    }
    
    public static void onBeforeUpdate(List<Contact> contactList){
        updatePrimaryContact(contactList);
    }
    
    /* Instruction Number 8
     * When an existing Non-Primary Contact is updated to become a Primary Contact,  the system should check if the Account related to the updated Contact does not yet have a Primary Contact. If the Account already has a Primary Contact, a message should be shown to the user stating: "Invalid Primary Contact. This Account has an existing Primary Contact."
     * 
    */
   
    public static void updatePrimaryContact(List<Contact> contactList){
        List<Account> accountList = [SELECT Id, (SELECT Id, Primary_Contact__c FROM Contacts) FROM Account];
        Map<Id, Account> accountMap = new Map<Id, Account>();
        
        for(Account acct : accountList)    {
            accountMap.put(acct.Id, acct);
        }
        
        for(Contact contactNonPrime : contactList){
            for(Contact cc : accountMap.get(contactNonPrime.AccountId).Contacts){
                if(contactNonPrime.Primary_Contact__c == TRUE){
                    if(cc.Primary_Contact__c == TRUE && contactNonPrime.Id != cc.Id){
                        contactNonPrime.Primary_Contact__c.addError('Invalid Primary Contact. This Account has an existing Primary Contact.');
                    }
                }
            }
        }
    }
    
  /* Instruction Number 6
   * When a new Non-Primary Contact is created and the Account related to the new Contact does not have any existing Contacts, the new Contact should automatically become the Account's Primary Contact.
   */
        
    public static void setPrimaryContact(List<Contact> contactList){
        List<Account> accountList = [SELECT Id, (SELECT Id, Primary_Contact__c FROM Contacts)
                                     FROM Account];
        Map<Id, Account> accountMap = new Map<Id, Account>();
        
        for(Account acct : accountList)    {
            accountMap.put(acct.Id, acct);
        }
            
            for(Contact contactPrime : contactList){
                if(contactPrime.Primary_Contact__c != true && contactPrime.AccountId != NULL){
                   
                    if(accountMap.get(contactPrime.AccountId).Contacts.size() <1){
                        contactPrime.Primary_Contact__c = TRUE;
                    }
                }else if(contactPrime.Primary_Contact__c == TRUE && contactPrime.AccountId != NULL) {
                    for(Contact cc : accountMap.get(contactPrime.AccountId).Contacts){
                        if(cc.Primary_contact__c == TRUE){
                              contactPrime.Primary_Contact__c.addError('Invalid Primary Contact. This Account has an existing Primary Contact.');
                        }
                  }
             } 
        }
    }
}
Best Answer chosen by kROOM
sridhar v 7sridhar v 7
Hi, 

Try the below Test Class. it should cover both the trigger as well as the handler class.


@isTest
private class ContactTriggerTest {

    static testMethod void ContactTriggerHandlerTest() {
        //insert Account
        Account objAccount = new Account();
        objAccount.name = 'test Account';
        insert objAccount;

        List < Contact > lstContacts = new List < Contact > ();
        //Insert a Primary Contact and a non Primary Contact.        
        Contact objPrimaryContact = new Contact();
        objPrimaryContact.accountId = objAccount.id;
        objPrimaryContact.lastName = 'lastName';
        objPrimaryContact.firstName = 'firstName';
        objPrimaryContact.Primary_Contact__c = true;
        lstContacts.add(objPrimaryContact);

        //Non Primary Contact.
        Contact objNonPrimaryContact = new Contact();
        objNonPrimaryContact.accountId = objAccount.id;
        objNonPrimaryContact.lastName = 'lastName';
        objNonPrimaryContact.firstName = 'firstName';
        objNonPrimaryContact.Primary_Contact__c = false;
        lstContacts.add(objNonPrimaryContact);

        insert lstContacts;

        //update non primary contact on account which already has a primary contact.
        objNonPrimaryContact.Primary_Contact__c = true;

        //catch the exception when another primary contact is added to account which has a Primary Contact.
        try {
            update objNonPrimaryContact;
        } catch (Exception ex) {
            system.assert(ex.getMessage().containsIgnoreCase('Invalid Primary Contact. This Account has an existing Primary Contact.'));
        }

        //insert Account
        Account objAccount2 = new Account();
        objAccount2.name = 'test Account2';
        insert objAccount2;

        //inserting a Non Primary Contact on Account which do not have any contacts.
        Contact objNonPrimaryContact2 = new Contact();
        objNonPrimaryContact2.accountId = objAccount.id;
        objNonPrimaryContact2.lastName = 'lastName';
        objNonPrimaryContact2.firstName = 'firstName';
        objNonPrimaryContact2.Primary_Contact__c = false;
        insert objNonPrimaryContact2;
    }
}

Thanks,

All Answers

sridhar v 7sridhar v 7
Hi, 

Try the below Test Class. it should cover both the trigger as well as the handler class.


@isTest
private class ContactTriggerTest {

    static testMethod void ContactTriggerHandlerTest() {
        //insert Account
        Account objAccount = new Account();
        objAccount.name = 'test Account';
        insert objAccount;

        List < Contact > lstContacts = new List < Contact > ();
        //Insert a Primary Contact and a non Primary Contact.        
        Contact objPrimaryContact = new Contact();
        objPrimaryContact.accountId = objAccount.id;
        objPrimaryContact.lastName = 'lastName';
        objPrimaryContact.firstName = 'firstName';
        objPrimaryContact.Primary_Contact__c = true;
        lstContacts.add(objPrimaryContact);

        //Non Primary Contact.
        Contact objNonPrimaryContact = new Contact();
        objNonPrimaryContact.accountId = objAccount.id;
        objNonPrimaryContact.lastName = 'lastName';
        objNonPrimaryContact.firstName = 'firstName';
        objNonPrimaryContact.Primary_Contact__c = false;
        lstContacts.add(objNonPrimaryContact);

        insert lstContacts;

        //update non primary contact on account which already has a primary contact.
        objNonPrimaryContact.Primary_Contact__c = true;

        //catch the exception when another primary contact is added to account which has a Primary Contact.
        try {
            update objNonPrimaryContact;
        } catch (Exception ex) {
            system.assert(ex.getMessage().containsIgnoreCase('Invalid Primary Contact. This Account has an existing Primary Contact.'));
        }

        //insert Account
        Account objAccount2 = new Account();
        objAccount2.name = 'test Account2';
        insert objAccount2;

        //inserting a Non Primary Contact on Account which do not have any contacts.
        Contact objNonPrimaryContact2 = new Contact();
        objNonPrimaryContact2.accountId = objAccount.id;
        objNonPrimaryContact2.lastName = 'lastName';
        objNonPrimaryContact2.firstName = 'firstName';
        objNonPrimaryContact2.Primary_Contact__c = false;
        insert objNonPrimaryContact2;
    }
}

Thanks,
This was selected as the best answer
kROOMkROOM
Does this work on bulk records also?