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
aam1raam1r 

System User access to Org-Wide Email Address

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
Gary WGary W
Hi aam1r,

Did you ever find a solution for this issue? I'm facing the exact same problem.
Jim Jim 3Jim Jim 3
Blessings to everyone here. My review is to inform you all about using spells to win liege sums of money in your sports bets and lottery games. I've been reluctant about spells because everything seems too good to be true until I was referred to Dr Amber by an ex co-worker of mine he helped to win the lottery. I was amazed about it and I took the risk because I've been playing sports bets for more than 15 years without winning huge sums. I told Dr Amber I needed to win my sports betting and he accepted my request and prepared a spell for me and also directed me on how to place my bets on my next game. I did as he said and after the game was played, I checked my ticket to see if I was able to win any of the games I played and I saw that I won all my sports bets to receive the sum of 65,000,000 Million Pounds. Now, I've verified that Dr Amber is  a gifted and great man with genuine powers. He's reachable on WA +12096910783 or phone +13183065044 or email: amberlottotemple@yahoo.com
Jameson IanJameson Ian
Thanks for sharing such a great information during this discussion. Definitely I will try the positive points on our sharjah warriors squad startup (https://uaeplt20.com/sharjah-warriors-squad-for-ilt20-2023/). So that we could improve it and bring some growth in it. Although Using these points will be also helpful for making our system easy for our employees and they will also perform better.
Ulysses ShawUlysses Shaw
Click gear icon in the upper right and click Setup. In the Quick Find search on the left enter Organization-Wide Addresses. Click Organization-Wide Addresses. Click Add for "User Selectable Organization-Wide Email Addresses" https://www.mylincolnportal.net/