-1

I need help to create a test class for my trigger below:

trigger UpdateLeadLastMQLDate on Event (before update) {

 Map<Id, Lead> leads = new Map<Id, Lead>();

    for(Event record: Trigger.new) {
        if(record.WhoId?.getSObjectType() == Lead.sObjectType) {
            leads.put(record.WhoId, null);
        }
    }
    leads.putAll([SELECT Last_MQL_Date_Time__c FROM Lead WHERE Id = :leads.keySet()]);
    for(Event record: Trigger.new) {
        record.Lead_Last_MQL_Date_Time__c= leads.get(record.WhoId)?.Last_MQL_Date_Time__c;
    }
}

Here the test class:

@isTest (seeAllData=false)
private class TestUpdateLeadLastMQLDate {
    static testMethod void testEventTrigger() {
        // set up leads
        List<Lead> testLeads = new List<Lead>();
        Lead lead1 = new Lead();
        lead1.Company = 'Test 1 New Company';
        lead1.LastName = 'Brian';
        lead1.LeadSource = 'Website';
        lead1.Status = 'MQL';
        lead1.Email = 'test@test.com';
        lead1.Country = 'USA';
        lead1.Last_MQL_Date_Time__c = DateTime.parse('17/06/2020 13:00');
        //lead1.Business_Type__c = 'App/Business';
        //lead1.Lead_Role__c = 'Product';
        testLeads.add(lead1);
        
        Lead lead2 = new Lead();
        lead2.Company = 'Test 12';
        lead2.LastName = 'Jordan';
        lead2.LeadSource = 'Personal';
        lead2.Status = 'New';
        lead2.Email = 'test23@test.com';
        lead2.Country = 'France';
        lead2.Last_MQL_Date_Time__c = DateTime.parse('17/06/2020 13:00');
        //lead2.Business_Type__c = 'App/Business';
        //lead2.Lead_Role__c = 'Product';
        testLeads.add(lead2);
        insert testLeads;
        
        // set up Events
        List<Event> events = new List<Event>();
        Event event1 = new Event();
        event1.WhoId=testLeads[0].Id; 
        event1.Subject= 'Test0';
        event1.Type='New';
        event1.STARTDATETIME=System.today();
        event1.ENDDATETIME=System.today();
        event1.Sales_Rep__c = '0052o000009lZyVAAU';
        event1.Description = 'bla';
        events.add(event1);
        
        
        Event event2 = new Event();
        event2.WhoId=testLeads[1].Id; 
        event2.Subject= 'Test1';
        event2.Type='Other';
        event2.STARTDATETIME=System.today();
        event2.ENDDATETIME=System.today();
        event2.Sales_Rep__c = '0052o000009lZyVAAU';
        event2.Description = 'bla';
        events.add(event2);
    
        Test.startTest();
        insert events;
        Test.stopTest();
        
        /*List<Lead> leadResults = [SELECT Last_MQL_Date_Time__c FROM Lead WHERE Id IN :testLeads];*/
        List<Event> eventResults = [SELECT Lead_Last_MQL_Date_Time__c FROM Event WHERE Id IN :events];
        for (Event e : eventResults) {
            //System.assertEquals(true, l.SQL_Scheduled__c);
            System.assertEquals(e.Lead_Last_MQL_Date_Time__c, DateTime.parse('17/06/2020 13:00'));
        }
    }
}
2
  • 2
    Yes, you should include your unit test, even though it's wrong. How are we able to see where you got stuck if you don't include it?
    – sfdcfox
    Feb 2 at 20:28
  • Hi @sfdcfox, here the test class
    – Adventurer
    Feb 2 at 21:00
1

The problem here is that you trigger the Event trigger on update, but you only ever performed an insert.

There are two possible fixes here.

First, you may want to change your trigger to (before insert, before update), to make sure it runs on insert. This is what I would personally recommend.

Second, you can change your unit test to:

insert events;
Test.startTest();
update events;
Test.stopTest();

I would recommend this only if you really meant to have the event trigger fire only after an update.

4
  • Oh God @Sfdcfox i do not know if one day i can have the same knowledge as you. But as i'm null in trigger, can you please let me know where to put insert events; Test.startTest(); update events; Test.stopTest();
    – Adventurer
    Feb 2 at 21:10
  • i means where to put these 2 lines into trigger? sorry for that question...
    – Adventurer
    Feb 2 at 21:12
  • @Chloe Change Test.startTest(); insert events; Test.stopTest(); to the second code block, that would work fine. But again, I'd suggest changing your trigger logic to run before insert.
    – sfdcfox
    Feb 2 at 21:13
  • Thanks a lot @sfdcfox. I will change the trigger logic to before insert
    – Adventurer
    Feb 2 at 21:27

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