You need to sign in to do that
Don't have an account?
cutspmjerry
select returns no rows and apex stops executing
I have attached a code snippet below that is confusing me. When the select statement returns a row the debug statement is executed and the rest of the code is executed also. But when no row is returned for the select statement everything seems to stop. Even the debug statement is not executed. Why is this? I tried putting in a try-catch statement to catch a dmlexception but nothing was caught.
Thanks in advance.
public void ProductGroupChanged() {
Payment_Type_Group__c ptg;
ptg = [ SELECT ID FROM Payment_Type_Group__c WHERE Product_Group__c =:ProductGroupID AND Default__c = true LIMIT 1];System.debug('Made it here!');
if (ptg != null) {this.opg.Product_Group__c = ProductGroupID;
this.opg.Payment_Type_Group__c = ptg.ID;} else {
this.opg.Product_Group__c = null; this.opg.Payment_Type_Group__c = null;}
}
For something like this, ptg needs to be a list - i.e. Payment_Type_Group__c [] ptg;
Then returning no results doesn't error out - it's just an empty list...So you can then check to see if ptg has any records:
if (ptg.size() > 0)
the exception is not a dmlexception but a null reference exception.
catch (Exception e) took care of the problem.
Thanks!