3

Some of the unit tests I have been writing fail because they are executed in an organization which is not permitted to send email.

Is there some way I can/should write either the Apex code and/or the corresponding unit tests so that this will be ignored in environments where email is not permitted, but still tested in those environments where it is permitted?

I suppose I could use try/catch, but that seems inelegant. Something like:

if System.org.allowsEmailSend() {}

might be better....

2

Take a look at Messaging.reserveSingleEmailCapacity. From the docs (emphasis mine):

This method can be called when you know in advance how many addresses emails will be sent to as a result of the transaction.If the transaction would cause the organization to exceed its daily email limit, using this method results in the following error: System.HandledException: The daily limit for the org would be exceeded by this request. If the organization doesn’t have permission to send API or mass email, using this method results in the following error: System.NoAccessException: The organization is not permitted to send email.

  • So, the best I can do is try/catch? – Brian Kessler Feb 28 '14 at 14:02
  • The only way I know of. Of course, you can encapsulate a call to Messaging.reserveSingleEmailCapacity and a test of the exception for the specific type into your own allowsEmailSend() method. Store the result statically to reduce overhead. – Jason Clark Feb 28 '14 at 14:12
1

It's 2018 and there still doesn't appear to be a way to handle this more cleanly than Jason's suggestion.

My concern is that the reservation may have other side-effects. For example, what happens if the actual number of emails sent at the end of the transaction is different to the number that were reserved? (After all, the "reservation" here is not really applied - it is just being used to capture the lack of permissions, so the use of 1 as a hard-coded amount seems logical.)

I'm hoping the following will do the trick (in some utility class):

private static Boolean canSendEmails = null;

public static Boolean getCanSendEmails() {
    if (canSendEmails == null) {
        // An attempt to reserve some email capacity is the only means available for determining permissions
        // or limits issues. In the case of a lack of permissions, System.NoAccessException is thrown. If
        // limits are exhausted then System.HandledException is thrown
        try {
            Messaging.reserveSingleEmailCapacity(1);

            canSendEmails = true;
        } catch (Exception e) {
            canSendEmails = false;
        }
    }

    return canSendEmails;
}

I can then call this "getCanSendEmails()" utility function before each attempt to send emails. Being that the value is static, it will only be computed once in a given session, on the first call to getCanSendEmails.

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.