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

Inconsistent behaviour with Messaging.sendEmailResult and Messaging.sendEmailError


Anybody come across this problem before?  Here's some code to send out a single e-mail:

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage ();
mail.setToAddresses ( new String[] { 'xxx.yyy.zzz' } );
mail.setSubject ( 'TEST' );
mail.setPlainTextBody ( 'TEST' );

Messaging.sendEmailResult[] results = Messaging.sendEmail ( 
                                           new Messaging.SingleEmailMessage[] { mail } , False
for ( Messaging.sendEmailResult result : results ) {
    if ( !result.isSuccess () ) {
        System.debug ( result );
} }

Note the deliberately bogus e-mail address.  When you run this, the following text is debug'ed out:

      getMessage=Invalid to address xx.yyy.zzz;

Note that the getErrors() "member" is a Messaging.SendEmailError[] array, as specified in the Apex Ref Guide.

Now change the loop to

for ( Messaging.sendEmailResult result : results ) {
if ( !result.isSuccess () ) {
Messaging.sendEmailError[] errs = result.getErrors ();

Seems reasonable, based on the first code fragment - but in fact it doesn't even compile:
Illegal assignment from LIST:Database.Error to LIST:Messaging.SendEmailError

So - change the loop as follows:

for ( Messaging.sendEmailResult result : results ) {
if ( !result.isSuccess () ) {
Database.error[] errs = result.getErrors ();

and it compiles, but now throws a run-time error, to the effect that you can't cast a Messaging.SendEmailError object to a
Database.error. Seems like some sort of internal inconsistency.

OK - now here's the real WTF about this: Everything I've written here was demonstrably true until 30 min ago - but as of now -
 the last loop does not throw that run-time error any more! I would just discard this msg, but it took me a certain amount of effort
and now I'd like to share my worry about the behaviour of production orgs (yes, I was testing both in Sandbox and in Prod
throughout) changing in the middle of a working day.

Thoughts/insight anyone?

did you ever get an answer on this?  i have been experiencing the exact same thing today and get the conversion error at runtime as well as the compiler error at compile time depending on whether i use Database.Error or Messaging.SendEmailError respectively...

Message Edited by gregs on 12-04-2008 03:47 PM
I filed a case for this with support
Has anyone found a solution for this?   We are getting the exact same issue and there appears to be no solution.
There is no solution to this that I am aware of.  I filed a case with support some time ago but have not heard back from them, so i doubt this issue is fixed... You might want to file a support case to make sure you get a resolution to this.