You need to sign in to do that
Don't have an account?
Kiran Chodavadiya
How to trigger the email notification to the Account owner for any update in email field of contact by Apex coding?
Hi,
I'd like to set up the email notification for email update in contact field. once existing email field has been update, Account owner of this related contact recieve email notification.
I'd like to set up the email notification for email update in contact field. once existing email field has been update, Account owner of this related contact recieve email notification.
Please use the below code.
All Answers
Please use the below code.
Thanks for helping me out for this.
could you please help me to find where i did mistake in following code?. its not working properly.
trigger SendEmailtoAccountOwner on Contact (After Update) {
List<messaging.SingleEmailMessage> emails = new List<messaging.SingleEmailMessage>();
// getting updated Contact's recordid
List<id> ContactId = new List<id>();
for (Contact con: Trigger.new) {
contact oldcon = trigger.oldMap.get(con.id);
if(con.email != oldcon.email) {
id VarId = con.Id;
ContactId.add(VarId);
}
}
//related Accounts which related contact has been updated.
List<Account> RelatedAccountList = new List<Account>();
RelatedAccountList = [SELECT id,OwnerId From Account Where id In:ContactId];
List<Id> ownerIds = new List<id>();
for (Account a :RelatedAccountList) {
if (RelatedAccountList.size()>0) {
id ownerId = a.OwnerId;
ownerIds.add(ownerId);
}
}
if(ownerIds.size()>0) {
List<User> users = new List<User>();
users = [SELECT id,Name,Email From User Where id in:OwnerIds];
if (Users.size()>0){
List<String> sendTo = new List<String>();
List<String> userName = new List<String>();
for(user u:users){
sendTo.add(u.email);
userName.add(u.Name);
}
messaging.SingleEmailMessage email= new messaging.SingleEmailMessage();
email.setToAddresses(sendTo);
email.setSubject('Email field Updation');
email.setPlainTextBody('Hello'+userName+'Your Related Contact email field has Been Updated');
emails.add(email);
messaging.sendEmail(emails);
}
}
}