Salesforce.com Sites Competition
Not quite what you’d expect, but if you have something like this in your code:
public String foo {get; set;}
You can’t write either of these in your test suite:
String s = controller.getFoo();
controller.setFoo('bar');
Rather, you can do the following:
String s = controller.foo; controller.foo = 'bar';
If you really want to use the longer methods, you can handcode them as full functions (i.e. public String getFoo() {return foo}) instead of using APEX shorthand.
If you have been working with SOQL/Salesforce for awhile you probably know that there is no distinct option when you are running a query. That is, suppose you had this in your database:
| Name | Lunch Choice |
|---|---|
| Joe | Cheeseburger |
| Frank | Veggieburger |
| Jordan | Cheese Steak |
| Joe Jr. | Cheese Steak |
| Mary Jane | Veggieburger |
If you ran a query for lunch choices, you would get back a list like this:
| Cheeseburger |
| Veggieburger |
| Cheese Steak |
| Cheese Steak |
| Veggieburger |
But suppose all you want is the distinct choices. Like this:
| Cheeseburger |
| Veggieburger |
| Cheese Steak |
Here’s what I put together to go for my Controller:
public List<SelectOption> getItems() {
List<SelectOption> options = new List<SelectOption>();
for(StudentLunches__c sl:[Select s.lunchChoice__c From StudentLunches__c s
WHERE s.Created_Date_Time__c > TODAY ])
{
uniqueCustomObjectSet.add(sl.lunchChoice__c);
}
List<String> uniqueCustomObjectList = new List<String>(uniqueCustomObjectSet);
for(integer i=0; i<uniqueCustomObjectList.size(); i++){
options.add(new SelectOption(uniqueCustomObjectList[i],uniqueCustomObjectList[i]));
}
options.add(new SelectOption('All Lunch Choices','All Lunch Choices'));
return options;
}
This code is being used to generate a group of checkboxes populated with only the unique values. It uses the Set object (see APEX reference here) which automatically rejects non-unique elements, and then converts it to a List, which is what we need for our checkboxes (apex:selectCheckboxes). This could be used in more or less the same way for a select list (apex:selectList).