+ Start a Discussion
Russell baker 1Russell baker 1 

Test class for event count trigger:

Hi Experts,
Below is my trigger, I need test call for this trigger Plz help !

trigger OpenEventCount on Event (after insert, after update, after delete, after undelete) {
  
 List<Event> eventList = (Trigger.isInsert|| Trigger.isUnDelete) ? Trigger.new : Trigger.old;
  
     List<Id> eventIds = new List<Id>();
     for (Event evt : eventList) {
         eventIds.add(evt.WhatID);
     }
     
     List<Opportunity> oppList = [
             select
                 id,
                 (select id, WhatID from Events),
                  Open_Events__c,Total_Events__c
             from
                 Opportunity
             where
                 id in :eventIds];
  
     for (Opportunity opp : oppList) {
         Integer count = 0;
         for(Event evt : opp.Events)
          {
            if(evt.WhatId != null) 
            count += 1;
          }  
               
         opp.Total_Events__c = opp.Events.size();
         opp.Open_Events__c = count;
     }
     update oppList;    
  
 }
Best Answer chosen by Russell baker 1
David ZhuDavid Zhu
For your reference, I scarmble up test method Pseudocode  code for after insert and update. You only need to make sure all mandatory fields are there and fix typos.
You can create your own test method for delete and undelete.
 
publice testmethod void OppEventInsertUpdate_Test()
{

	Profile p = [SELECT Id FROM Profile WHERE Name='administrators'];

	//create a user
	  User usr = new User(Alias = 'user1',Email='testuser1@testorg.com',EmailEncodingKey='UTF-8', 			LastName='Testing1', LanguageLocaleKey='en_US',LocaleSidKey='en_US', ProfileId = 		p.Id,TimeZoneSidKey='America/Los_Angeles', UserName='user1@testorgasdf.com');

	inser usr;

	//create an account
	Account acct = new Account( Name = 'Test Account' );
	insert acct;

	//create an opportunity
	Opportunity opp = new Opportunity(name = 'new opp',accountid=acct.id,closedate = '11/20/2015',stagename = 'prospecting');
	insert opp;

	// create the event and insert it to fire the trigger
    Event evnt = new Event( Name = 'Test Event', ownerid= usr.Id, subject='call',StartDateTime = 'xxxx', EndDateTime='xxxxx',whatid=opp.id);
    insert event;

	//may add your assertion here for after insert trigger


	Event evnt1 = [select .... from event where id = :evnt.id];
	evnt1.Name = 'new name';
	update event1;

	//may add your assertion here for after update trigger

}

 

All Answers

srlawr uksrlawr uk
Do you have any existing test code for this/a current attempt?

Is there any specific part of the test you are struggling with, or are you just after someone to write the whole thing for you ;)

There is a really good trailhead module on writing test classes for triggers here, that you can complete in about 15 minutes

https://developer.salesforce.com/trailhead/apex_testing/apex_testing_triggers

There is also a developer blog on the topic here, which provides a fairly complicated trigger, and how to test it.

https://developer.salesforce.com/page/How_To_Test_Your_Apex_Triggers

I think if you have a crack at it, and then post your code and any problems you have with it people (namely I) would be much more willing to help out with it! You never know though, someone might just write you a test class ;)
 
David ZhuDavid Zhu
For your reference, I scarmble up test method Pseudocode  code for after insert and update. You only need to make sure all mandatory fields are there and fix typos.
You can create your own test method for delete and undelete.
 
publice testmethod void OppEventInsertUpdate_Test()
{

	Profile p = [SELECT Id FROM Profile WHERE Name='administrators'];

	//create a user
	  User usr = new User(Alias = 'user1',Email='testuser1@testorg.com',EmailEncodingKey='UTF-8', 			LastName='Testing1', LanguageLocaleKey='en_US',LocaleSidKey='en_US', ProfileId = 		p.Id,TimeZoneSidKey='America/Los_Angeles', UserName='user1@testorgasdf.com');

	inser usr;

	//create an account
	Account acct = new Account( Name = 'Test Account' );
	insert acct;

	//create an opportunity
	Opportunity opp = new Opportunity(name = 'new opp',accountid=acct.id,closedate = '11/20/2015',stagename = 'prospecting');
	insert opp;

	// create the event and insert it to fire the trigger
    Event evnt = new Event( Name = 'Test Event', ownerid= usr.Id, subject='call',StartDateTime = 'xxxx', EndDateTime='xxxxx',whatid=opp.id);
    insert event;

	//may add your assertion here for after insert trigger


	Event evnt1 = [select .... from event where id = :evnt.id];
	evnt1.Name = 'new name';
	update event1;

	//may add your assertion here for after update trigger

}

 
This was selected as the best answer
Russell baker 1Russell baker 1
Hi David,

This is work for me:

@isTest
public class OpenEventCounttest{
  
    private static testmethod void unitTest(){
    
    // Profile p = [SELECT Id FROM Profile WHERE Name='SF-BIS_2015'];

    //create a user
     // User usr = new User(Alias = 'user1',Email='testuser1@testorg.com',EmailEncodingKey='UTF-8', LastName='Testing1', LanguageLocaleKey='en_US',LocaleSidKey='en_US', ProfileId = p.Id,TimeZoneSidKey='America/Los_Angeles', UserName='user1@testorgasdf.com');

   // insert usr;
      Account acc=new Account();
          acc.Name='Demo';
           //Add Mandatory fields if any for Account
         insert acc;
         acc=[SELECT Name FROM Account WHERE Id=:acc.Id];
         System.assertEquals(acc.Name,'Demo');
      Opportunity opp=new Opportunity();
          opp.CloseDate=System.Today();
          opp.StageName='Needs Analysis';
          opp.Name='TestOpp';
           //Add Mandatory fields if any for opportunity
          Insert opp;
          opp=[SELECT Name FROM Opportunity WHERE Id=:opp.Id];
         System.assertEquals(opp.Name,'TestOpp');
      Event tsk=new Event();
          //tsk.ownerid= usr.Id;
          tsk.WhatId =opp.Id;
          tsk.subject='call';
          tsk.StartDateTime = datetime.now();
          tsk.EndDateTime= DateTime.now();
           //Add Mandatory fields if any for tassk
         insert tsk;
         tsk=[SELECT Subject FROM Event WHERE Id=:tsk.Id];
         System.assertEquals(tsk.Subject,'call');
         
    }
}

Thank you so much to show me right path. i much appriciate your help buddy.

Regards
Russell.