Let me preface with I have used Apex only a little bit.
I have a visual force page that will be hosted on sites publicly. I take the value of the url string and then query a custom field on the account object to return a few variables to display. The query works and returns what I need however if I get 0 results I get an error, I would expect that. I have been trying to use .size() to catch if no results but can't seem to get the syntax right. See Code below for 1 variation.
Any help and explanation would be much appreciated. Thanks!
public class application {
private final Account account;
/** This will return the original URL **/
public String currentURL { get {return site.getOriginalUrl(); } }
/** Remove any slashes **/
public String accountURL { get; set; } {
if(currentURL.contains('/')) accountURL = currentURL.replace('/','');
else accountURL = currentURL;
}
public application() {
account = [SELECT Id, Name, LogoID__c FROM Account WHERE URL_Code__c = :accountURL LIMIT 1];
if (account.size() == 0) {
return null;
} else {
return account.get(0);
}
}
public Account getAccount() {
return account;
}
}
} This is the code that worked in case others come across this.
public class application {
private final Account account;
/** This will return the origina; URL **/
public String currentURL { get {return site.getOriginalUrl(); } }
/** Remove any slashes **/
public String accountURL { get; set; } {
if(currentURL.contains('/')) accountURL = currentURL.replace('/','');
else accountURL = currentURL;
}
public application() {
List<Account> accs = [SELECT Id, Name, LogoID__c FROM Account WHERE URL_Code__c = :accountURL LIMIT 1];
if(accs != null && accs.size() > 0) {
account = accs[0];
} else {
account = null;
}
}
public Account getAccount() {
return account;
}
}