function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AlphaPAlphaP 

Easy Question (I think) - Apex Test Class Question

I'm writng a test class for a lead event trigger and I;m stuck on how to account for one part of my test class.

 

The select querey is covered - which reads:

 

List<Event> checkEvent = [select id from event where WhoId in :leadidset];

 

Now I have a part of code that looks like this that is not covered:

 

    else if ((l.Call_Back_Requested_All__c <> null) && (CheckEvent.size() == 1) && (l.isconverted==false)) {
    for(Event MyEvent : checkEvent)
    {
    MyEvent.ActivityDateTime = l.Call_Back_Requested_All__c;
    MyEvent.Description = 'Call back customer ' + l.firstname + ' ' + l.lastname +
    ' with Lead Party ID ' + l.Company + '.';
    MyEvent.Subject = 'Call back customer ' + l.firstname + ' ' + l.lastname;
    MyEvent.DurationInMinutes = 15;
    //calEvent.WhatId = Trigger.new[0].Id;
    MyEvent.ReminderDateTime = l.Call_Back_Requested_All__c.addMinutes(-15);
    MyEvent.IsReminderSet = true;
    MyEvent.Whoid = l.Id;
    updateEvent.add(MyEvent);

 

 

When I'm creating my test for this -  I can't get this part covered.   How can I make sure I insert a lead with an id that matches the whoid?   I can't assign an id in a test class, right?   If I put in the lead first, it doesn't fit the flow and cover the class.  If I try to say something like 'whoid = l1.id,' that deson't work because I haven't inserted that lead yet.

 

Does that quiestion make sense?

 

Thanks!

 

dmchengdmcheng

I don't understand your question "How can I make sure I insert a lead with an id that matches the whoid".  Your unit test code should insert Lead records, and from that you will have your Lead IDs.  Then you insert Events using those lead IDs for the whoid field.  Then you can execute your class method on the data you just created.

AlphaPAlphaP

The problem I'm running into is that this part of the code needs to have the event created before the lead.  The lead needs to match the event.   But how can I have an event that matches a lead id that hasn't been created yet?