1

I've got a bit of a crazy maze of code, the rest of the code is working so it's not super relevant, but because of the way our objects are constructed (and the specifics of how they want the class to work) I need to get the name of a lookup field via query so that I can add it into a map that will give me the id from another object. I've already written code that will give me distinct Campus_Events__c records, but I'm stumped with how to get to the School objects Name field through the lookup. It works in soql when I debug so I'm not quite sure why? Anyone have a workaround for this?

Here's a snippet of code, I'll put the entirety of the code up if needed, but it has an 800 line Map in it and would just look like a wall of text.

The Error that I'm getting is "The variable does not exist: id" when I try to assign String schoolName = a.school__r.name;

TL;DR - How do reference the name of an object and assign it to a string via it's lookup?

//Get Todays Date           
    Date d=date.today();


//Get Recruiting Events in the last 30 days
    List<Campus_Events__c> cEvents=[SELECT id, school__c, School__r.name, Event_Date__c,     Event_title__c FROM Campus_Events__c WHERE Event_Type__c='Fair' AND Event_Date__c<=:d AND Event_Date__c>=:d-30];

for (Campus_Events__c c:cEvents) {

String schoolName=a.School__r.name;
system.debug(schoolName);
}
5
  • Looks like you're already doing it right. What's not working? If schoolname in null chances are the campus event doesn't have a parent school. You might try debugging c.School__c first then c.School__r.Name Commented Sep 23, 2014 at 20:23
  • It's giving me an error that says "Variable does not exist" every where I'm trying to use c.School__r.name, originally I was trying it in a map such as "accountID=hsMap.get(c.School__r.name);" but now I'm just trying to assign it to a string so I can figure out why. I tried debugging just School__c and there's no nulls present.
    – mikesalty
    Commented Sep 23, 2014 at 20:28
  • Can you verify that the query is giving you results with: system.debug('cEvents: ' + cEvents.size()); Commented Sep 23, 2014 at 20:32
  • @MatthewMitchener it is, dphil pointed me in the right direction. I was calling the wrong object "a" rather than "c". Very embarassing :(
    – mikesalty
    Commented Sep 23, 2014 at 20:34
  • 1
    No worries. Another helpful tidbit, you can simplify your query with Event_Date__c = LAST_N_DAYS:30 Commented Sep 23, 2014 at 20:39

1 Answer 1

2

You named your variable wrong:

for (Campus_Events__c c:cEvents) {

  String schoolName= **c**.School__r.name;//I changed your variable name a to c
  system.debug(schoolName);
}
1
  • 1
    oh my god, it's been a long day :(
    – mikesalty
    Commented Sep 23, 2014 at 20:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .