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
noor Zabinoor Zabi 

Create a test class for contact trigger?

trigger OpportunityTrigger on Opportunity (before update, after insert, after update, after delete, after undelete) {
    //trigger.oldMap --> Map<Id,Opportunity>
    if(Trigger.isBefore){
    for(Opportunity opp: Trigger.new) {
        if(Trigger.oldMap.get(opp.Id).StageName == 'Closed Won') {
            opp.addError('You cannot modified the opportunity when stage is Closed Won.');                                
        }
    }    }
    else{
        OpportunityTriggerHandler.rollupV3();

    }}



rollupV3:- Contact Trigger Handler

public static void rollup_v3(){
            Set<Id> accIds = getAccIds();
            List<Account> accLst = new List<Account>();
            for(AggregateResult agg : [SELECT AccountId, Count(Id)  FROM Contact 
            WHERE AccountId in: accIds AND MailingCountry != 'USA'GROUP BY AccountId]) {
                Id accId = (Id)agg.get('AccountId');
                Account acc = new Account(
                    Id= accId,
                    Number_of_non_USA_contact__c = (Integer)agg.get('expr0')
                    
                );
                accLst.add(acc);
                accIds.remove(accId);
                
            }
            //To fix the last but USA contact deleted or update to other country
            if(accIds.size() > 0) {
                for(Id accId : accIds) {
                    Account acc = new Account(
                        Id = accId,
                        Number_of_non_USA_contact__c = 0
        );
        accLst.add(acc);
    }
}

        if(accLst.size() > 0)
             update accLst;
        }

        public static Set<Id> getAccIds(){

            //trigger.new---> List<sObject>
        List<Contact> triggerOld = (List<Contact>)Trigger.old;
        List<Contact> triggerNew = (List<Contact>)Trigger.new;
        

        //Collect the account ids to update
        Set<Id> accIds = new Set<Id>();
        for(Contact con: trigger.isDelete ? triggerOld: triggerNew){
            accIds.add(con.AccountId);
        }
        System.debug('accIds: ' + accIds);
        return accIds;
        }


Test Class:

@isTest
public class ContactTriggerTest {
    @isTest Public Static void myTest(){
        Contact con = new Contact();
        test.startTest();
        Delete con; Insert con; Undelete con; Update con;
        test.stopTest();
        
    }
}
Best Answer chosen by noor Zabi
Suraj Tripathi 47Suraj Tripathi 47

Hi, Noor Zabi

Please find the solution. "Create a test class for contact trigger?"

Here you are asking for a Contact trigger but you have shared the code of Opportunity Trigger. 

But I have written a test class for Contact trigger only.

I want to tell you that if you are writing a trigger for any objects then no need to write "test.startTest() and test.stopTest()". Trigger automatically fire when we use DML(insert,update,delete etc) in test class;

@isTest
public class ContactTriggerTest {
    @isTest Public Static void myTest(){
        Account ac=new Account();
        ac.Name='Test';
        insert ac;
        
        Contact con = new Contact();
        con.LastName='Data';
        con.AccountId=ac.Id;
        insert Con;
        
        Contact conObj = new Contact();
        conObj.LastName='Data 2';
        conObj.AccountId=ac.Id;
        insert conObj;
        
        Delete con;
        Undelete con;
        
        
        Contact conObjUpdate=new Contact();
        conObjUpdate.id=conObj.id;
        conObjUpdate.LastName='Data 2 Update';
        update conObjUpdate;
        
        Contact conUpdate=new Contact();
        conUpdate.id=con.id;
        con.LastName='Data Update';
        update conUpdate;
    }
}

Please let me know it is working or not??

If it helps you please mark it as Best Answer so that other people would take reference from it.

Thank You!