+ Start a Discussion
LoneStar69LoneStar69 

Trigger to default the FROM address

Hi Friends,

I want to write a trigger that defaults the FROM address of outgoing email.
I have written the below code,shows no errors but, it doesn't seem to work.

trigger Validate_OnEmail on EmailMessage (before insert, before update)
    {
       Set<Id> caseIds = new Set<Id>();
       for (EmailMessage newEmail : trigger.new)
        {
            caseIds.add(newEmail.ParentID);
        }
         Map<id,Case> a = new Map<id,Case>([SELECT RecordTypeId FROM Case WHERE RecordTypeId = 'XYZ' AND id IN :caseIds]);
        for (EmailMessage newEmail:trigger.new)
        {
            if( newEmail.Incoming != true && a.containsKey(newEmail.ParentId) )
              
    {
                OrgWideEmailAddress[] oadd = [select Id from OrgWideEmailAddress where Address = 'email@sample.com'];

if ( oadd.size() > 0 ) {
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setOrgWideEmailAddressId(oadd.get(0).Id);
    
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
            }    
        }
    }

Appreciate your help!!!
 
Best Answer chosen by LoneStar69
LoneStar69LoneStar69
https://success.salesforce.com/ideaView?id=08730000000kdYZAAY

All Answers

ManojjenaManojjena
Hi Mairuz,

Try to follow the steps and configure the default address what you want .
Setup>EmailAdministration>Organisation Wide Address>Create A record >Default Address 
User-added image

Please let me know if it helps .

Thnaks 
Manoj
LoneStar69LoneStar69
Hi,

We have multiple help desks, single case feed layout and want to default FROM address to each record type.
Wondering, is there a way that this can be accomplished?

 
LoneStar69LoneStar69
https://success.salesforce.com/ideaView?id=08730000000kdYZAAY
This was selected as the best answer