3

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...

| improve this question | | | | |
  • It’s not constructed. It is returned only as part of messaging.send – Eric Oct 26 '18 at 21:25
4

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.

| improve this answer | | | | |
  • Now how the heck was I supposed to find that?? Thank you!! Can I do something similar for Messaging.SendEmailError? – mpaler Oct 27 '18 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... – mpaler Oct 27 '18 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. – Pranay Jaiswal Oct 27 '18 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... – mpaler Oct 27 '18 at 18:14
1

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);
| improve this answer | | | | |
  • this is incomplete, there is also the errors property which is a List<Messaging.SendEmailError> which has three properties: message, statusCode, targetObjectId – cropredy Feb 8 at 0:40

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.