3

Is there a method in which we can get all the org wide email addresses in an org with their Ids without using SOQL? I was hoping that there would be something similar to the getall method for Custom Settings or the getUserInfo class but I can't find anything.

Motivation: Just avoiding a SOQL query to the transaction.

Update:

This is what I am planning on implementing if we have to use SOQL

public static map<String,OrgWideEmailAddress> emailMap{
    get{
        if(emailMap== null){
            emailMap= new Map<String, OrgWideEmailAddress>();
            List<OrgWideEmailAddress> emailList= [SELECT Id, Address FROM OrgWideEmailAddress];
            For(OrgWideEmailAddress o : emailList){
                orgEmailMap.put(o.Address, o);               
            }
        }
        return emailMap;      
    }
    set;
}
4
  • I think SOQL query is the only option.
    – Ratan Paul
    Mar 8, 2016 at 17:46
  • 1
    You could write a batch that populates them to a List Custom Setting perhaps.
    – Adrian Larson
    Mar 8, 2016 at 17:52
  • 1
    if you do end up with the SOQL query, be sure to a) wrap it in a Util class or the like, and b) cache its result so if multiple triggers/services in the same transaction context need the orgWide email addresses, they are fetched only once
    – cropredy
    Mar 9, 2016 at 0:28
  • @crop1645 I've added how I think I should implement this to my question. Is that what you mean? If so please add as an answer and I'll accept as it looks like SOQL is the only option.
    – Girbot
    Mar 9, 2016 at 11:27

1 Answer 1

3

Org Wide email address fetching is only really available through Apex, so, what I would do is create a new method in your Util class that returns the list of org Wide email addresses in some useful way. You will want to cache the results of the SOQL so if multiple aspects of your transaction need to fetch org wide email addresses (e.g. a set of linked triggers and/or services), the SOQL is only done once since it is unlikely that you will be adding new org wide email addresses in the middle of a transaction

Here's an example (I changed some variable names to match my conventions from your OP):

public static map<String,OrgWideEmailAddress> emailAddressToOrgWideEmailMap{
    get{
        if(emailAddressToOrgWideEmailMap== null){
            emailAddressToOrgWideEmailMap= new Map<String, OrgWideEmailAddress>();
            for (OrgWideEmailAddress owea= [SELECT Id, Address FROM OrgWideEmailAddress])
                emailAddressToOrgWideEmailMap.put(owea.Address, owea);               
        }
        return emailAddressToOrgWideEmailMap;      
    }
    set; // keep public if you want to inject a known value in testmethods
}

public static SelectOption[] getOrgWideEmailAddressPickList() {
  SelectOption[] res = new List<SelectOption>();
  for (String emailAddr : emailAddressToOrgWideEmailMap)
     res.add(new SelectOption(emailAddressToOrgWideEmailMap.get(emailAddr).id,emailAddr));   
  return res;
} 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .