0

I'm trying to write a unit test for a class that uses Event sObjects and I don't know how to create an Event variable and assign it an AccountId. Whenever I try to set the AccountId manually I get an error Field is not writable: Event.AccountId

Here is the code for the class I'm trying to test:

trigger EventAppointmentKept on Event (after update) {
    VisitMeetingCountHandler handler = new VisitMeetingCountHandler();
    Set<id> acctVisitids = new set<id>();
    Set<id> acctMeetingids = new set<id>();
    List<Account> acctWithVisit = new List<Account>();
    List<Account> acctWithMeeting = new List<Account>();

    for (Event e : Trigger.new) {
        Event oldE = Trigger.oldMap.get(e.Id);

        if (!oldE.Appointment_Kept__c && e.Appointment_Kept__c) {
            if (e.Subject.containsIgnoreCase('Meeting')) {
                acctVisitids.add(e.AccountId);
            }
            if (Pattern.matches('PQ Meeting|Signing Meeting|Rewrite Meeting|Online PQ Appointment', e.Subject)) { 
                acctMeetingids.add(e.AccountId);
            } 
        }
    }

    acctWithVisit = [SELECT id, Visit_Count_Current__c FROM Account WHERE id IN: acctVisitids];
    acctWithMeeting = [SELECT id, PQ_Meeting_Count_Current__c FROM Account WHERE id IN: acctMeetingids];

    if (!acctWithVisit.isEmpty()) {
        handler.increaseVisitCountCurrent(acctWithVisit);
    }
    if (!acctWithMeeting.isEmpty()) {
        handler.increasePQCountCurrent(acctWithMeeting);
    }
}

Here is my test:

@isTest
public class testEventAppointmentKept {
    @isTest static void testEventAppointmentKept() {
        VisitMeetingCountHandler handler = new VisitMeetingCountHandler();
        Set<id> acctVisitids = new set<id>();
        Set<id> acctMeetingids = new set<id>();
        List<Account> acctWithVisit = new List<Account>();
        List<Account> acctWithMeeting = new List<Account>();

        Account testAccount1 = new Account(Name = 'TaskMeetingCompleteUnitTest', first_name__c = 'test', last_name__c = 'test', email__c = 'test@test.com');
        Account testAccount2 = new Account(Name = 'TaskMeetingCompleteUnitTest', first_name__c = 'test test', last_name__c = 'test test', email__c = 'testtest@test.com');

        insert testAccount1;
        insert testAccount2;

        List<Account> accountList = [Select Id From Account Where Name = 'TaskMeetingCompleteUnitTest' limit 2];

        Event noAppointmentKept = new Event(Appointment_Kept__c = false, whoId = testAccount1.id);
        Event appointmentKept = new Event(Appointment_Kept__c = true, subject = 'Meeting', whoId = testAccount2.id);

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

        eventList.add(noAppointmentKept);
        eventList.add(appointmentKept);

        for (Integer i = 0; i < eventList.size(); i++) {
            Event e = eventList.get(i);
            Event oldE = eventList.get(0);

            if (!oldE.Appointment_Kept__c && e.Appointment_Kept__c) {
                if (e.Subject.containsIgnoreCase('Meeting')) {
                    acctVisitids.add(e.AccountId);
                }
                if (Pattern.matches('PQ Meeting|Signing Meeting|Rewrite Meeting|Online PQ Appointment', e.Subject)) { 
                    acctMeetingids.add(e.AccountId);
                } 
            }
        }

        acctWithVisit = [SELECT id, Visit_Count_Current__c FROM Account WHERE id IN: acctVisitids];
        acctWithMeeting = [SELECT id, PQ_Meeting_Count_Current__c FROM Account WHERE id IN: acctMeetingids];

        if (!acctWithVisit.isEmpty()) {
            handler.increaseVisitCountCurrent(acctWithVisit);
        }
        if (!acctWithMeeting.isEmpty()) {
            handler.increasePQCountCurrent(acctWithMeeting);
        }
    }
}

Is there a better way to do this? Right now I have 0% code coverage.

| improve this question | | | | |
  • 1
    In order to test a trigger, you need to use DML statements. You do not literally copy the trigger code into the test method in order to test it. – sfdcfox Aug 20 '17 at 17:23
1

I think you should use WhatId instead of WhoId while setting AccountId as WhoId is used only to assign ContactId and LeadId. use the below code to create Event record in test class.

Event noAppointmentKept = new Event(Appointment_Kept__c = false, WhatId = testAccount1.id);

Event appointmentKept = new Event(Appointment_Kept__c = true, subject = 'Meeting',WhatId = testAccount2.id);

and i think there are missing fields as i run your code in anonymous window, it asked me for missing fields and the below line of code works fine for me :

Account testAccount1 = new Account(Name = 'TaskMeetingCompleteUnitTest');
insert testAccount1;

Event noAppointmentKept = new Event(ActivityDateTime=date.valueOf('2017-08-08'),DurationInMinutes=50, WhatId = testAccount1.id);

insert noAppointmentkept;
| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.