7

I'm trying to create a simple mock of messaging Messaging.sendEmail and in my code I'm trying to do the following to construct mocked instances of Messaging.SendEmailResult

Messaging.SendEmailResult result = new Messaging.SendEmailResult();

Compiler is giving the following error:

Type cannot be constructed: Messaging.SendEmailResult

Is there a work around?

Edit:

This doesn't work either:

Type t = Type.forName('Messaging', 'SendEmailResult');
Messaging.SendEmailResult newObj = (Messaging.SendEmailResult)t.newInstance();

Bummer...

1
  • It’s not constructed. It is returned only as part of messaging.send
    – Eric
    Commented Oct 26, 2018 at 21:25

2 Answers 2

9

Yes you can construct the Messaging.SendEmailResult using the JSON deserializer

Messaging.SendEmailResult  er =(Messaging.SendEmailResult) JSON.deserialize('{}', Messaging.SendEmailResult.class);
System.debug(er);

Get your instance. Happy Days.

4
  • 1
    Now how the heck was I supposed to find that?? Thank you!! Can I do something similar for Messaging.SendEmailError? Commented Oct 27, 2018 at 14:53
  • Do you think I can do something with this to mock a SendMailResult? Can I put something into that empty json bracket? Very interested... Commented Oct 27, 2018 at 15:00
  • @mpaler these are little hack I found while coding on platform. That being said I dont see why you should not be able to reproduce the SendEmail error. I have not tried to put values in empty JSON so cant say it accepts all. Buy try and let us know if it does. Commented Oct 27, 2018 at 15:54
  • 1
    Thanks. I'm trying Messaging.SendEmailError s2 =(Messaging.SendEmailError) JSON.deserialize('{}', Messaging.SendEmailError.class); System.debug('debug: ' + s2); and debug gives 11:11:30.19 (30504543)|FATAL_ERROR|System.UnexpectedException: Salesforce System Error: 377575750-91930 (243059598) (243059598) Frankly, not sure what/how to pass anything to the deserialize step. Will continue banging my head... Commented Oct 27, 2018 at 18:14
5

Since the properties of this class are not in the official salesforce documentation, hopefully this helps someone. If you need to create an instance of this that return false for isSuccess()

Messaging.SendEmailResult failedMessageResult = (Messaging.SendEmailResult)JSON.deserialize('{"success":false}', Messaging.SendEmailResult.class);
1
  • 2
    this is incomplete, there is also the errors property which is a List<Messaging.SendEmailError> which has three properties: message, statusCode, targetObjectId
    – cropredy
    Commented Feb 8, 2020 at 0:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .