You need to sign in to do that
Don't have an account?
Hannah 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
}
}
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
}
}
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
Event evnt = new Event( Name = 'Test Event', ActivityDate = '2016-10-31', WhoId = b.id);
Try that.
Error: Compile Error: Invalid field Name for SObject Event at line 8 column 32
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);
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
}
}