• Jameson Ian
  • NEWBIE
  • 0 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
Hi everyone.  I hope you can help with an issue i'm having around sending emails from apex.

What's the setup?
I have a web-to-case form setup, which is working fine in all aspects. I have a trigger/class that will verify the information and send an email back if details could not be found/matched in salesforce.  Valid cases come through just fine, having the System user as the CreatedBy.  Invalid or unmatched details have a process in place where the case is deleted and an email sent out to the address provided on the form asking the person to call instead.  The sender of this email is set from the Oeg-Wide Email Address list.  Here's an extract of the code handling this:
EmailTemplate emailTemplate = [SELECT Id, Body, HtmlValue FROM EmailTemplate WHERE DeveloperName = 'Case_W2C_Details_Mismatch_Template'];
            System.debug('Email template ID = '+emailTemplate.Id);
            OrgWideEmailAddress owea = [SELECT Id, Address, DisplayName FROM OrgWideEmailAddress WHERE Address = 'No-Reply@company.co.uk'];
            System.debug('Email address Id for sender = '+owea.Id);
            Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
            if ( owea != null) {email.setOrgWideEmailAddressId(owea.Id);}
            email.setToAddresses(emailAddresses);
            email.setSaveAsActivity(false);
            email.setSubject('Online Enquiry');
            email.setHtmlBody(emailTemplate.HtmlValue);
            email.setTemplateId(emailTemplate.Id);

            if(EmailDeliverabilityEnabled){
                Messaging.SendEmailResult [] res = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
                System.debug('Send email result' + res);
            }
            else {
                System.debug('WebToCaseService_Error: Email Deliverability is not enabled');
            }

So what's the issue then?
Every part of the process is working fine except the latter, where details are unmatched.  No email is going out to the address provided and in the logs i get this error:

EXCEPTION_THROWN|[169]|System.EmailException: SendEmail failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_OR_READONLY, Not profiled to access this Org-wide Email Address: []

What have i already tried?
1. I went to the no-reply email address in the Org-Wide Email Adresses and edited it to "Allow All Profiles to Use this From Address"

User-added image
This does work, however it allows users to be able to select this email when sending emssages via salesforce - whcih we don't want. 

2. If i "Allow only selected profiles.." and include System Administrator the above error is still faced. 

3. If i "Allow only selected profiles.." and manually select all listed profiles - i still get the same error.

4. I then set up another default no-reply address as a "Special Purpose Org-Wide Email Address" and changing the code to use this instead.  This also worked but is an address inaccessible to selected profiles, so doesn't resolve the issue without creating another.

My Questions then
Why would System user not be allowed access to Org-Wide Emails?  Is there a way around this without setting up a second no-reply address specifically for this purpose?

Many thanks for reading through this.
Aamir
  • July 26, 2021
  • Like
  • 1