This functionality can be accomplished in multiple ways. The following example could be used from a Trigger on the Matter object. In the example provided below, a trigger fires on the Matter update. This code example sends a Matter Status of "Appeal" and sends Email Notifications to all Staff and Contact Participant record types with a Participant Role of XYZ Contact.
trigger MatterTrigger on advpm__Matter__c (after update)
{
set<Id> matterIDs = new set<Id>();
for (advpm__Matter__c m : (list<advpm__Matter__c>) trigger.new)
{
// when Matter is updated and Status is 'In Appeal'.
if (m.advpm__Status__c == 'In Appeal')
matterIDs.add( m.Id ); }
if ( !matterIDs.isEmpty() )
{
list<Messaging.SingleEmailMessage> mailingList;
mailingList = new list<Messaging.SingleEmailMessage>();
// retrieve list of Participants in role 'XYZ Contact'
// and where RecordType is either 'Staff' OR 'Contact'
advpm__Participant__c[] pList = [select Id, advpm__Matter__c, advpm__Matter__r.Name,
advpm__Matter__r.advpm__Matter_Number__c, advpm__Staff__c,
advpm__Staff__r.Email, advpm__Contact__c, advpm__Contact__r.Email
from advpm__Participant__c where advpm__Matter__c IN: matterIDs
and advpm__Role__c = 'XYZ Contact'
and RecordType.Name IN ('User', 'Contact')];
for (advpm__Participant__c p : pList)
{
if (p.advpm__Staff__c != null)
{
Messaging.SingleEmailMessage mail;
mail = new Messaging.SingleEmailMessage();
mail.setToAddresses( new String[] { p.advpm__Staff__r.Email } );
mail.setBccSender( false );
mail.setUseSignature( false );
mail.setSaveAsActivity( true );
mail.setWhatId( p.advpm__Matter__c );
mail.setSubject(p.advpm__Matter__r.Name + ' has been update.');
//prepare Email body with Matter details. Fetch Matter fields in
//Participants database query above first before using them here.
string body = '';
body += 'Matter: '+p.advpm__Matter__r.Name+' - ';
body += p.advpm__Matter__r.advpm__Matter_Number__c+'<br /><br />';
body += '<br /><br />';
body += 'Sincerely,<br /><br />';
body += UserInfo.getName()+'<br />';
body += UserInfo.getOrganizationName()+'<br />';
mail.setHtmlBody( body );
mailingList.add( mail );
}
// check if Contact has an email address
if (p.advpm__Contact__c != null && p.advpm__Contact__r.Email != null)
{
Messaging.SingleEmailMessage mail;
mail = new Messaging.SingleEmailMessage();
mail.setToAddresses( new String[] { p.advpm__Contact__r.Email } );
mail.setBccSender( false );
mail.setUseSignature( false );
mail.setSaveAsActivity( true );
mail.setWhatId( p.advpm__Matter__c );
mail.setTargetObjectId( p.advpm__Contact__c );
mail.setSubject(p.advpm__Matter__r.Name + ' has been update.');
//prepare Email body with Matter details. Fetch Matter fields in
//Participants database query above first before using them here.
string body = '';
body += 'Matter: '+p.advpm__Matter__r.Name+' - ';
body += p.advpm__Matter__r.advpm__Matter_Number__c+'<br /><br />';
body += '<br /><br />';
body += 'Sincerely,<br /><br />';
body += UserInfo.getName()+'<br />';
body += UserInfo.getOrganizationName()+'<br />';
mail.setHtmlBody( body );
mailingList.add( mail );
}
}
if (!mailingList.isEmpty()) {
Messaging.SendEmailResult[] results = Messaging.sendEmail(mailingList, false);
}
}
}