1

I am building dynamic soql query from an object. I am getting the object in a Field and another field from a object in Salesforce for the Query. With that said I am trying to capture the Query Exception but when the Query Exception happens the code just stops and an error appears on the screen without hitting my catch...... I tried searching for a solution but I do not see one..... Anythoughts outside of quering to see if the value from the field is really an object?

Even If i put my catch above my return statement that i read somewhere it still bypasses it....

Public List<sObject> getsObjectId(String pstrObjectName, String pstrName, List<sObject> plstOrder){
    Try{
    String strQuery = 'Select Id  From ' + pstrObjectName + ' where Name =  \'' + String.escapeSingleQuotes(pstrName) + '\'';
    plstM2MOrder = Database.query(strQuery);

    return plstOrder;

    }Catch(DMLException  e){
    system.debug('exception happened' + e.getmessage() );
    clsErrorhandling.InsertLogError(e.getMessage(), strErrorHandlingObjRecord, 'trgOrder');
    Return Null;
    }            

}
2

Don't think a QueryException is a DMLException; also catch and handle QueryException.

  • Thanks --- I didnt Catch that... – CodeMonger May 16 at 14:04
2

Adding on top of Keith's answer.

You are using Database.query(), which is not a DML operation. A DMLException is thrown only on scenarios as mentioned below, and thus your operation for any issues on SOQL never reaches the catch block.

Any problem with a DML statement, such as an insert statement missing a required field on a record.

For any SOQL operations you will need to catch the QueryException:

Any problem with SOQL queries, such as assigning a query that returns no records or more than one record to a singleton sObject variable.

In your case specifically though, Database.query() will only throw a Runtime Exception if the query does not return the correct sObject type. In all other cases, the exception block will never be reached.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.