function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
cutspmjerrycutspmjerry 

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;

  }

}

BritishBoyinDCBritishBoyinDC

 

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)

 


 

SuperfellSuperfell
the query assignment to one object will throw an exception if the query doesn't return exactly one row. I can't remember the exact exception type, but its not likely to be a dmlexception.
harryZharryZ

the exception is not a dmlexception but a null reference exception.

 

cutspmjerrycutspmjerry

catch (Exception e) took care of the problem.

 

Thanks!