I have a method that should return an "Id" when it finds a record, but if it does not find a matching record then it can't return an Id. For example, I run a query on the Account object:
private static Id getID( String param ) {
List<Account> accList = [SELECT Id FROM Account WHERE customField__c = : param ];
if (accList.size() > 0 ) {
return accList[0].Id;
}
else {
}
}
My question is the code that calls the getId() method throws an error because when a record is not found I am not returning any Id back to the calling code, In a perfect world, I would have an Account record always that matched by criteria in the query, but this isn't the reality. How can I have the best of both worlds where when it does find a matching record it returns the Id, but when it doesn't I am not sure how to handle that.