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
Robert Bange*Robert Bange* 

Cannot insert an Event with a test class

I have created a test class for testing a trigger on Event. 

I get the the error: 

Class.testTriggerOnEvent.insertEvent: line 19, column 1

Line 19 is the insert -

I would welcome suggestions to make this work...

Regards, Robert

This is the class:

@istest
 public class testTriggerOnEvent {
     static testmethod void insertEvent() {
         Event e = new Event();
         
         e.Subject='Test for trigger';
         e.WhatId='001g000000Msxb7AAB';
         e.WhoId='003g000000JkXolAAF';
         e.OwnerId='00520000003bZVYAA2';
         e.recordtypeid='012g00000000VSrAAM';
         e.Type='Visit';
         e.IsAllDayEvent=false;
         e.DurationInMinutes=60;
         e.activitydatetime=system.now();
         e.ShowAs = 'Busy';
         e.IsReminderSet = false;

        
         insert e;
                      
     }
 }
Best Answer chosen by Robert Bange*
Robert Bange*Robert Bange*
Hi James, If the test class does not read existing data this does sound very logical! thanks for your advice.

All Answers

James LoghryJames Loghry
Your test class, by design, cannot see existing data.  In your test class, you are hardcoding Ids like crazy.  The WhatId is not going to exist, so your insert will fail.  You need to create / insert a record before the event, whichever sobject that you intend to work with, and then pass that Id as the WhatId.

You should also create a User records for the WhoId and OwnerId as well.

Additionally, the RecordTypeId is likely to change between Sandbox / Production at somepoint, so I would suggest querying for the RecordType by its developer name instead of hardcoding the RecordTypeId.

Hope that helps.
Robert Bange*Robert Bange*
Hi James, If the test class does not read existing data this does sound very logical! thanks for your advice.
This was selected as the best answer