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
AnonymouseAnonymouse 

How to Create Example Test Class to Test Event Trigger

Hello,

I currently have an Event Trigger and the code coverage is only 80%. I need to assign the WhatID for an event. How may I do this with a test class? This is what I have so far:

...
Event someEvent = new Event(Subject = 'Test', 
                              ActivityDate = date.today(), 
                              StartDateTime = System.Now(),
                              EndDateTime = System.Now() + 5,
                              whatId = ???);
...
Do I need to get the whatID from a User, Account, Contact, or something else? Any help is greatly appreciated!

Sincerely,
Jackie
Best Answer chosen by Anonymouse
mritzimritzi
WhatId can be Account, Lead, Contract or Custom Object (that's child of Account).

In your test class, insert an Account before Event, use the newly inserted accounts Id in WhatId field.
 
.....
Account account = new Account(
    Name = 'Test Account'
    //add any other required field
);
insert account;
Event someEvent = new Event(Subject = 'Test', 
    ActivityDate = date.today(), 
    StartDateTime = System.Now(),
    EndDateTime = System.Now() + 5,
    whatId = account.Id);
...

If this solves your problem, please mark this as BEST ANSWER
 

All Answers

mritzimritzi
WhatId can be Account, Lead, Contract or Custom Object (that's child of Account).

In your test class, insert an Account before Event, use the newly inserted accounts Id in WhatId field.
 
.....
Account account = new Account(
    Name = 'Test Account'
    //add any other required field
);
insert account;
Event someEvent = new Event(Subject = 'Test', 
    ActivityDate = date.today(), 
    StartDateTime = System.Now(),
    EndDateTime = System.Now() + 5,
    whatId = account.Id);
...

If this solves your problem, please mark this as BEST ANSWER
 
This was selected as the best answer
AnonymouseAnonymouse
Thank you Mritzi.