+ Start a Discussion
Ben RubioBen Rubio 

I'm trying to send an email to to both the previous owner of and opportunity and the new owner of the Opp when ownership changes. Can someone help with this please? I was told it could be a workflow and also apex. Can anyone share the code needed?

Best Answer chosen by Ben Rubio
HARSHIL U PARIKHHARSHIL U PARIKH
Try to follow the following steps,

1) Create a lookup field named "Previous Owner" on opportunity record which looksup to the USER obejct. (make this field hidden from other users if you want)
2) Create a process which assigns the value to this "Previous Owner" field when "Opportunity Owner" changes.
Take a look:

User-added image


User-added image

User-added image

3) Create a workflow which sends an email since now you have email for both of the owners. The Actual current Owner and Previous Owner.

Hope this helps!

All Answers

HARSHIL U PARIKHHARSHIL U PARIKH
Try to follow the following steps,

1) Create a lookup field named "Previous Owner" on opportunity record which looksup to the USER obejct. (make this field hidden from other users if you want)
2) Create a process which assigns the value to this "Previous Owner" field when "Opportunity Owner" changes.
Take a look:

User-added image


User-added image

User-added image

3) Create a workflow which sends an email since now you have email for both of the owners. The Actual current Owner and Previous Owner.

Hope this helps!
This was selected as the best answer
HARSHIL U PARIKHHARSHIL U PARIKH
Here is a formula for "Previous Owner"

User-added image
Ben RubioBen Rubio
Thanks for your quick response.  I'm going to try and get back with you! 
Vijaya Kumar RegantiVijaya Kumar Reganti
Hi Rubino,

I guess this is possible in both the ways.

For now I will discuss the trigger logic here.

Have the trigger.new which has the new owner and trigger.old for the old owner and send the email using the messaging.singleEmailMessage.

And also this can be done using the process builder as well.

Best Regards,
Vijay
Vijaya Kumar RegantiVijaya Kumar Reganti
Hi Rubino,

This code snippet may be helpful.

Trigger MyTrigger on opportunity(before update) {
    
    OppTriggerClass. processEmail(trigger.new, trigger.oldMap);
}

// Trigger class
public class OppTriggerClass {
    public static void processEmail(List<Opportunity> newList, Map<Id, Opportunity> oldMap) {
        
        List<String> toAddresserList = new List<String>();
        Set<Id> oldOwnerIdSet = new Set<Id>();
        Set<Id> newOwnerIdSet = new Set<Id>();
        
        for(Opportunity opp :newList) {
            if(opp.OwnerId != oldMap.get(opp.Id).OwnerId) {
                oldOwnerIdSet.add(oldMap.get(opp.Id).OwnerId);
                newOwnerIdSet.add(opp.OwnerId);
            }else{
                newOwnerIdSet.add(opp.OwnerId);
            }
        }
        
        Map<Id, String> oldIdEmailMap = new Map<Id, String>();
        Map<Id, String> newIdEmailMap = new Map<Id, String>();
        // collect new emails
        for(User newOwner :[Select Id, email From User Where Id =:newOwnerIdSet]) {
            newIdEmailMap.put(newOwner.Id, newOwner.email);
        } 
        
        // Old emails
        for(User oldOwner :[Select Id, email From User Where Id =:oldOwnerIdSet]) {
            newIdEmailMap.put(oldOwner.Id, oldOwner.email);
        }
        
        // collect email Id's and send the email
        List<String> newEmailList = new List<String>();
        List<String> oldEmailList = new List<String>();
        for(Opportunity opp :newList) {
            if(opp.OwnerId != oldMap.get(opp.Id).OwnerId) {
                if(newIdEmailMap.containsKey(opp.OwnerId)) {
                    newEmailList.add(newIdEmailMap.get(opp.OwnerId));    
                }
                if(oldIdEmailMap.containsKey(oldMap.get(opp.Id).OwnerId)) {
                    oldEmailList.add(oldIdEmailMap.get(oldMap.get(opp.Id).OwnerId));    
                }
            }else{
                newEmailList.add(newIdEmailMap.get(opp.OwnerId)); 
            }
        } 
        
        // Check for the new list and the old emil list and use them as the to addresses to send the email     using the messaging.singleEmailMessage
        https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm

    }
}


Best Regards,
Vijay
Ben RubioBen Rubio
Govind Guru:  Can the email that is send to both Previoius and current Owner be sent within this same Process Builder job?
Ben RubioBen Rubio
Also, when creating an email alert, I can not select Previous Owner from the list of selected recipients.  Can you help please?User-added image
Ben RubioBen Rubio
Hello.  I create the process but have received the following error.  Not sure why error occured. I appreciate your help.  User-added image