This is confusing me and no matter how many times I read through it I can't seem to figure it out.

I'm getting a Variable does not exist: listOfDels error on the last line of code.

Can anyone see why?

// Loads of code above this...

public void GetCustomFields() {
 cf = [SELECT id, Email_to_send_confirmation_to__c, attachmentid__c, AccountID__c, courses__c, triggercheckbox__c, courses__r.Course_Notes2__c, courses__r.VenueNotesForConfirmation__c, Send_Confirmation_Automatically__c, has_confirmation_been_sent__c, courses__r.venue_address__c, courses__r.Course_Name__c, courses__r.Duration__c, courses__r.Course_Start_Date__c, courses__r.Start_Time__c, courses__r.Finish_Time__c, courses__r.Tutor2__c FROM Course_Sale__c WHERE id = :oppr];

public void GetDelNames(){
  List<Delegate__c> dels = [SELECT id, Name, AccountID__c, Courses__c from Delegate__c WHERE AccountID__c =: cf.AccountID__c AND courses__c =: cf.Courses__c];
  String listofDels;
     for(Delegate__c del: dels){
     if(listOfDels == null) listOfDels = del.Name;
     listOfDels = listOfDels+ '<br/>'+del.Name;

public PageReference emailAtt()
{EmailTemplate template = [SELECT Id, Subject, HtmlValue, Body FROM EmailTemplate WHERE Name = 'Training_Confirmation_Email'];


String subject = template.Subject;
subject = subject.replace('{!Courses__c.Course_Name__c}', cf.courses__r.Course_Name__c);

String htmlBody = template.HtmlValue;
htmlBody = htmlBody.replace('{!Courses__c.Course_Name__c}', cf.courses__r.Course_Name__c);
htmlBody = htmlBody.replace('{!Courses__c.VenueNotesForConfirmation__c}', cf.courses__r.VenueNotesForConfirmation__c);
htmlBody = htmlBody.replace('{!Courses__c.Course_Notes__c}', cf.courses__r.Course_Notes2__c);
htmlBody = htmlBody.replace('[ListOfDelegates]', GetDelNames(listOfDels));
  • 5
    GetDelNames method does not take any parameter.So why are you passing one?
    – Mr.Frodo
    Commented Jul 26, 2016 at 14:23
  • 2
    listOfDels is defined inside the GetDelNames method so out of scope within emailAtt() Change GetDelNames to return a string type and return listofDels. Just call this method in emailAtt. Commented Jul 26, 2016 at 14:38
  • What you want to do is return listofDels from GetDelNames? So the void GetDelNames() should be a string GetDetNames(). When you call the function, just say GetDelNames() without passing any variable.
    – abhi
    Commented Jul 26, 2016 at 14:41

1 Answer 1


listofDels is declared as a variable local to method GetDelNames() but referenced outside of that method (in fact, inside another method emailAtt().

If you want instance method X to reference a variable, then that variable must either be:

  • Declared within the method
  • Declared as an instance variable to the class
  • Declared as a static variable to the class
  • Thank you, I don't know why I didnt do this first, I just put the lot inside emailAtt()
    – Joe Hayes
    Commented Jul 27, 2016 at 8:15

You must log in to answer this question.

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