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?
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:
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.
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:
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.
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
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:
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
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:
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!
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
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