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
sfdcJonsfdcJon 

Try/Catch fails from test method...

Hey guys-

 

I have the following which has been driving me crazy:

 

I'm writing a test method for a custom controller, and the second try/catch always fails when I run the test.

 

The code works perfectly when invoked through VF, but the test method fails on the second try/catch every time...

 

Any ideas out there??

 

 

//Record Type could throw an error if the 'Name' has changed
        try{
            rt = [SELECT Id 
                         FROM RecordType 
                         WHERE Name = 'Standard Request'
                         AND SobjectType = 'Case' 
                         LIMIT 1];}
        catch(QueryException e1){error=TRUE;}
        
        //Contact record which matches the current user. Text field could be incorrect on User and throw an error
        //Cast to ID, then back to string, to get full 18 characters
        ID convertToLong = u.User_Contact_Link__c;
        String longId = convertToLong;
 
        system.debug('++++UserCon'+u.User_Contact_Link__c);
        system.debug('++++Long'+longId);
        
        try{
            c = [SELECT Id
                        FROM Contact
                        WHERE id = :longId
                        LIMIT 1];}
        catch(QueryException e2){error=TRUE;}
                  
        //If either exception occurs, then exit and throw a generic error.
        if(error == TRUE){
        
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'An error has occurred. Please open this ticket from the Change Request tab.'));    
            return null;
        }       
h8r41dh8r41d

What's the error message say?

 

Gabriel Alack
Contact Us - We Can Help!
Salesforce Superheroes
------------------------------
help@salesforcesuperheroes.com
www.salesforcesuperheroes.com
1-888-407-9578 x122

sfdcJonsfdcJon

It doesn't actually error.  The test runs fine, but it gets caught in the second  'catch' part every time. The 'longId' referenced in the query is a valid Contact id, so the query shouldn't fail, but it's still going to the catch every time.

 

Basically I can't get the test method to get past the second try/catch, even though the query shouldn't be failing.

 

It works fine from VF, but for some reason not from the test method.

h8r41dh8r41d

Why not just avoid converting the ID to a string? Have you tried this?

 

 c = [SELECT Id
                        FROM Contact
                        WHERE id = :u.User_Contact_Link__c
                        LIMIT 1];}

 

Gabriel Alack
Contact Us - We Can Help!
Salesforce Superheroes
------------------------------
help@salesforcesuperheroes.com
www.salesforcesuperheroes.com
1-888-407-9578 x122

sfdcJonsfdcJon

I had that, but there's a chance it could be 15 characters and mess up the query.  Casting to an id gives me the 18 character id just in case.