+ Start a Discussion
uptime_andrewuptime_andrew 

Test Method Errors - DmlException Ignored?

Hello,

 

Over the weekend, our sandbox was upgraded to Winter '12, and many of my Apex tests have failures now.

 

For instance, I have a test that tries to save a Lead without a Last Name (which should lead to an error for a required field missing).

 

My test:

 

static testMethod void testExceptionsConvertCloseNoLead() { 
  myGenHelpers gh = new myGenHelpers();
		
  Lead testLead = gh.getUptimeTestLead();
  insert testLead;
	            	
  testLead.LastName = null;
	            	
   myLeadConvertCloseExtension lcc = new myLeadConvertCloseExtension(new ApexPages.StandardController(testLead));
   lcc.save();
}

 

Last week, and still in production, this test passes successfully.  However, today, it fails.

 

The problem is in this code segment:

 

                try {
	                update this.myLead;
                } catch (DmlException e) {
                    for (Integer i = 0; i < e.getNumDml(); i++) {
				    	System.debug('myClass.save: error: ' + e.getDmlMessage(i));
                        this.myLead.addError(e.getDmlMessage(i)); 
                    }
                    return null;
                }

 

Instead the error being handled gracefully, the test just fails at the upsert.

 

Any thoughts?

sherodsherod

I'd contact support. I don't recall any changes to this behavior mentioned in the Release notes.

philbophilbo

Hey,

 

I don't think it's just DmlExceptions that are problematic.  We have numerous classes in our application which initialize themselves from SObject records via the following construction:

 

global with sharing class myUserClass {
    webService String Id;
    webService String Name;
//  ...plus other User fields...
 
    public myClass ( User u ) {
	try { 
            this.Id = u.Id; 
        } 
        catch ( SObjectException e ) { 
            System.debug ( 'Caught ref to unqueried User Id fld' ); 
        }
	try { 
            this.Name = u.Name; 
        } 
        catch ( SObjectException e ) { 
            System.debug ( 'Caught ref to unqueried User Name fld' ); 
        }
//      ... and similarly for other User fields ...
    }
}

 The idea being, this class can be used by other classes without them having to worry about initializing it with the exact right User fields having been queried from the database beforehand.

 

This code has worked fine in all previous SFDC releases, but in Winter '12 it fails, as now the SObjectException is not being caught - so for example initializing the above class with a User whose Name field hasn't been queried throws an exception that is not catchable (not even if you wrap the whole thing in a try/catch block that catches general Exceptions).

 

Salesforce's advice was to upgrade the API versions of these classes to 22.0.  We followed this advice, and, puzzlingly, it made the classes work correctly for a very short period of time, but then they started failing again in a matter of minutes.

 

So there seems to be a glimmer of a common thread here. 

 

Any comments from anybody else experiencing exception related issues in Winter '12, or any SFDC tech person reading this thread - are more than welcome!

 

 

uptime_andrewuptime_andrew

Speaking with support so far, they got me to upgrade the API version as well, but errors still popped up (not as many as on Monday, but errors nonetheless).

 

I'll post any workarounds/solutions that come about.

LaurentDelLaurentDel

Same here.

 

It's specifivally when we try to catch a Specific Exception. It then ignores the catch.

We are having it with QueryException and NullPointerException so far. It only happens in Winter 12 orgs, same code works fine in Summer 11.

philbophilbo

Hey all,

 

FYI - This has been identified as a Winter '12 bug and the SFDC R&D team is busy getting it fixed. 

 

uptime_andrewuptime_andrew

Thanks Philbo - did this get acknowledged somewhere publicly, or did support or your CSM tell you?

 

It would be helpful to reference this to may account manager in upcoming discussions.

 

 

philbophilbo

We got this info thru their support folks via an open case.

 

I will keep this thread current with any updates that come my way.

manubkkmanubkk

Same issue here, after the Winter 12 upgrade, many of my tests are failing! I did not even change the API version to v23 yet.

 

The failures are related to SObjectException, they are not being caught inside the try/catch block. The weird thing is that these failures are not even consistent, the tests run without failure at times.

philbophilbo

Cautious optimism.  We heard from SFDC yesterday that they were going to be deploying a fix overnight.  This morning our orgs seem to be working better.  We have not tested all our problematic sandboxes so can't speak to all instances, but it does look like this problem may have been squashed.

uptime_andrewuptime_andrew

Thanks - I ran all my tests twice this morning, with 100% success both times. 

 

Hopefully it's all resolved now.

NuDevNuDev

I'll add another confirmation on this.  This issue appears to be resolved, for the time being anyway.

SaraMorganatVenyuSaraMorganatVenyu

Regarding this issue, I had a class that was using a try-catch block to handle dmlexceptions and when I went to test that it was working by intentionally setting a field with an invalid value and then redirecting the user to a special error page that I created, I get redirected there, but the message I try to pass to that page does not get displayed. All I get returned is the following standard message:

 

invalid id: null

An unexpected error has occurred. Your development organization has been notified.

 

Why can't I pass a specific error message to a custom page?

 

BTW, my code is below:

 

//Create the agreement
            echosign_dev1__SIGN_Agreement__c agmt = new echosign_dev1__SIGN_Agreement__c();
            agmt.Name = o.name;
            agmt.echosign_dev1__Opportunity__c = o.id;
            agmt.echosign_dev1__Recipient__c = getPrimaryContact(o.accountid);     //Contact field
            agmt.echosign_dev1__RemindRecipient__c      = 'Never'; 
            agmt.echosign_dev1__SenderSigns__c          = true;
            agmt.echosign_dev1__Message__c              = 'Please sign this document';
            agmt.echosign_dev1__SignatureType__c        = 'e-Signature';
            agmt.echosign_dev1__PasswordProtectSign__c  = false; 
            agmt.echosign_dev1__SenderSigns__c          = true;           
            agmt.echosign_dev1__PasswordProtectPDF__c   = false;  
            agmt.echosign_dev1__Status__c               = 'Draft';
            
            try {
              //agmt.echosign_dev1__Opportunity__c  = 'null'; 
                insert agmt;
            }
            catch (system.Dmlexception e) {
                system.debug (e);
                error = 'An unexpected error occurred while trying to create the Echosign Agreement. Please contact your System Administrator for assistance.';
                errPage.getParameters().put('id', o.id);
                errPage.getParameters().put('err', error);
                return errPage; 
            }  

Anyone have any ideas on why this is happening?