function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sergio Ruiz Barrios 5Sergio 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!
Best Answer chosen by Sergio Ruiz Barrios 5
Rohit Sharma 66Rohit Sharma 66
/* Try the following code */

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

badibadi
Is there any specific reason you need a trigger? If you only need to send an email to Account Owner and Account Team if record is modified with Activo status then you can do this with Workflow rule. 

 
Rohit Sharma 66Rohit Sharma 66
/* Try the following code */

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.
Sergio Ruiz Barrios 5Sergio Ruiz Barrios 5
Hi, thanks for your reply!

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!!!
Rohit Sharma 66Rohit Sharma 66
Hi,

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.
Sergio Ruiz Barrios 5Sergio Ruiz Barrios 5
ah no, what i want is an email alert when any field in an account with status Activo is changed. do you need all the field names?

thanks for the help, really apreciate it
Sergio Ruiz Barrios 5Sergio Ruiz Barrios 5
*the trigger email alert works perfectly when the account status is changed from activo!
Rohit Sharma 66Rohit Sharma 66
/* Try the following code, it will fire when Account status is Active and you modify any other field */

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);
    }
       
}   
Sergio Ruiz Barrios 5Sergio Ruiz Barrios 5
It works!!!!! Awesome!
 
What about the email template? :)
Rohit Sharma 66Rohit Sharma 66
/* Try the following code */

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.
This was selected as the best answer
Sergio Ruiz Barrios 5Sergio Ruiz Barrios 5
Really good job. Thank you so much for you time!
Sergio Ruiz Barrios 5Sergio Ruiz Barrios 5

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

Rohit Sharma 66Rohit Sharma 66
What dynamic text you want to show or in which field you want to show the dynamic text ?
Sergio Ruiz Barrios 5Sergio Ruiz Barrios 5
My Email Template is as folows:
Estimado Agente,
Se ha modificado la información del Account con el nombre {!Account.Name}.
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 :(
Rohit Sharma 66Rohit Sharma 66
you have to set accountId in mail.setWhatId() method. But for this u have to set contactId in mail.SetTragetObjectId() function. Currently we are passing UserId in the mail.SetTragetObjectId() function.
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