Got the compile error for the snippet of code below. Says variable record.whoId (bolded) doesn't exist. Tried adding more brackets to the if statement, capitalizing WhoId and didn't work. Here's the full code from @AdrianLarson - System.QueryException: List has no rows - How can I tie Events created for qualified Contacts to Opps created from the Contact?

Map<Id, List<Event>> events = new Map<Id, List<Event>>();
for (Event event : [
    SELECT WhoId FROM Event
    WHERE WhoId IN :contactIds
    AND WhatId = null
    AND Type = 'MQL'
    if (!events.containsKey(record.WhoId))
        events.put(**record.WhoId**, new List<Event>());
  • 1
    Please edit your post with your current code (including the loop definition). Comments are not for clutter and harder for other users to notice. :) – Adrian Larson Feb 13 '17 at 22:27
  • Sorry! I'm new. Will do – Brooks Bruce Feb 13 '17 at 22:37
  • 1
    No worries. That's why I'm trying to nudge you in the right direction. – Adrian Larson Feb 13 '17 at 22:52
  • Gonna clear out this thread now. – Adrian Larson Feb 13 '17 at 23:34

If you are looping over a List<Event>, you'll need to pull the WhoId from the loop record:

for (Event record : [SELECT WhoId, ... FROM Event WHERE ...])
    if (!events.containsKey(record.WhoId))
        events.put(record.WhoId, new List<Event>());

The documentation isn't great here, but the syntax when you declare a for loop is:

for (loop_variable_typeloop_variable_name:loop_collection)

In the code I have above:

  • the loop_variable_type is Event
  • the loop_variable_name is record
  • the loop_collection is the query

In your code, your loop_variable_name is event, so the variable record is not defined in scope. Either rename your loop variable to record, or change the code references inside to event.

  • 2
    I always recommend not using the name of an object for any class, class member, or variable. It makes it a lot easier to have bad things happen. – sfdcfox Feb 13 '17 at 23:19
  • 2
    @sfdcfox And it's just sensible practice to name single records record and collections records when there's little chance of naming collisions. – Adrian Larson Feb 13 '17 at 23:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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