• spitzerg
  • NEWBIE
  • -1 Points
  • Member since 2009

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

I am trying to run a query where I expect a single object to be returned under normal conditions. However, there is the possibility that invalid data will be submitted in which case the query will not return anything. Rather than handling this by getting a list, checking its size and then proceeding it seems sensible to simply try and return the single object and catch an exception if one arises using the following:

 

 

private static Licensable_Product_Release__c parseProductInformation( XmlStreamReader reader ) {

String identifier, version;

 

// Code to get identifier and version...

 

try {

return [SELECT Name FROM Licensable_Product_Release__c WHERE Licensable_Product__r.Identifier__c = :identifier AND Version__c = :version];

} catch( QueryException e ) {

System.debug( e.getMessage() );

}

 

return null; 

 

The problem is in my test case I intentionally feed it data so that the query will fail and at that point get:

 

 

Debug Log:

 

*** Beginning Test 1: XmlProductUpdatesRequestTransformer.static testMethod void test()

 

20091030045250.098:Class.XmlProductUpdatesRequestTransformer.parseProductInformation: line 63, column 12: SOQL query with 0 rows finished in 7 ms

System.QueryException: List has no rows for assignment to SObject

 

Unless I'm not understanding something here, it seems that my try/catch is being completely ignored. I've searched the forums and have found people getting this error without having a try/catch block but then it seems that adding a try/catch has solved the problem.

 

 

Any help greatly appreciated, thanks. 

 

I am trying to run a query where I expect a single object to be returned under normal conditions. However, there is the possibility that invalid data will be submitted in which case the query will not return anything. Rather than handling this by getting a list, checking its size and then proceeding it seems sensible to simply try and return the single object and catch an exception if one arises using the following:

 

 

private static Licensable_Product_Release__c parseProductInformation( XmlStreamReader reader ) {

String identifier, version;

 

// Code to get identifier and version...

 

try {

return [SELECT Name FROM Licensable_Product_Release__c WHERE Licensable_Product__r.Identifier__c = :identifier AND Version__c = :version];

} catch( QueryException e ) {

System.debug( e.getMessage() );

}

 

return null; 

 

The problem is in my test case I intentionally feed it data so that the query will fail and at that point get:

 

 

Debug Log:

 

*** Beginning Test 1: XmlProductUpdatesRequestTransformer.static testMethod void test()

 

20091030045250.098:Class.XmlProductUpdatesRequestTransformer.parseProductInformation: line 63, column 12: SOQL query with 0 rows finished in 7 ms

System.QueryException: List has no rows for assignment to SObject

 

Unless I'm not understanding something here, it seems that my try/catch is being completely ignored. I've searched the forums and have found people getting this error without having a try/catch block but then it seems that adding a try/catch has solved the problem.

 

 

Any help greatly appreciated, thanks.