Internal Server Error With apex:inputCheckboxes

I’m using an <apex:selectCheckboxes> element in a VF page, with the options coming from this controller method:

public List<SelectOption> GetTemplateOptions()
{
    List<SelectOption> options = new List<SelectOption>();

    if(templates == null)
    {
        templates = new Map<Id, Template__c>();

        for(Template__c template : [select Id, Name from Template__c where RecordType.DeveloperName = 'template'])
        {
            /* THE_BORK */
            templates.Put(template.Id, template);
        }
    }

    for(Template__c template : templates.Values()) 
    {
        /* THE_LINE */
        options.Add(new SelectOption(template.Id, template.Name));
    }

    return options;
}

I’m collecting the results in an initialised list of strings and it’s all fine and dandy. However, if I move the line marked THE_LINE and put it where it says THE_BORK, the system is throwing an internal server error when calling an action on the page.

I’ve been unable to reproduce in a developer org and obviously I’ve found a work around, but can anyone see what’s wrong here because I can’t work it out (maybe I’ve been looking at it for too long).

Answer

Assuming the issue hasn’t resolved itself, you could always try:

public SelectOption[] getTemplateOptions() {
    SelectOption[] options = new SelectOption[0];
    if(templates == null) {
        templates = new Map<Id, Template__c>(
            [SELECT Id, Name FROM Template__c WHERE RecordType.DeveloperName = 'template']
        );
    }
    for(Template__c record: templates.values()) {
        options.add(new SelectOption(record.Id, record.Name));
    }
    return options;
}

Often, simple changes in code structure can resolve problems. Othertimes, salesforce.com issues hot-fixes for errors like this. There’s nothing wrong with the original code, outside some general optimizations as I’ve provided here.

Attribution
Source : Link , Question Author : Matt Lacey , Answer Author : sfdcfox

Leave a Comment