+ Start a Discussion
Hannah ButcherHannah Butcher 

Having difficulty writing the apex test class for a trigger that updates a custom lead field from the activity date on an event.

I am struggling to write the test class for a trigger that updates a custom lead field (date_demo_scheduled_for__c) based on the ActivityDate when an event is created. The following is the apex trigger which works in the sandbox and below that is what I have for the test class. Any insight would be greatly appreciated! I am a total newbie with apex currently.
TRIGGER:

Trigger DemoEvent on Event (after insert,after update) {
Event[] eventList;
eventList = trigger.new;
for (Event e:eventList) {
String leadId = e.WhoId;
if (e.WhoId!=null) {
Lead l = [SELECT Date_Demo_Scheduled_For__c FROM Lead WHERE Id = :leadId]; {
l.Date_Demo_Scheduled_For__c = e.ActivityDate;
} update l;
}
}
}

TEST CLASS:
@isTest
private class DemoEventTest {
    static testMethod void leadFix() {
       Lead b = new Lead (Name= 'DemoEventTest', Company='Grow.com', Date_Demo_Scheduled_For__c= '2016-11-02', Date_Demo_Given__c= '2016-10-15', Source__c='LinkedIn', Medium__c='Mined Sales', Campaign__c='No Data', Request__c='No Data');
       // Insert Lead
       insert b;
      // Insert Event
      Event evnt = new Event( Name = 'Test Event', ActivityDate = '2016-10-31');
                        insert event;
       // Retrieve the new Lead
       b = [SELECT Date_Demo_Scheduled_For__c FROM Lead WHERE Id =:b.Id];
       // Test that the trigger correctly updated the date demo scheduled for
    }
}
Best Answer chosen by Hannah Butcher
JeffreyStevensJeffreyStevens
Ya - Event doesn't have a Name field.  There is a "Description" field though.

And - I'm not sure Assigning the date that way will work.  If it doesn't - I think this will work...

ActivityDate  = Date.newInstance(2016, 10, 31);

All Answers

JeffreyStevensJeffreyStevens
You probably need to assign the WhoID in the Event - to the Lead that was created.

Event evnt = new Event( Name = 'Test Event', ActivityDate = '2016-10-31', WhoId = b.id);

Try that.
Hannah ButcherHannah Butcher
Thanks for the idea. You are right that it should be tied to the lead. However, I am getting the following error now: 
Error: Compile Error: Invalid field Name for SObject Event at line 8 column 32
JeffreyStevensJeffreyStevens
Ya - Event doesn't have a Name field.  There is a "Description" field though.

And - I'm not sure Assigning the date that way will work.  If it doesn't - I think this will work...

ActivityDate  = Date.newInstance(2016, 10, 31);
This was selected as the best answer
Hannah ButcherHannah Butcher
So much closer! Thank you! It didn't give me any syntax errors, but when I ran the test I got the following error:

Time Started10/17/2016 10:33 AM
ClassDemoEventTest
Method NameleadFix
Pass/FailFail
Error MessageSystem.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [DurationInMinutes]: [DurationInMinutes]
Stack TraceClass.DemoEventTest.leadFix: line 9, column 1

The test now looks like:
@isTest
private class DemoEventTest {
    static testMethod void leadFix() {
       Lead b = new Lead (LastName='Testly', Company='Grow.com', Date_Demo_Scheduled_For__c= Date.newInstance(2016, 11, 02), Date_Demo_Given__c= Date.newInstance(2016, 10, 15), Source__c='LinkedIn', Medium__c='Mined Sales', Campaign__c='No Data', Request__c='No Data');
       // Insert Lead
       insert b;
      // Insert Event
Event evnt = new Event( Description = 'Test Event', ActivityDate  = Date.newInstance(2016, 10, 31), WhoId = b.id);
                        insert evnt;
       // Retrieve the new Lead
       b = [SELECT Date_Demo_Scheduled_For__c FROM Lead WHERE Id =:b.Id];
       // Test that the trigger correctly updated the date demo scheduled for
    }
}
JeffreyStevensJeffreyStevens
so, it looks like you have DurationInMinutes required.  It's an integer, so set it to zero or one, in your New Event() statement.  there might be others like it - I just don't know your org.