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
MariPMariP 

get the Org Wide Address ID

Hi,

Is it possible to get the ID of the Organization Wide Address you want, to be chosen with a test on its display address ?

I would like not to put "hard code" ID.

 

I found some code somewhere and pasted it in mine, but it doesn't work :

 

// create a new single email message object  
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
	
// Assign the addresses for the To, CC and BCC lists to the mail object.  
email.setBccAddresses(lEmailAddressesList);

// Use Organization Wide Address  
for(OrgWideEmailAddress owa : [select id, Address from OrgWideEmailAddress]) {
if(owa.Address.contains('CSR')) email.setOrgWideEmailAddressId(owa.id); } 

// Specify the subject line for your email address.  
email.setSubject('The request has been closed : ' + closedRequest.Name);
	
// Set to True if you want to BCC yourself on the email.  
email.setBccSender(false);
	
// Optionally append the salesforce.com email signature to the email.  
// The email address of the user executing the Apex Code will be used.  
email.setUseSignature(false);

email.setHtmlBody('This request :<b> ' + closedRequest.Name +' </b>has been closed<br />' +
'Subject : ' + closedRequest.Subject__c +'<br />' +
'Short description : ' + closedRequest.Short_description__c +'<br />' +
'<p>Resolution :<b> ' + closedRequest.Answer__c +' </b></p>' +
'<p>Thank you</p>');

email.setSaveAsActivity(false);
  	
// Send the email you have created.  
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email })

For now, I haven' find any documentation about this.

Does someone have any solution for me ?

Marie

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You need to pull the displayname field back from the database too:

 

 

// Use Organization Wide Address  
for(OrgWideEmailAddress owa : [select id, Address, DisplayName from OrgWideEmailAddress]) {
if(owa.Address.contains('CSR')) email.setOrgWideEmailAddressId(owa.id); } 

 

 

All Answers

bob_buzzardbob_buzzard

These line are attempting to locate the OWA:

 

 

// Use Organization Wide Address  
for(OrgWideEmailAddress owa : [select id, Address from OrgWideEmailAddress]) {
if(owa.Address.contains('CSR')) email.setOrgWideEmailAddressId(owa.id); } 

 This is looking tor an org wide address where the email contains 'CSR' - is that correct for your instance?

 

 

 

MariPMariP

Yes, that's right.

 

We have 2 Org Wide Addresses, and I want to get the second one.

 

Its display name is :  Keystonefoods CSR Helpdesk-no reply

bob_buzzardbob_buzzard

Thing is, you are comparing the email address and not the display name.  Does the email address contain the exact 'CSR' pattern?

MariPMariP

You're right, it doesn't....

I'm gonna change my code, and let you know...

 

MariPMariP

Here is my code now :

 

// Use Organization Wide Address  
for(OrgWideEmailAddress owa : [select id, Address from OrgWideEmailAddress]) {
	if(owa.DisplayName.contains('CSR')) email.setOrgWideEmailAddressId(owa.id); } 

 And her is the message I get when I execute the code :

 

 

There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger CSR_CORE_RequestAfterUpdate caused an unexpected exception, contact your administrator: CSR_CORE_RequestAfterUpdate: execution of AfterUpdate caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: OrgWideEmailAddress.DisplayName: Class.CSR_CORE_RequestTriggers_SendSingleEmail.SendEmail: line 117, column 6".

bob_buzzardbob_buzzard

You need to pull the displayname field back from the database too:

 

 

// Use Organization Wide Address  
for(OrgWideEmailAddress owa : [select id, Address, DisplayName from OrgWideEmailAddress]) {
if(owa.Address.contains('CSR')) email.setOrgWideEmailAddressId(owa.id); } 

 

 

This was selected as the best answer
MariPMariP

ooopss...

it's evident !!!

 

It's working perfectly now !

and sorry, I'm really a novice !!!

 

thanks, thanks, THANKS !!!!

Marie

yurybondyurybond

Hi

 

I couldn't send email using  OrgWideEmailAddress.Id before verification of this email - it thows exception. 

 

How can I check if Adress from OrgWideEmailAddress oblect was VERIFIED?

 

thanks

aalbertaalbert

When you initially set up the org-wide email, it sends a verification email to the email address. There is a link in the email sent to the org-wide email address, that once clicked, verifies the address. Then, to double-check, the Admin can login to salesforce.com, and under Setup | Administration Setup | Email Administration | Org Wide Email Addresses, the status will be set to "Verified." 

 

yurybondyurybond

thanks for the advice,

 

but I'd like to check verification status of selected org-wide email in apex code.

sandeep@Salesforcesandeep@Salesforce

Thank you so much Bob for helping here.

Ramesh SomalagariRamesh Somalagari
Hi all,I have same issue some one can help me http://salesforce.stackexchange.com/questions/47271/how-to-override-the-from-email-in-messaging-singleemailmessage
Jeremy LeiserJeremy Leiser
@MariP - Does Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email }) count as a DML operation?  If so, how does one bulkify this?
abonanaabonana
Jeremy, technically the call is already bulkified in that you are able to specify and process a list of emails: Messaging.sendEmail(new SingleEmailMessage[] {email1, email2, email3}). This doesn't count as a DML operation but it does count against daily email limits and bulkifying will not help you there.