+ Start a Discussion
Taylor Ochs 5Taylor Ochs 5 

Notify new Account Team Members Trigger

I need to write a trigger to nofity new members when they are added to an account team.  My code doesn't seem to work, any help or ideas?

trigger Notify_Account_Team on Account (before insert, before update) {
    for(Account acct : trigger.new){
        List<Messaging.SingleEmailMessage> atm = new List<Messaging.SingleEmailMessage>(); 
        EmailTemplate et=[Select id from EmailTemplate where
                          DeveloperName=:'Notify_Assign'];
            List<AccountTeamMember> recips = new List<AccountTeamMember>(
[SELECT UserId
FROM AccountTeamMember
for(AccountTeamMember rid : recips){
mail.setTargetObjectId(rid.UserId);
mail.setSenderDisplayName('Salesforce System');
mail.setUseSignature(false);
mail.setBccSender(false);
mail.setSaveAsActivity(false);
mail.setTemplateId(et.Id);
        }

    }
atm.add(mail);

}
Himanshu ParasharHimanshu Parashar
HI Taylor,

Modify your code as defiend below
 
trigger Notify_Account_Team on Account (after insert, after update) {


List<Messaging.SingleEmailMessage> atm = new List<Messaging.SingleEmailMessage>();

EmailTemplate et=[Select id from EmailTemplate where DeveloperName=:'Notify_Assign'];

//Get details from the Accountteammember object on the basis of account ids
for(AccountTeamMember  rid : [SELECT UserId
FROM AccountTeamMember where Accountid in : Trigger.newmap.keyset()]
{

mail.setTargetObjectId(rid.UserId);
mail.setSenderDisplayName('Salesforce System');
mail.setUseSignature(false);
mail.setBccSender(false);
mail.setSaveAsActivity(false);
mail.setTemplateId(et.Id);
}
atm.add(mail);

}
Points to consider.

1. I have changed trigger to after insert and after update reason being you can't get Account id in before insert trigger.
2. EmailTemplate out of for loop now.
3. Singleemail message list out of loop now.


Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
 
KaranrajKaranraj
Try the below code
 
trigger Notify_Account_Team on Account (before insert, before update) {
    
List<Messaging.SingleEmailMessage> atm = new List<Messaging.SingleEmailMessage>();EmailTemplate et=[Select id from EmailTemplate where DeveloperName=:'Notify_Assign'];
    for(Account acct : trigger.new){
          
for(AccountTeamMember rid : [SELECT UserId FROM AccountTeamMember]){
mail.setTargetObjectId(rid.UserId);
mail.setSenderDisplayName('Salesforce System');
mail.setUseSignature(false);
mail.setBccSender(false);
mail.setSaveAsActivity(false);
mail.setTemplateId(et.Id);
atm.add(mail);
        }
    
    }
    Messaging.sendEmail(atm);


}


Thanks,
Karanraj
http://www.karanrajs.com
Brenda S FinnBrenda S Finn
what error are you getting? 
Taylor Ochs 5Taylor Ochs 5
I updated the code per comments from the feed, but I got this error - this is the updated code.
Line 11 Errors: Expecting a right parenthesis, found mail.setTargetObjectId(rid.UserId);
Line 14, Variable does not exist: mail

trigger Notify_Account_Team on Account (before insert, before update) {
     
List<Messaging.SingleEmailMessage> atm = new List<Messaging.SingleEmailMessage>();EmailTemplate et=[Select id from EmailTemplate where DeveloperName=:'Notify_Assign'];
    for(Account acct : trigger.new){

for(AccountTeamMember rid : [SELECT UserId FROM AccountTeamMember]){
mail.setTargetObjectId(rid.UserId);
mail.setSenderDisplayName('Salesforce System');
mail.setUseSignature(false);
mail.setBccSender(false);
mail.setSaveAsActivity(false);
mail.setTemplateId(et.Id);

    atm.add(mail);
    
        }
     
    }

    Messaging.sendEmail(atm);


}
Himanshu ParasharHimanshu Parashar
Hi Taylor,

You need to define singel email message at that line.  I am assuming that you are using my code because other code has some governer limit issues.
trigger Notify_Account_Team on Account (before insert, before update) {
     
List<Messaging.SingleEmailMessage> atm = new List<Messaging.SingleEmailMessage>();

EmailTemplate et=[Select id from EmailTemplate where DeveloperName=:'Notify_Assign'];

//Get details from the Accountteammember object on the basis of account ids
for(AccountTeamMember  rid : [SELECT UserId
FROM AccountTeamMember where Accountid in : Trigger.newmap.keyset()])
{
   Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
   mail.setTargetObjectId(rid.UserId);
   mail.setSenderDisplayName('Salesforce System');
   mail.setUseSignature(false);
   mail.setBccSender(false);
   mail.setSaveAsActivity(false);
   mail.setTemplateId(et.Id);
}

    Messaging.sendEmail(atm);


}


Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

Taylor Ochs 5Taylor Ochs 5
Yes I am using your code, Does that mean I type in the API name of the email template I want to use for this example? Thanks! Taylor Ochs Salesforce Success Manager Cheshire Impact E: Taylor@cheshireimpact.com W: www.CheshireImpact.com
Himanshu ParasharHimanshu Parashar
yes that is correct.
Rahul BorgaonkarRahul Borgaonkar
While writing trigger you should always consider bulk processing, this trigger will fail if query
SELECT UserId FROM AccountTeamMember where Accountid in : Trigger.newmap.keyset() returns more than 10 records. As
Total number of sendEmail methods allowed 10 in one transaction.
Then you may have to use Batch Apex to send mails as limit reset for each batch.

Regards,
Himanshu ParasharHimanshu Parashar
HI Taylor,

I missed one line in my code.

put following line inside the for loop at the end.
 
atm.add(mail);