+ Start a Discussion
Priya134Priya134 

Test Class on Event Trigger

Hello Everyone!
How do i write test class for the trigger below. 
Trigger AccountEventTrigger on Event (Before Insert) {
    
/******************************************************************************
It controlls the creation of Events from accounts which are yet to be approved.
*******************************************************************************/
    list<Id> accountIds = new list<Id>();
    Map<Id,Account> accMap = new Map<Id,Account>();
    for(Event Event: Trigger.new){
        if(Event.whatId != null && String.valueof(Event.whatId).startsWith('001'))
            accountIds.add(Event.whatId);
    }
    if(!accountIds.isEmpty()){
        for(Account acc : [select id,Submitted_for_Approval__c from Account where id in : accountIds]){
            accMap.put(acc.id,acc);
        }   
        for(Event Event: Trigger.New){
            if(Event.whatId != null && accMap.containsKey(Event.whatId) && accMap.get(Event.whatId).Submitted_for_Approval__c!= false)
                Event.addError('New Event Cannot be Created');
        }
    } 
}
Best Answer chosen by Priya134
DeveloperSudDeveloperSud
Hi ,

Please refer the below code and mark this as solved if this works for you.
@isTest
public class AccountEventTriggerTest {

    static testMethod void myUnitTest1(){
        
        Account a= new Account ();
        a.name='Test Account';
        a.Submitted_for_Approval__c=true;
        insert a;
        
        Event e = new Event();
        e.WhatId=a.id;
        e.StartDateTime=system.today();
        e.EndDateTime=system.today()+5;
        
        Test.startTest();  
			try
			{
				insert  e;       
			}
			catch(Exception ex)
			{
				Boolean expectedExceptionThrown =  ex.getMessage().contains('New Event Cannot be Created') ? true : false;
				System.AssertEquals(expectedExceptionThrown, true);                
			}
		Test.stopTest();  
    }
}

 

All Answers

Raj VakatiRaj Vakati
Hi PK ,
Here is the code

@isTest
private class AccountEventTrigger_Test {
    private static testmethod void testcase1(){
        Account a = new Account();
        a.Name='Test' ;
        Insert a ; 
        
        Event event = new Event( Subject = 'Test Event', StartDateTime=System.today(), EndDateTime=System.today()+5);  
        event.WhatId = a.Id;
        insert event;
        
        
    }
    
}
Priya134Priya134
HI Rajamohan,

I ran the test class. it fails with this error.

User-added image
DeveloperSudDeveloperSud
Hi ,

Please refer the below code and mark this as solved if this works for you.
@isTest
public class AccountEventTriggerTest {

    static testMethod void myUnitTest1(){
        
        Account a= new Account ();
        a.name='Test Account';
        a.Submitted_for_Approval__c=true;
        insert a;
        
        Event e = new Event();
        e.WhatId=a.id;
        e.StartDateTime=system.today();
        e.EndDateTime=system.today()+5;
        
        Test.startTest();  
			try
			{
				insert  e;       
			}
			catch(Exception ex)
			{
				Boolean expectedExceptionThrown =  ex.getMessage().contains('New Event Cannot be Created') ? true : false;
				System.AssertEquals(expectedExceptionThrown, true);                
			}
		Test.stopTest();  
    }
}

 
This was selected as the best answer
Priya134Priya134
Yes DeveloperSud it works! Thanks so much!