You need to sign in to do that
Don't have an account?
Sergio Ruiz Barrios 5
Account Email Alert Trigger
Hi All,
I need to create a trigger on Account object every time an account record with status "Activo" is modified to send an email alert to the account owner and also all the users in the account team.
NOTE: The status field is Estado__c
Any idea how to do it?
Thanks in advance!
I need to create a trigger on Account object every time an account record with status "Activo" is modified to send an email alert to the account owner and also all the users in the account team.
NOTE: The status field is Estado__c
Any idea how to do it?
Thanks in advance!
trigger accountTrigger on Account (after update) {
List<Messaging.SingleEmailMessage> emailList = new List<Messaging.SingleEmailMessage>();
List<Messaging.SingleEmailMessage> emailList1 = new List<Messaging.SingleEmailMessage>();
List<Id> accId = new list<Id>();
// query email Template, where Developer name is your Email Template 'Template Unique Name'
EmailTemplate et=[Select id from EmailTemplate where DeveloperName =: 'CustomerPortalChangePwdEmail'];
for(Account acc : Trigger.new){
If(acc.Estado__c == 'Activo'){
accId.add(acc.Id);
system.debug('*********accId'+accId);
// send email to the account Owner
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(acc.OwnerId);
mail.setSenderDisplayName('Salesforce System');
mail.setUseSignature(false);
//mail.setBccSender(false);
mail.setSaveAsActivity(false);
//mail.setPlainTextBody('Testing');
mail.setTemplateId(et.Id);
emailList.add(mail);
system.debug('*********emailList'+emailList);
}
}
if(!accId.isEmpty()){
for(AccountTeamMember rid : [SELECT UserId FROM AccountTeamMember WHERE AccountId =: accId]){
// send email to the account Team
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(rid.UserId);
mail.setSenderDisplayName('Salesforce System');
mail.setUseSignature(false);
mail.setBccSender(false);
mail.setSaveAsActivity(false);
// mail.setPlainTextBody('Testing Account Team');
mail.setTemplateId(et.Id);
emailList1.add(mail);
system.debug('*********emailList1'+emailList1);
}
}
// send email if emailList is not empty
if(!emailList.isEmpty()){
Messaging.sendEmail(emailList);
}
// send email if emailList1 is not empty
if(!emailList1.isEmpty()){
Messaging.sendEmail(emailList1);
}
}
// Mark this as best answer if it works for you.
All Answers
trigger accountTrigger on Account (after update) {
List<Messaging.SingleEmailMessage> emailList = new List<Messaging.SingleEmailMessage>();
List<Messaging.SingleEmailMessage> emailList1 = new List<Messaging.SingleEmailMessage>();
List<Id> accId = new list<Id>();
for(Account acc : Trigger.new){
if(Trigger.oldMap.get(acc.Id).Estado__c == 'Activo') {
If(Trigger.newMap.get(acc.Id).Estado__c != 'Activo'){
accId.add(acc.Id);
system.debug('*********accId'+accId);
// send email to the account Owner
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(acc.OwnerId);
mail.setSenderDisplayName('Salesforce System');
mail.setUseSignature(false);
//mail.setBccSender(false);
mail.setSaveAsActivity(false);
mail.setPlainTextBody('Testing');
emailList.add(mail);
system.debug('*********emailList'+emailList);
}
}
}
if(!accId.isEmpty()){
for(AccountTeamMember rid : [SELECT UserId FROM AccountTeamMember WHERE AccountId =: accId]){
// send email to the account Team
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(rid.UserId);
mail.setSenderDisplayName('Salesforce System');
mail.setUseSignature(false);
mail.setBccSender(false);
mail.setSaveAsActivity(false);
mail.setPlainTextBody('Testing Account Team');
emailList1.add(mail);
system.debug('*********emailList1'+emailList1);
}
}
// send email if emailList is not empty
if(!emailList.isEmpty()){
Messaging.sendEmail(emailList);
}
// send email if emailList1 is not empty
if(!emailList1.isEmpty()){
Messaging.sendEmail(emailList1);
}
}
// Let me know if you find anny issues.
I´m afraid is not working, i can save it, but if i change any account field i dont recieve the email.
I don´t know if it matters, but the target accounts have a Record Type called "Account" with the status "Active".
Another thing, where in the code i can select the email template? (I want an email template called "modificacion Account"
Thank you very much for your help!!!
Trigger will fire only when u change the "Estado__c" from Activo to any other value. Account record type will not matter here.
Please try and let me know if it works, later will try for email template as well.
thanks for the help, really apreciate it
trigger accountTrigger on Account (after update) {
List<Messaging.SingleEmailMessage> emailList = new List<Messaging.SingleEmailMessage>();
List<Messaging.SingleEmailMessage> emailList1 = new List<Messaging.SingleEmailMessage>();
List<Id> accId = new list<Id>();
for(Account acc : Trigger.new){
If(acc.Estado__c == 'Activo'){
accId.add(acc.Id);
system.debug('*********accId'+accId);
// send email to the account Owner
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(acc.OwnerId);
mail.setSenderDisplayName('Salesforce System');
mail.setUseSignature(false);
//mail.setBccSender(false);
mail.setSaveAsActivity(false);
mail.setPlainTextBody('Testing');
emailList.add(mail);
system.debug('*********emailList'+emailList);
}
}
if(!accId.isEmpty()){
for(AccountTeamMember rid : [SELECT UserId FROM AccountTeamMember WHERE AccountId =: accId]){
// send email to the account Team
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(rid.UserId);
mail.setSenderDisplayName('Salesforce System');
mail.setUseSignature(false);
mail.setBccSender(false);
mail.setSaveAsActivity(false);
mail.setPlainTextBody('Testing Account Team');
emailList1.add(mail);
system.debug('*********emailList1'+emailList1);
}
}
// send email if emailList is not empty
if(!emailList.isEmpty()){
Messaging.sendEmail(emailList);
}
// send email if emailList1 is not empty
if(!emailList1.isEmpty()){
Messaging.sendEmail(emailList1);
}
}
What about the email template? :)
trigger accountTrigger on Account (after update) {
List<Messaging.SingleEmailMessage> emailList = new List<Messaging.SingleEmailMessage>();
List<Messaging.SingleEmailMessage> emailList1 = new List<Messaging.SingleEmailMessage>();
List<Id> accId = new list<Id>();
// query email Template, where Developer name is your Email Template 'Template Unique Name'
EmailTemplate et=[Select id from EmailTemplate where DeveloperName =: 'CustomerPortalChangePwdEmail'];
for(Account acc : Trigger.new){
If(acc.Estado__c == 'Activo'){
accId.add(acc.Id);
system.debug('*********accId'+accId);
// send email to the account Owner
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(acc.OwnerId);
mail.setSenderDisplayName('Salesforce System');
mail.setUseSignature(false);
//mail.setBccSender(false);
mail.setSaveAsActivity(false);
//mail.setPlainTextBody('Testing');
mail.setTemplateId(et.Id);
emailList.add(mail);
system.debug('*********emailList'+emailList);
}
}
if(!accId.isEmpty()){
for(AccountTeamMember rid : [SELECT UserId FROM AccountTeamMember WHERE AccountId =: accId]){
// send email to the account Team
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(rid.UserId);
mail.setSenderDisplayName('Salesforce System');
mail.setUseSignature(false);
mail.setBccSender(false);
mail.setSaveAsActivity(false);
// mail.setPlainTextBody('Testing Account Team');
mail.setTemplateId(et.Id);
emailList1.add(mail);
system.debug('*********emailList1'+emailList1);
}
}
// send email if emailList is not empty
if(!emailList.isEmpty()){
Messaging.sendEmail(emailList);
}
// send email if emailList1 is not empty
if(!emailList1.isEmpty()){
Messaging.sendEmail(emailList1);
}
}
// Mark this as best answer if it works for you.
Hi Rohit, sorry one last thing, eveyrthing works just fine but once sending the automatic email the dynamic text is not populated. Any advice??
Thanks so much again
Estimado Agente,
Para más información puede hacer clic en el siguiente enlace: {!Account.Link}
Un saludo.
But i recieve this email:
Estimado Agente,
Se ha modificado la información del Account con el nombre .
Para más información puede hacer clic en el siguiente enlace:
Un saludo.
The dinamic text is missing :(
There is a work around for this:
U have to create a dummy contact and pass that contact Id in the mail.SetTragetObjectId() function.
If(acc.active__c == 'Yes'){
system.debug('*********con'+acc.Owner.Email);
User user = [select id, name, email from user where id =:acc.ownerId];
contact con = new Contact(LastName='Sharma', AccountId = acc.Id, Email=user.email);
insert con;
Then pass these values:
mail.setTargetObjectId(con.Id);
mail.setWhatId(acc.Id);
Please try from your end.
U can follow following links as well:
https://developer.salesforce.com/forums/?id=906F00000008wGoIAI
http://salesforce.stackexchange.com/questions/81499/whatid-is-not-available-for-sending-emails-to-userids