+ Start a Discussion
Etienne Gaudry 5Etienne Gaudry 5 

Test Event EndDateTime

Hi, i need to create event on lead for a test apex.

The EndDateTime (or activityDate) of this event need to be past.

I have create a lead and event on this lead but i can't choose a negative EndDateTime.
How can i do ?

It's possible to set a date time on Start Test for launch the test in the futur? Ty

Best Answer chosen by Etienne Gaudry 5
Alain CabonAlain Cabon
What is your code ?
 
@isTest
public class PastDate_TEST {
    @isTest
    private static void testMethod1 () {
        Event evt = new Event(startDateTime=datetime.now()-2,EndDateTime=datetime.now()-1);
        insert evt;
        evt.ActivityDateTime = evt.StartDateTime;
        update evt;
        DateTime createdDatetime = datetime.now() - 2;
        Test.setCreatedDate(evt.Id, createdDatetime);
        Event evt2 = [select createdDate,startDateTime,EndDateTime,ActivityDateTime from event where id = :evt.id];
        system.debug('createdDate:' + evt2.createdDate + ' > ' + evt2.ActivityDateTime + ' > start : ' + evt2.StartDateTime + '> end : ' + evt2.EndDateTime);
    } 
}

 

All Answers

Alain CabonAlain Cabon
What is your code ?
 
@isTest
public class PastDate_TEST {
    @isTest
    private static void testMethod1 () {
        Event evt = new Event(startDateTime=datetime.now()-2,EndDateTime=datetime.now()-1);
        insert evt;
        evt.ActivityDateTime = evt.StartDateTime;
        update evt;
        DateTime createdDatetime = datetime.now() - 2;
        Test.setCreatedDate(evt.Id, createdDatetime);
        Event evt2 = [select createdDate,startDateTime,EndDateTime,ActivityDateTime from event where id = :evt.id];
        system.debug('createdDate:' + evt2.createdDate + ' > ' + evt2.ActivityDateTime + ' > start : ' + evt2.StartDateTime + '> end : ' + evt2.EndDateTime);
    } 
}

 
This was selected as the best answer
Etienne Gaudry 5Etienne Gaudry 5

Ty Alain, your code work !

i hadn't used DateTime, and Test.setCreatedDate :)