2

I am writing a try catch in my constructor but can not get past the first query of crs. Is there a limit on how many queries you can do in a try catch? What would the reason be for not allowing the controller to read the following two queries? When I run the query in SOQL querier I am returned data so I know it is not null.

I know it is not hitting the other two queries because the system.debug returns for the first query but not the other two. I have attempted to put conditional logic around the queries but still was only able to get through the first one.

Does anyone have any insight on try-catch blocks? I am sure I am missing some fundamental rule about them.

public class IDAnalyticsController {
    public Id thecurrentPageId;
    public Consumer_Risk_Summary__c crs {get;set;}
    public Consumer_Risk_Summary__c crsG2 {get;set;}
    public Consumer_Risk_Summary__c crsG3 {get;set;}

    public IDAnalyticsController(){
       thecurrentPageId = ApexPages.currentPage().getParameters().get('id');
       Id guarantorId1 = [SELECT PG1__c FROM Credit_Review__c WHERE Id = :thecurrentPageId].PG1__c;
       Id guarantorId2 = [SELECT PG2__c FROM Credit_Review__c WHERE Id = :thecurrentPageId].PG2__c;
       Id guarantorId3 = [SELECT PG3__c FROM Credit_Review__c WHERE Id = :thecurrentPageId].PG3__c;
        try {
            crs = [SELECT Id, Status__c, Bankcard_Score__c, Comply360_Grade__c, Transaction_Date__c, Credit_Review__c, Guarantor__c, Credit_Review__r.PG1_Cons_Risk_Summary__c, Credit_Review__r.PG1__c FROM Consumer_Risk_Summary__c Where Credit_Review__c = :thecurrentPageId AND Credit_Review__r.PG1__c = :guarantorId1 Order By Transaction_Date__c DESC LIMIT 1];
            system.debug('got into crs');
            crsG2 = [SELECT Id, Status__c, Bankcard_Score__c, Comply360_Grade__c, Transaction_Date__c, Credit_Review__c, Guarantor__c, Credit_Review__r.PG2_Cons_Risk_Summary__c, Credit_Review__r.PG2__c FROM Consumer_Risk_Summary__c Where Credit_Review__c = :thecurrentPageId AND Credit_Review__r.PG2__c = :guarantorId2 Order By Transaction_Date__c DESC LIMIT 1];
            system.debug('got into crsG2');
            crsG3 = [SELECT Id, Status__c, Bankcard_Score__c, Comply360_Grade__c, Transaction_Date__c, Credit_Review__c, Guarantor__c, Credit_Review__r.PG3_Cons_Risk_Summary__c, Credit_Review__r.PG3__c FROM Consumer_Risk_Summary__c Where Credit_Review__c = :thecurrentPageId AND Credit_Review__r.PG3__c = :guarantorId3 Order By Transaction_Date__c DESC LIMIT 1];
            system.debug('got into crsG3');
        }
        catch (Exception e){
            system.debug('this is the exception:' + e);
        }
    }

UPDATE:

    try {

        List<Consumer_Risk_Summary__c> searchResults = [SELECT Id, Status__c, Bankcard_Score__c, Comply360_Grade__c, Transaction_Date__c, Credit_Review__c, Guarantor__c, Credit_Review__r.PG1_Cons_Risk_Summary__c, Credit_Review__r.PG1__c FROM Consumer_Risk_Summary__c Where Credit_Review__c = :thecurrentPageId AND Credit_Review__r.PG1__c = :guarantorId1 Order By Transaction_Date__c DESC LIMIT 1];
        if (!searchResults.isEmpty()) crs = searchResults[0];
        system.debug('got into crsPG1: ' + crs);
        system.debug('this is searchResults: ' + searchResults);

        searchResults = [SELECT Id, Status__c, Bankcard_Score__c, Comply360_Grade__c, Transaction_Date__c, Credit_Review__c, Guarantor__c, Credit_Review__r.PG2_Cons_Risk_Summary__c, Credit_Review__r.PG2__c FROM Consumer_Risk_Summary__c Where Credit_Review__c = :thecurrentPageIdG2 AND Credit_Review__r.PG2__c = :guarantorId2 Order By Transaction_Date__c DESC LIMIT 1];
        if (!searchResults.isEmpty()) crsG2 = searchResults[0];
        system.debug('got into crsG2: ' + crsG2);
        system.debug('this is searchResults: ' + searchResults);

        searchResults = [SELECT Id, Status__c, Bankcard_Score__c, Comply360_Grade__c, Transaction_Date__c, Credit_Review__c, Guarantor__c, Credit_Review__r.PG3_Cons_Risk_Summary__c, Credit_Review__r.PG3__c FROM Consumer_Risk_Summary__c Where Credit_Review__c = :thecurrentPageIdG3 AND Credit_Review__r.PG3__c = :guarantorId3 Order By Transaction_Date__c DESC LIMIT 1];
        if (!searchResults.isEmpty()) crsG3 = searchResults[0];
        system.debug('got into crsG3: ' + crsG3);
        system.debug('this is searchResults: ' + searchResults);
    }

UPDATE 2: The filtering on the queries needed a more specific Guarantor to pull from as well as the current page id needed to be consistent.

Where Credit_Review__c = :thecurrentPageId AND Guarantor__c = :guarantorId1
Where Credit_Review__c = :thecurrentPageId AND Guarantor__c = :guarantorId2
Where Credit_Review__c = :thecurrentPageId AND Guarantor__c = :guarantorId3
4

If you only see the first debug statement in your log, it means your crsG2 query is the one which explodes. That is because your code assumes this WHERE clause will return at least one record:

Where Credit_Review__c = :thecurrentPageId AND Credit_Review__r.PG2__c = :guarantorId2

If it doesn't match any records, you will get a QueryException and exit the try block immediately. That goes for any of the three queries. If no data is returned an exception is thrown. If you would like to be able to check all three conditions, a common pattern is to query to a list and check if it is empty:

List<Consumer_Risk_Summary> searchResults = [/*first_query*/];
if (!searchResults.isEmpty()) crs = searchResults[0];

// you can reuse the list variable
searchResults = [/*second_query*/];
if (!searchResults.isEmpty()) crsG2 = searchResults[0];

searchResults = [/*second_query*/];
if (!searchResults.isEmpty()) crsG3 = searchResults[0];

Also, a couple notes on exception handling:

  • So I have all three queries set up the way you suggested and when I query them individually they all return a record. But when I put them in the try block I still can not get past the first query. This means to me that it thinks there is no data which is perplexing... – Olivia Feb 8 '17 at 1:01
  • @Olivia You filter on different Credit_Review__c values in your second snippet. In the first you compare them all to theCurrentPageId. – Adrian Larson Feb 8 '17 at 1:02
  • You are right, it was a part of the query that was throwing it. When filtering on the current page Id and the guarantor Id I was using the wrong variables. Where Credit_Review__c = :thecurrentPageIdG2 AND Credit_Review__r.PG2__c = :guarantorId2 needs to be: Where Credit_Review__c = :thecurrentPageId AND Guarantor__c = :guarantorId1. When I used Credit_Review__r.PG2__c it was pulling every record rather then just the one belonging to the Guarantor. – Olivia Feb 8 '17 at 1:39

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.