I want to send him an email
I have done with the new owner using workflow rules.....
답변 3개
This is the trigger taht Icreated.....and it is working fine
but still need the apex class to write for this...can anyone help
trigger AccountOwnerUpdate on Account (before update) {
List<Messaging.SingleEmailMessage> mails= new List<Messaging.SingleEmailMessage>();
List<User> obsoleteUsers = new List<User>();
//Ensure that both Maps contain the same ID's
system.assert(trigger.oldMap.keySet().containsAll(trigger.newMap.keySet()));
system.assert(trigger.newMap.keySet().containsAll(trigger.oldMap.keySet()));
system.assert(trigger.oldMap.keySet().size()>0);
Boolean test=false;
Account oldAcc = null;
User oldAccOwner = null;
Account newAcc = null;
User newAccOwner = null;
for (Account acc : trigger.new) {
newAcc=acc;
newAccOwner=[select Id from User where Id=:newAcc.OwnerId];
if ((trigger.oldMap!=null)&&(trigger.oldMap.get(newAcc.Id)!=null)) {
oldAcc=trigger.oldMap.get(newAcc.Id);
oldAccOwner=[select Id,Email from User where Id=:oldAcc.OwnerId];
}
system.assert(oldAcc!=null);
system.assert(oldAccOwner!=null);
system.assert(newAcc!=null);
system.assert(newAccOwner!=null);
if (newAccOwner.Id != oldAccOwner.Id) {
obsoleteUsers.add(oldAccOwner);
}
}
//obsolete_users = [Select Name, id, Email From User where Id in :obsolete_user_ids];
for(User u: obsoleteUsers) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setSubject('Owner Changed');
List<String> toAdrs = new List<String>();
toAdrs.add(u.Email);
system.assert(u.Email!=null);
if (u.Email==null || u.Email=='sfdc-prod-accounts@onivation.de') continue;
mail.setToAddresses(toAdrs);
mail.setBccSender(false);
mail.setUseSignature(false);
String mailContent = 'You have been removed from an Account. Please contact Admin for further details. Thanks and regards' ;
mail.setPlainTextBody(mailContent);
mails.add(mail);
}
if (mails.size()>0){
Messaging.sendEmail(mails);
}
}