I am trying to run below code in trigger.
List<User> recipients = [SELECT id FROM User WHERE UserRoleId=:operationsRole.Id];
for (User usr:recipients)
{
Messaging.SingleEmailMessage notification = new Messaging.SingleEmailMessage();
notification.setTargetObjectId(usr.Id);
notification.setWhatId(updatedOpp.Id);
notification.setTemplateId(msgTemplate.id);
notification.setSaveAsActivity(False);
notification.setSenderDisplayName('SF Robot');
List<Messaging.SendEmailResult> postStats =
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {notification});
for (Messaging.SendEmailResult r:postStats)
{
System.Debug('************postStat:' + r.isSuccess());
}
}
Best Answer
Refer documentation below
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_Messaging_SingleEmailMessage_setWhatId.htm