+ Start a Discussion
The FinnsterThe Finnster 

Help with Test Class for a Create Event Trigger please

Hi,

I have a trigger than creates and edits an event when a custom object record name Calendar Event is created/edited.

I need help with writing a test class please. Any help will be appreciated.
 
trigger CreateEvent on Calender_Event__c (after insert,before update) {

    if(trigger.isInsert)
    {
        List<Event> eventList = new List<Event>();

        for(Calender_Event__c fac : Trigger.new)
        {
            if(fac.Name != null)
            {
                Event e = new Event();
                e.StartDateTime = fac.Start_Date_Time__c;
                e.EndDateTime = fac.End_Date_Time__c;
                e.Subject = fac.Subject__c;
                e.WhatId = fac.Id;

                eventList.add(e);
            }
        }

        insert eventList;
    }

    if(trigger.isUpdate)
    {
        List<Id> facIds = new List<Id>();

        Map<Id, Event> eventMap = new Map<Id, Event>();

        List<Event> eventList = new List<Event>();

        for(Integer i = 0; i < Trigger.new.size(); i++)
        {
            if(Trigger.new[i].Start_Date_Time__c <> Trigger.Old[i].Start_Date_Time__c)
            {
                facIds.add(Trigger.new[i].Id);
            }
        }
        
        for(Event ev : [SELECT Id, WhatId, StartDateTime, EndDateTime FROM Event WHERE WhatId IN :facIds])
        {
            eventMap.put(ev.WhatId, ev);
        }
        
        for(Calender_Event__c dfc : Trigger.new)
        {
            Event evnt = eventMap.get(dfc.Id);
            if(evnt <> null)
            {
                evnt.StartDateTime = dfc.Start_Date_Time__c;
                evnt.EndDateTime =dfc.End_Date_Time__c;

                eventList.add(evnt);
            }
        }

        update eventList;
    }
}

 
Best Answer chosen by The Finnster
Maharajan CMaharajan C
Hi Finn, 

Use the Record Type Id instead of Name : 

Id rt = Schema.SObjectType.Calender_Event__c.getRecordTypeInfosByName().get('SIFM').getRecordTypeId();

ce.RecordTypeId =  rt;

https://www.biswajeetsamal.com/blog/add-record-type-in-test-class/

Thanks,
Maharajan.C

All Answers

Maharajan CMaharajan C
Hi Finn,

Please try the below test class:
 
@isTest
Public class CreateEventTest{
	static testmethod void createEventTest(){
		Datetime StartDateTime = Datetime.now();
		Datetime EndDateTime = Datetime.now().addDays(30);
		Calender_Event__c ce = new Calender_Event__c();
		ce.Name = 'Test Event';
		ce.Start_Date_Time__c = StartDateTime;
		ce.End_Date_Time__c = EndDateTime;
		ce.Subject__c = 'Test Calendar Subject';
		// Add the Remaining Mandatory Fields here to create the Calendar Event
		Test.startTest(); 
		insert ce;
		Test.stopTest();
	}
	static testmethod void updateEventTest(){
		Datetime StartDateTime = Datetime.now();
		Datetime EndDateTime = Datetime.now().addDays(30);
		Calender_Event__c ce = new Calender_Event__c();
		ce.Name = 'Test Event';
		ce.Start_Date_Time__c = StartDateTime;
		ce.End_Date_Time__c = EndDateTime;
		ce.Subject__c = 'Test Calendar Subject';
		// Add the Remaining Mandatory Fields here to create the Calendar Event
		insert ce;
		Test.startTest(); 
			ce.Start_Date_Time__c  = Datetime.now().addDays(5);
			update ce;
		Test.stopTest();
	}
}

Please add the system.asserts in above code.

Thanks,
Maharajan.C
Abhishek BansalAbhishek Bansal
Hi Finn,

You need to publish your events in the test class, please find the sample code below:
@isTest
public class PlatformEventTest {
    @isTest static void test1() {
        // Create test event instance
		Calender_Event__c testCalEvent = new Calender_Event__c();
		testCalEvent.Start_Date_Time__c = DateTime.Now();
		testCalEvent.End_Date_Time__c = DateTime.Now().addDays(30);
		testCalEvent.Subject__c = 'Test Calendar Event Subject';
        Test.startTest();
        // Call method to publish events
        Database.SaveResult sr = EventBus.publish(testCalEvent);
        Test.stopTest();
        // Perform validation here
        // Verify that the publish was successful
        System.assertEquals(true, sr.isSuccess());
    }
}

Let me know if you need any other help on this.

Thanks,
Abhishek Bansal.
The FinnsterThe Finnster
@Maharajan, I have added the class and ran the test but got a null pointer error on the recordtype.name.

This is the error 

Error MessageSystem.NullPointerException: Attempt to de-reference a null object
Stack TraceClass.CreateEventTest.createEventTest: line 10, column 1

 
@isTest
Public class CreateEventTest{
    static testmethod void createEventTest(){
        Datetime StartDateTime = Datetime.now();
        Datetime EndDateTime = Datetime.now().addDays(1);
        Calender_Event__c ce = new Calender_Event__c();
        ce.Start_Date_Time__c = StartDateTime;
        ce.End_Date_Time__c = EndDateTime;
        ce.Subject__c = 'Viewing';
        ce.RecordType.Name = 'SIFM';
        // Add the Remaining Mandatory Fields here to create the Calendar Event
        Test.startTest(); 
        insert ce;
        Test.stopTest();
    }
    static testmethod void updateEventTest(){
        Datetime StartDateTime = Datetime.now();
        Datetime EndDateTime = Datetime.now().addDays(1);
        Calender_Event__c ce = new Calender_Event__c();
        //ce.Name = 'Test Event';
        ce.Start_Date_Time__c = StartDateTime;
        ce.End_Date_Time__c = EndDateTime;
        ce.Subject__c = 'Viewing';
        ce.RecordType.Name = 'SIFM';
        // Add the Remaining Mandatory Fields here to create the Calendar Event
        insert ce;
        Test.startTest(); 
            ce.Start_Date_Time__c  = Datetime.now().addDays(3);
            update ce;
        Test.stopTest();
    }
}
Maharajan CMaharajan C
Hi Finn, 

Use the Record Type Id instead of Name : 

Id rt = Schema.SObjectType.Calender_Event__c.getRecordTypeInfosByName().get('SIFM').getRecordTypeId();

ce.RecordTypeId =  rt;

https://www.biswajeetsamal.com/blog/add-record-type-in-test-class/

Thanks,
Maharajan.C
This was selected as the best answer